WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Show parent of category in archive (4 posts)

  1. katootje
    Member
    Posted 6 years ago #

    Hi,

    I like to show the parent categoryname on the archive page of a child category. I think i have to add something to this line:
    <h2 class="pagetitle">Archive for the ‘<?php single_cat_title(); ?>’ Category</h2>

    I just want to change it in:
    <h2 class="pagetitle"><?php here the code for the parent /> - <?php single_cat_title(); ?></h2>

    I've look everywhere but can't find what to put there.
    Anyone??

  2. Kafkaesqui

    Posted 6 years ago #

    You can do this by collecting the category parent ID from the query object and using get_the_category_by_ID() to display it:

    <?php
    global $wp_query;
    $category = $wp_query->get_queried_object();
    ?>
    
    <h2 class="pagetitle"><?php echo get_the_category_by_ID($category->parent); ?> - <?php single_cat_title(); ?></h2>
    
    To test for it, change your <code><h2></code> to:
    
    <h2 class="pagetitle"><?php if( $category->parent ) { echo get_the_category_by_ID($category->parent) . ' - '; } single_cat_title(); ?></h2>
  3. katootje
    Member
    Posted 6 years ago #

    Thnxx, the first line is working perfect!

  4. Roberto Simões
    Member
    Posted 6 years ago #

    How can I put the categories links?

    thanks

Topic Closed

This topic has been closed to new replies.

About this Topic