functions – Output a specific link in WordPress post if the single post’s category’s name contain certain word

Question

I want to output a specific link in every post, depending on the post’s category. For example, If a post is in “travel” category, then the link to https://www.example.com/travelbooking will be displayed in this post. If a post is in “hotel” category, then the link to https://www.example.com/hotelbooking will be displayed in this post.

I tried this code

    <?php 
  $category = get_the_category();
  $firstCategory = '$category[0]->cat_name';
  if (strpos($firstCategory, 'travel') !== false) {
echo '<a href="https://www.example.com/travelbooking">Visit travelbooking</a>';
}

      if (strpos($firstCategory, 'hotel') !== false)  {
    echo '<a href="https://www.example.com/hotelbooking">Visit hotelbooking</a>';   
}  

      else {
     echo '<a href="https://www.example.com">Visit homepage</a>';
}
      ?> 

But it doesn’t work, any suggestion?

0
Stian 1 month 2021-08-14T22:11:34-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse