Forums

Building my first theme - Displaying Category - Simple Question - Please help (2 posts)

  1. TylorFamous
    Member
    Posted 11 months ago #

    Hello. I am trying to do (what I thought) was a pretty simple thing. I want to display a single line of CSS at the top of the page depending on the category. This is the first template I have made so I am walking a little bit blind.

    Here is the code I tried

    <?php $current_category = single_cat_title("", false);
    
    if($current_category == "Category 1"){
    	echo "Yay! If I see this in the source code then it worked!";
    }?>

    That dose not give me anything. Apparently the Single_cat_title isn't outputting anything (or I am not sure what it is outputting. I assume it is supposed to be giving me the category name).

    I know the code should work (assuming that I have all the right values) because if I change the single_cat_title to "Category 1" it works.

    <?php $current_category = "Category 1";
    
    if($current_category == "Category 1"){
    	echo "Yay! If I see this in the source code then it worked!";
    }?>

    If anyone could help answer why this isn't working, that would be great! Thanks in advance for any help :)

  2. MichaelH
    moderator
    Posted 11 months ago #

    So you are using that code in a Category Template?

    Take a look at the WordPress Default Theme's wp-content/themes/default/archive.php for examples of single_cat_title.

    Also see:
    Template Hierarchy

Reply

You must log in to post.

About this Topic