admin管理员组

文章数量:1122832

I just read "How to create option page for wordpress theme" in wp.tutsplus - 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

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 edited Aug 5, 2012 at 16:05 Amit Kosti 3,6101 gold badge22 silver badges34 bronze badges asked Apr 1, 2012 at 16:09 DaveDave 511 silver badge2 bronze badges 1
  • 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 Commented Apr 1, 2012 at 19:55
Add a comment  | 

3 Answers 3

Reset to default 0

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(  ); 

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
);

Put this code into your function.php

$terms = get_the_terms( $post->ID , 'category');
if($terms) {
  foreach( $terms as $term ) {
    $cat_obj = get_term($term->term_id, 'category');
    $cat_slug = $cat_obj->slug;
  }
}

and then set var $cat_slug into your "choices",

and then set var $cat_slug into your "choices",

$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" => $cat_slug
);

本文标签: theme developmentGet category slug and display it on a querypost