WordPress.org

Support

Support » How-To and Troubleshooting » Displaying category description?

Displaying category description?

  • I’m using this in my header.php file:

    <h1><?php wp_title(”); ?></h1>
    <div class=”description”>
    <?php if (!is_category()) bloginfo(‘description’); ?></div>

    The main blog tagline displays on every page. I’d like the category description to display when I’m on a post in a certain category. What is the best way to do this?

    Thanks,
    Brett

Viewing 6 replies - 1 through 6 (of 6 total)
  • This will display the category description for each post within that category while displaying the blog description outside of the category (notice I only have two pages):

    <?php
    if( ($page_id != 8) && ($page_id != 2))
    {
    $category = get_the_category();
    echo $category[0]->category_description;
    }
    else
    {bloginfo(‘description’);}
    ?>

    page_id = 2 is the About page. However, it doesn’t return an ID when I’m on the about page. So the default category description displays instead. Any suggestions as to why the About page is not returning a page_id?

    Thanks,
    Brett

    While Pages don’t have categories in a visible, practical way to group them (as in the case of posts) – for internal technical reasons [so we were told!] they always belong to the “default” category of your blog. More exactly, to the whatever was the default category in the moment when the Page has been created.

    Yes – I understand that pages belong to the default category but why doesn’t the About page have a page_id?

    Well, the About Page does have an id – you just said above, it’s = 8.
    What I don’t understand: why are expecting that the code you posted would return a Page’s ID? I am not a coder, but there is nothing in that code to display any kind of page ID…

    You’re right. I added this:

    global $wp_query;
    $page_id = $wp_query->post->ID;

    But then modified the code to use this:

    if( !is_page() )
    {
    $category = get_the_category();
    echo $category[0]->category_description;
    }
    else
    {bloginfo(‘description’);}

    I’m looking for the same thing. Like the original question asker, I’m referring to posts, not pages:

    I’d like the category description to display when I’m on a post in a certain category. What is the best way to do this?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Displaying category description?’ is closed to new replies.
Skip to toolbar