WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Conditional tags: need parent category to reach "children" (7 posts)

  1. David Borrink
    Member
    Posted 4 years ago #

    I want to have two different methods of displaying single posts:

    1. H1 headers, and with no meta tags and comments
    (this will be a specific group of child categories under one parent category)
    2. H2 headers, and with meta tags and comments
    (this will be every other post)

    Here's my conditional tag code for the headers portion:

    <?php if(in_category('55')) : ?>
    	<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
                <h1><?php the_title(); ?></h1>
    
       <?php else :?>
    	<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
                <h2><?php the_title(); ?></h2>
    
       <?php endif;?>

    I'm testing a single page using a post under category 55. Category 55 is a second-child level category, two levels under a parent category of 25. I try 25 and it uses the h2 header style. I try the second level category of 16 and still get the h2 header style. But I try 55 and I get the h1 style.

    Any tips on how I can get the top level parent of 25 to be used so I can cover all the child levels below with one category id?

  2. Michael Fields
    Themer
    Posted 4 years ago #

    This function maybe of help to you:

    if( post_is_in_descendant_category( array( 55,25,16 ) ) ) {
    	/* Do Stuff Here... */
    }
    function post_is_in_descendant_category( $cats, $post_id = null ) {
    	if( is_array( $cats ) ) {
    		foreach ( ( array ) $cats as $cat ) {
    			$descendants = get_term_children( ( int ) $cat, 'category' );
    			if ( $descendants && in_category( $descendants, $post_id ) )
    				return true;
    		}
    	}
    	return false;
    }

    I'm not sure that I completely understand what you need to do, but this function accepts and array of category_id's as it's first argument and will return true if the current post is in any of these categories OR any of the categories' descendants.

    Hope this helps,
    -Mike

  3. David Borrink
    Member
    Posted 4 years ago #

    Mike,

    Thanks for taking the time to write up a code snippet example. It would work great if I was interested in one set of descendants, but it's bigger than that.

    To further explain my situation: category 25 is the parent category to probably 7 child categories which each have 2 to 6 child categories.

    So I want to have only category 25 listed to work with with the conditional tag so that if affects all its "decendants". Right now I'm only able to make it work by putting in a category id for a second-level child, and while it can be done, I don't what to have to enter all the ID numbers for the second level.

    We plan on adding more of these second level items and it would be nice to have my client not have to wait for me to re-write the template each time they want to add a new category.

    Do that explain the situation better?

  4. David Borrink
    Member
    Posted 4 years ago #

    Mike, I'm wondering if your code goes completely in the functions.php file, or if the top part goes in the single.php while the "function" goes in function.php

  5. Michael Fields
    Themer
    Posted 4 years ago #

    Have you tried the function? I think that it will work for this situation. Try this and let me know if it works. I'm currently using this function in a project and it works beautifully for a similar situation.

    if( post_is_in_descendant_category( array( 25 ) ) ) {
    	/* Do Stuff Here... */
    }

    Mike, I'm wondering if your code goes completely in the functions.php file, or if the top part goes in the single.php while the "function" goes in function.php

    Yep, you got it!

  6. David Borrink
    Member
    Posted 4 years ago #

    Hey, it worked!! NICE! I tested it on blog posts and the catalog posts, and the catalog posts came up with H1 headers, while the blog posts came up with H2. Perfect!

    Here's my actual code now:

    <?php /* function to note catalog posts*/ if( post_is_in_descendant_category( array( 25 ) ) ) { ?>
            <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
            <h1><?php the_title(); ?></h1> 
    
            <?php } else { ?>
            <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
            <h2><?php the_title(); ?></h2>
    
             <?php } ?>

    Thanks!

  7. Michael Fields
    Themer
    Posted 4 years ago #

    David, glad it worked for you!

Topic Closed

This topic has been closed to new replies.

About this Topic