• Resolved safi2266

    (@safi2266)


    Hi folks

    recently I add new theme to my site with options (control panel).
    this theme has multible content displaying methods, I have issue with these since I am not good enough in php
    and I looking for someone to help me to figure out
    the primary issue that my theme depand on slug not category name, my theme option.php look like something like this

    <?php 
    
    $shortname = "op";
    $default_colorscheme = "Default";
    
    $cats_array = get_categories('hide_empty=0');
    $pages_array = get_pages('hide_empty=0');
    $site_pages = array();
    $site_cats = array();
    
    foreach ($pages_array as $pagg) {
    	$site_pages[$pagg->ID] = htmlspecialchars($pagg->post_title);
    	$pages_ids[] = $pagg->ID;
    }
    
    foreach ($cats_array as $categs) {
    	$site_cats[$categs->cat_ID] = $categs->cat_name;
    	$cats_ids[] = $categs->cat_ID;
    }
    
    $op_categories_obj = get_categories('hide_empty=1');
    $op_categories = array();
    foreach ($op_categories_obj as $op_cat) {
    $op_categories[$op_cat->cat_ID] = $op_cat->category_nicename;
    }
    $categories_tmp = array_unshift($op_categories, "اختر تصنيفاً:");	
    
    $effects = array("fade", "slideRandom", "overRandom", "slideUp", "slideRight", "slideDown", "slideLeft", "overUp", "overRight", "overDown", "overLeft", "none");
    
    $options = array (
    	...
    	...
    array( 	"name" => "Content category one",
    		"desc" => "Select the category for first content category.",
    		"id" => $shortname."_content_cat_one",
    		"std" => "Select a category:",
    		"type" => "select",
    		"options" => $op_categories),	
    
    array( 	"name" => "Number of posts in the first category",
    		"desc" => "Select number of posts in the first category.",
    		"id" => $shortname."_content_number_one",
    		"type" => "select",
    		"options" => array('1','2','3','4','5','6','7','8','9','10')),	
    
    array( 	"name" => "Layout to posts for the first category",
    		"desc" => "Select the layout to posts for the first category.",
    		"id" => $shortname."_content_layout_one",
    		"std" => "One big and three small posts",
    		"type" => "select",
    		"options" => array('First post with bottom content and the rest small posts','First post full width','All posts with bottom content','All posts with right content','Small posts with right content')),
    ...
    ...

    and the output home_content.php file look something like this

    <div id="home_category_content">
    
    <?php if (get_option('op_content_one') == 'on') { ?>
    
    <div class="home_category_posts">
    
    <?php
    $featucat = get_option('op_content_cat_one');
    $numbercatposts = get_option('op_content_number_one');
    $my_query = new WP_Query('showposts='. $numbercatposts .'&category_name='. $featucat .'');
    if ($my_query->have_posts()) :
    $i = 0;
    ?> 
    
    <?php print '<div class="home_cat_line"><h2>'.$op_content_cat_one.'</h2><span></span></div>';?>

    my problem is the variable $op_content_cat_one return the category nicename (slug) and I want to get category name.
    so will you please help me with this

    TIA
    SaFi

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with get_category_by_slug()

    Change this:

    <?php print '<div class="home_cat_line"><h2>'.$op_content_cat_one.'</h2><span></span></div>';?>

    To this:

    <?php
    $cat_name = get_category_by_slug($op_content_cat_one);
    $cat_name = ($cat_name) ? $cat_name->name : $op_content_cat_one;
    ?>
    <?php print '<div class="home_cat_line"><h2>'.$cat_name.'</h2><span></span></div>';?>

    Thread Starter safi2266

    (@safi2266)

    Hello keesiemeijer,

    Thank you very match you save my life, your suggestion work fine
    but I placed

    $cat_name = get_category_by_slug($op_content_cat_one);
    $cat_name = ($cat_name) ? $cat_name->name : $op_content_cat_one;

    immediately after

    if ($my_query->have_posts()) :

    and again I am very grateful for you help but forgive I want explanation of your that code if you do not mind.

    Regards,
    SaFi

    Moderator keesiemeijer

    (@keesiemeijer)

    I don’t mind.

    the get_category_by_slug() function returns a category object if the slug exists and returns false if it doesn’t.
    You can see what’s in a category object with this:

    $cat_name = get_category_by_slug($op_content_cat_one);
    echo '<pre>';
    print_r($cat_name);
    echo '</pre>';

    This will show something similar to this:

    stdClass Object
    (
        [term_id] => 3
        [name] => My Category
        [slug] => my-category
        [term_group] => 0
        [term_order] => 0
        [term_taxonomy_id] => 3
        [taxonomy] => category
        [description] => My cool category
        [parent] => 0
        [count] => 4
        [cat_ID] => 3
        [category_count] => 4
        [category_description] => My cool category
        [cat_name] => My Category
        [category_nicename] => my-category
        [category_parent] => 0
    )

    The second line is a ternary operator.

    $cat_name = ($cat_name) ? $cat_name->name : $op_content_cat_one;

    It assigns the category name (property) value from the category object to the variable $cat_name if there is a category object, otherwise it assigns the category slug from $op_content_cat_one.
    This next example does exactly the same:

    $cat_name = get_category_by_slug($op_content_cat_one);
    
    // if $cat name is not false
    if($cat_name !== false){
      $cat_name = $cat_name->name;
    } else {
      $cat_name = $op_content_cat_one;
    }

    This is just a simple conditional statement to see if the function returned an object or not.

    I hope this helps.

    Thread Starter safi2266

    (@safi2266)

    Thanks alot keesiemeijer, it is very helpful.

    you are my hero !!

    Best Regards,

    SaFi

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘howto display category name instead of slug’ is closed to new replies.