2

I need to use an if statement to detect if the category page that is currently being viewed is from a category list being lifted from wordpress. I cant use a static list as this list is likely to change as the admin changes things.

<?php 
     $args = array(
         'orderby' => 'name',
         'parent' => 8,
         'taxonomy' => 'category'
     );
     $categories = get_categories( $args );
     foreach ( $categories as $category1 ) { ?>

     <?php if (is_category(<?php echo get_cat_name("$category1->term_id;"); ?>)) : ?>
         <p>This is the text to describe category A</p>
     <?php else : ?>
         <p>This is the text to describe category B</p>
     <?php endif; ?>  
}?>

This is what I have so far, it works if I use a static list but when I try and get the is category to work with the get_cat_name it gives me an error 500.

Thanks.

1 Answer 1

3

You are getting a 500 error because of this line:

<?php if (is_category(<?php echo get_cat_name("$category1->term_id;"); ?>)) : ?>

It should simply be:

<?php if (is_category(get_cat_name($category1->term_id))) : ?>

echo is used to send a string to the output buffer (in other words, to output the text to the browser). You don't need to echo values into another function. Additionally, when you pass a value to a function, you can send the variable ($category1->term_id) directly.

You also don't need to open a <?php tag when you haven't closed the previous tag.

Sign up to request clarification or add additional context in comments.

3 Comments

also, you don't need quotes and semicolon here "$category1->term_id;". instead use $category1->term_id
@SamvelAleqsanyan Good catch; updating the answer.
Thanks, that fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.