Forums

[resolved] Identifying a custom category (5 posts)

  1. barnster
    Member
    Posted 1 year ago #

    Hi all.

    I am trying to enable unique page descriptions for all pages, categories, tags and posts.

    I use this code in header.php to pull out the description based upon the content type (home, category, tag, month, post):

    <meta name="description" content="<?php if ( (is_home()) || (is_front_page()) ) {
    	echo ('homepage description goes here');
    } elseif(is_category()) {
    	echo category_description();
    } elseif(is_tag()) {
    	echo '-tag archive page for this blog' . single_tag_title();
    } elseif(is_month()) {
    	echo 'archive page for this blog' . the_time('F, Y');
    } else {
    	echo get_post_meta($post->ID, "custompagedescription", true);
    }?>">

    This works fine for all standard content but this doesn't seem to be working for a custom category for some reason; instead, it shows the description for the most recent post within the custom category.

    If I try using: <?php echo category_description() ?> somewhere else on the page, then this does pull out the description for the custom category.

    I can only assume that the code above is not identifying the custom category as a category and so is defaulting to showing the description for the most recent post.

    Anyone know why this is? If this is the case, then how would I include an elseif for a custom category?

    Thanks for your help.

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    If you mean "Custom Taxonomies" you can use the conditional tag is_tax()

  3. barnster
    Member
    Posted 1 year ago #

    You little beauty.

    That does it nicely. Final code is:

    <meta name="description" content="<?php if ( (is_home()) || (is_front_page()) ) {
    	echo ('homepage description goes here');
    } elseif(is_category()) {
    	echo category_description();
    } elseif(is_tax()) {
    	echo category_description();
    } elseif(is_tag()) {
    	echo '-tag archive page for this blog' . single_tag_title();
    } elseif(is_month()) {
    	echo 'archive page for this blog' . the_time('F, Y');
    } else {
    	echo get_post_meta($post->ID, "custompagedescription", true);
    }?>">
  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Nice! You can even simplify your code with this:

    elseif(is_category() || is_tax()) {
    	echo category_description();
    }

    the || part means "or"

  5. barnster
    Member
    Posted 1 year ago #

    Even better!

    Thank you.

Topic Closed

This topic has been closed to new replies.

About this Topic