Forums

[resolved] Using is category to display specific content (8 posts)

  1. frescova
    Member
    Posted 2 years ago #

    I want to display a specific block of html when viewing an archive page based on on category.

    I have a category "news (id 1)" with 5 children categories;

    If I use (is_category ('1')) the html only appears when the news category is being browsed, but if I go to any of the children categories the html does not display;

    I know I can use an array, is_category(array(1,'news-cat-1','news-cat-2')); but then I have to manually edit the code every time I add a child category to news.

    Is there a way to make it so that when the user is viewing the news category or any of it's children the html display?

    thanks in advance for any guidance..

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    Maybe this will help:

    <?php
    $current_cat = intval( get_query_var('cat') );
    $news = get_cat_ID('news');
    $taxonomy = 'category';
    $children = get_term_children( $news, $taxonomy );
    if (in_array($current_cat, $children)) {
      echo 'current category is a child of the news category';
    }
    ?>
  3. frescova
    Member
    Posted 2 years ago #

    Thanks,
    I'll give it a shot...

  4. frescova
    Member
    Posted 2 years ago #

    OK - that sort of worked...

    It displays the html I want for all children categories, but now it does not display it for the parent category...

    Any ideas

    Thanks again for your help

  5. MichaelH
    Volunteer
    Posted 2 years ago #

    Sorry, wouldn't you need something like:

    if (in_array($current_cat, $children) && is_category ('1')) {
  6. frescova
    Member
    Posted 2 years ago #

    nope - now it does not bring back anything at all...

    Here's what I have:

    <?php
    $current_cat = intval( get_query_var('cat') );
    $news = get_cat_ID('Press Room');
    $taxonomy = 'category';
    $children = get_term_children( $news, $taxonomy );
    if (in_array($current_cat, $children) && is_category('428')) {
    ?>

    428 is the "Press Room" Category ID - I have - Press room has the child category "Press Releases" and Press Realeses has children categories of 2010, 2009, etc...

    Thanks again...

  7. MichaelH
    Volunteer
    Posted 2 years ago #

    Darn---not && that means AND

    so
    &&
    should be
    ||

  8. frescova
    Member
    Posted 2 years ago #

    Sweet :-)
    Thanks for your help Michael!

Topic Closed

This topic has been closed to new replies.

About this Topic