0

This part of my script runs WordPress categories and displays them in a random order.

The problem I'm having is that the url is based upon the categories ID There's one category that should go to a different page if clicked in stead of the categories page.

So I created a variable and kicked in the url. Created a if-statement to see when the $cat_id is loaded and then change it.

But it only outputs the url multiple times (every found category)

What can it be?

$category_link = get_category_link( $cat_id );
$custom_link = get_option( 'home' ).'/sub/sub-sub';
echo '
    <div class="carousel_items">
        <div class="category_image">
            <a href="'.($cat_id == 5) ? esc_url($custom_link) : esc_url( $category_link ).'">
                <img src="'.get_bloginfo('template_directory').'/images/svg/'.$cat_id.'.svg" />
            </a>
        </div>
   </div>';
2
  • It's outputting the custom_link or the category_link? I assume this is inside a foreach or similar?
    – Erbilacx
    Commented Jul 10, 2017 at 11:40
  • Sorry. Yes this is within a foreach and it's outputting the $custom_link Commented Jul 10, 2017 at 11:47

1 Answer 1

1

As you are concatenating the result to a href you must wrap the ifelse statement inside another set of parentheses so the result is calculated first and then returned:

<a href="'.(($cat_id == 5) ? esc_url($custom_link) : esc_url($category_link)).'">
1
  • No I did not and I will thank you for this good sir!!! Commented Jul 10, 2017 at 12:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.