Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I just read "How to create option page for wordpress theme" in wp.tutsplus.com - I have a problem with getting categories .

there is two parts to my question

- Part A - Get category slug and display it

In this tutorial there is a select option as follow :

$options[] = array(
    "section" => "select_section",
    "id"      => WPTUTS_SHORTNAME . "_select_input",
    "title"   => __( 'Select (type one)', 'wptuts_textdomain' ),
    "desc"    => __( 'A regular select form field', 'wptuts_textdomain' ),
    "type"    => "select",
    "std"    => "3",
    "choices" => array( "1", "2", "3")

Now, instead of the 1-2-3 choices , I want to return category slugs to display it to user - can someone please modify this code so it return the slugs for categories created on the site?

- Part B - Display category slug in query_posts format

The typical returning value for above code is this :

<?php echo $wptuts_option['wptuts_select_input']; ?>

I use this code for my query post :

<?php query_posts('showposts=1&category_name=news'); ?>

where the "news" is the category "slug" - not name of the category now , can someone please modify the code so it gets the category slug based on the option chose in the theme option page ?

I am stuck in fixing this problem and I looked every where, but cant find a solution thanks

share|improve this question
    
Thanks Jamie I tried that but got an error , it is really annoying that it is not working, I did everything and tried all possible solution which I knew , dont really know what to do now ;( –  user14798 Apr 1 '12 at 19:55
add comment

2 Answers

I am not positive how to do what you want. But I have been using this framework which is very similar to what you have above but is much easier to use

http://aquagraphite.com/2011/11/smof-documentation/

setting up options is really easy and it is supported through github. There is a place to ask these exact kind of questions. That and the framework has a lot of examples in it that you can copy and paste to get your admin area up and running. I just commented out all the example options so that I could reference them when I wanted to create one of them.

As for your answer. I don't know how to do you but you could try some things like this

"choices" => get_categories(  ); 
share|improve this answer
add comment

We see from the example that the option accepts a simple array of values:

array( "1", "2", "3")

get_categories returns an array of objects, so we need to do a bit of reformatting to make it work:

$categories = get_categories();
$slugs = array();
foreach( $categories as $category )
    $slugs[] = $category->slug;

Now we have a simple array of $slugs to pass:

$options[] = array(
    "section" => "select_section",
    "id"      => WPTUTS_SHORTNAME . "_select_input",
    "title"   => __( 'Select (type one)', 'wptuts_textdomain' ),
    "desc"    => __( 'A regular select form field', 'wptuts_textdomain' ),
    "type"    => "select",
    "choices" => $slugs
);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.