0

The goal is to print 2 articles under an article IF the article is 200 words or less.

I've had some help over at WordPress, but the code a couple of helpful people provided I can't integrate into a seamless whole.

Here is the basic code, which works but doesn't include formatting (it just prints whole articles as a single paragraph):

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>
                    ";
                }

                wp_reset_query();
            }
        }

    }

    return $content;
}

function word_count($content) {
    $word_count = str_word_count( strip_tags( $content ) );
    return $word_count;
}

Here is the modification suggestion to print the formatting for the articles:

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);

Where do you put these suggested modifications in the original code to make it work, though?

(CREDITS go to Pramod Jodhani for the original code and Steven Stern for the modification suggestion.)

0

Your code is almost done and it's missing a key point. Here's the whole one you need to get it done (put in functions.php):

function add_2_articles_if_less_than_200( $content ) {
    global $post;

    if ( $post->post_type == 'post' ) {
        // Use $post->post_content here as its content is raw - not formatted with HTML.
        $count = mb_strlen( $post->post_content, 'UTF-8' );

        // If $count is less then 200 then add two more articles from the same category.
        if ( $count < 20000 ) {
            $current_post_id = $post->ID;

            $current_post_categories = wp_get_post_categories( $current_post_id );

            if ( ! empty( $current_post_categories[0] ) ) {
                $qry = new WP_Query(
                    array(
                        'cat'            => $current_post_categories[0],
                        'posts_per_page' => 2,
                        'post__not_in'   => array( $current_post_id ),
                    )
                );

                if ( $qry->have_posts() ) {
                    while ( $qry->have_posts() ) : $qry->the_post();
                        // Remove this current function from tag `the_content` to avoid infinite loop. This is the key to make it work!
                        remove_filter( 'the_content', 'add_2_articles_if_less_than_200', 20 );

                        $filtered_post_content = apply_filters( 'the_content', $qry->post->post_content );

                        // Add filter again, otherwise this whole function won't work.
                        add_filter( 'the_content', 'add_2_articles_if_less_than_200', 20 );

                        $content .= "
<div class='additional_post'>
    <h3>" . $qry->post->post_title . "</h3>
    <div class='additional_post_content'>$filtered_post_content</div>
</div>";
                    endwhile;
                }

                // Make sure you reset the query as your page does not end here.
                wp_reset_query();
            }
        }
    }

    return $content;
}

// Use priority 20 to make sure your function will run after WordPress default filter to avoid issues.
add_filter( 'the_content', 'add_2_articles_if_less_than_200', 20 );

Pay attention to the code, especially its comments! I hope it works for you.

  • Thanks, but unfortunately it doesn't work. Nothing is printed when I replace the old code with this one – ordresser Sep 21 '18 at 18:37
  • Odd. I tested the code myself and it's working fine. – filipecsweb Sep 23 '18 at 16:24
  • What theme are you using? I tried your code on a second domain, with a different theme, and it doesn't do anything there either (although the first code works on both sites, just that it doesn't format the content) – ordresser Sep 24 '18 at 22:38
  • Mine it's a fresh WP installation with Storefront theme. Is your code written in functions.php? – filipecsweb Sep 25 '18 at 3:39
  • In functions, yes. That's where I added it. – ordresser Sep 25 '18 at 18:09

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.