0

I want to add a button on top of all posts have catogery "music", Here's what I have tried but it does not work at all. Please give me advice.

  function add_go_back_btn($content) {
       global $post;
       $cat = get_the_category();
       $post_type = get_post_type();
       if($cat === 'music' && $post_type === 'post') {
            return '<div><a href="/music">This is a button</a></div>' . $content;
       }
       else return $content;
   }

   add_filter( 'the_content', 'add_go_back_btn' );
1

Your code is fine, but your logic isn't working because of $cat. $cat is returning an Array with an Object which you're trying to check against. But you can easily just use in_category('music') like this:

function add_go_back_btn($content) {
 global $post;
 $cat = get_the_category();
 $post_type = get_post_type();

 if(in_category('music') && $post_type === 'post') {
    return '<div><a href="/music">This is a button</a></div>' . $content;
 } else return $content;
}
add_filter( 'the_content', 'add_go_back_btn' );
0

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.