Can I pass a variable within a conditional statement on archive?
-
I want to say if is subcategory of category X, display posts like this
I can’t find how to do this anywhere, so I’m trying to write my own variable to use…
Here’s the function:/** * https://alex.leonard.ie/2011/04/20/wordpress-get-id-of-top-level-parent-category/ * Returns ID of top-level parent category, or current category if you are viewing a top-level * * @param string $catid Category ID to be checked * @return string $catParent ID of top-level parent category */ function pa_category_top_parent_id ($catid) { while ($catid) { $cat = get_category($catid); // get the object for the catid $catid = $cat->category_parent; // assign parent ID (if exists) to $catid // the while loop will continue whilst there is a $catid // when there is no longer a parent $catid will be NULL so we can assign our $catParent $catParent = $cat->cat_ID; } return $catParent; }
Here’s how I’m using it to return the ID of the current category IF it’s parent category is 33
<?php $catid = get_query_var('cat'); $parent = pa_category_top_parent_id ($catid); if( $parent == '33' ) { $subcatid = get_query_var('cat'); }?>
I can get it to echo on the page (
<strong><?php echo $subcatid ; ?></strong>
), but it won’t work inside my conditional statement to return true/display what I want on archive.php:
if (is_category('16') || in_category('16') || is_category('33') || is_category( $subcatid ) ) { }
Can you even do this? Please help!!!
- The topic ‘Can I pass a variable within a conditional statement on archive?’ is closed to new replies.