TransWikia.com

Get category slug and display it on a query_post

WordPress Development Asked by Dave on October 24, 2020

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

3 Answers

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

Answered by AddWeb Solution Pvt Ltd on October 24, 2020

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

Answered by Milo on October 24, 2020

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

Answered by Jamie on October 24, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP