WordPress.org

Ready to get started?Download WordPress

Forums

Displaying category description? (7 posts)

  1. brettr
    Member
    Posted 7 years ago #

    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

  2. brettr
    Member
    Posted 7 years ago #

    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

  3. moshu
    Member
    Posted 7 years ago #

    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.

  4. brettr
    Member
    Posted 7 years ago #

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

  5. moshu
    Member
    Posted 7 years ago #

    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...

  6. brettr
    Member
    Posted 7 years ago #

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

  7. drew3000
    Member
    Posted 7 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.