How can you print additional posts under a post, but keep their formatting?
-
The goal is to check if the article has 200 words. If it does, do nothing extra. If it does not, print the 2 most recent articles from the same category underneath the article (and if possible do not print the same article if it happens to be one of the 2 most recent).
Pramod Jodhani @promz offered this code, which works as far as checking for 200 words and printing more articles, but when it prints the articles, they come out as one big paragraph with no formatting – the paragraphs aren’t separated, the title’s aren’t linked, there’s no featured image, etc.
How can I keep the formatting when I print the additional articles?
add_filter("the_content" , "add_2_articles_if_less_than_200"); function add_2_articles_if_less_than_200($content) { global $post; if($post->post_type == "post") { $count = word_count($content); if($count < 200) { //if count is less then 200 then add two more articles from the same category $current_post_id = $post->ID; $categries = wp_get_post_categories($current_post_id); if(isset($categries[0])) { $cat = $categries[0]; $args = array( "cat" => $cat, "posts_per_page" => 2, "post__not_in" => array($current_post_id) ); $qry = new WP_Query($args); while($qry->have_posts()) { $qry->the_post(); $content .=" <div class='additional_post'> <h3>".get_the_title()."</h3> <div class='additional_post_content'>".get_the_content()."</div> </div> "; }
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘How can you print additional posts under a post, but keep their formatting?’ is closed to new replies.