• 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>
    					";
    				}
    
    • This topic was modified 6 years, 2 months ago by ordresser.
    • This topic was modified 6 years, 2 months ago by ordresser.
    • This topic was modified 6 years, 2 months ago by Jan Dembowski.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    apply_filters( 'the_content', get_the_content() ) will run the post content through the normal filter used by the_content.

    Thread Starter ordresser

    (@ordresser)

    I tried placing your code many ways in the larger piece, but I couldn’t get it to work. Where exactly is it supposed to go, please?

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    where you have

    <div class='additional_post_content'>".get_the_content()."</div>

    replace with

    <div class='additional_post_content'>". $fmt_content ."</div>

    and put this above it someplace

    $unfmt_content = get_the_content();
    $fmt_content = apply_filters('the_content', $unfmt_content);

    Google “apply_filters the_content” for examples.

    Thread Starter ordresser

    (@ordresser)

    I tried putting your code “someplace” (tried putting it everywhere in the code) and it doesn’t seem to make anything happen. Where exactly does it go?

    I also websearched your suggested querry, but the results are a lot of people with similar (but not relevant to the larger piece of code I’m trying to use in this case, a function that constructs two more articles based on ifs) questions about how to use the function.

    • This reply was modified 6 years, 2 months ago by ordresser.
    • This reply was modified 6 years, 2 months ago by ordresser.
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Inside your function, replacing appropriate lines.

    Thread Starter ordresser

    (@ordresser)

    Which lines are the appropriate lines?

    Thread Starter ordresser

    (@ordresser)

    As far as I can tell this doesn’t work, @sterndata, unfortunately. I have tried it every which way I can think of.

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.