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" />
  • It's outputting the custom_link or the category_link? I assume this is inside a foreach or similar?
    – Erbilacx
    Jul 10 '17 at 11:40
  • Sorry. Yes this is within a foreach and it's outputting the $custom_link Jul 10 '17 at 11:47

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)).'">
  • No I did not and I will thank you for this good sir!!! Jul 10 '17 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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