A
A
Alexander Sobolev2021-07-21 11:51:01
WordPress
Alexander Sobolev, 2021-07-21 11:51:01

How to replace content depending on Wordpress category?

Hello!

function routeCard_shortcode($atts) {
    extract(shortcode_atts(array( 'post_id' => '' ), $atts)); $output="";
    if ( empty($post_id) ) {  $output.= "(_*_)";  } else {  $output.= "Post Id: ".$post_id;  }	
    return $output;
    }
    add_shortcode('routeCard', 'routeCard_shortcode');
 
    function routeCard_content( $data ){
        $cat = get_category_by_slug( 'routes' ); $id = $cat->term_id;
        if (in_array($id, $data['post_category'])) { $data['post_content'] = "[routeCard]";}
        return $data;
    }
    add_filter( 'wp_insert_post_data' , 'routeCard_content' , 99 );

At what point and through which hooks is it better to set/replace the default content of a post and determine if a post belongs to a certain category?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Zolin, 2021-07-21
@artzolin

The_content hook is suitable for you, you can also check if the post belongs to a certain category

add_filter( 'the_content', 'my_content' );     
function my_content( $content ) {
  
  // проверяем, что у записи нет контента
  if ( strlen($content) == 0 ) {
    return 'new content';
  }

  return $content;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question