如何在另一个帖子下添加2个帖子?格式应保持不变

时间:2018-09-20 作者:ordresser

如果一篇文章不超过200字,目标是在一篇文章下打印2篇文章。

我在WordPress得到了一些帮助,但是一些有帮助的人提供的代码我无法集成到一个无缝的整体中。

下面是基本代码,它可以工作,但不包括格式(它只是将整篇文章打印为一个段落):

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;
}
以下是打印文章格式的修改建议:

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);
但是,您将这些建议的修改放在原始代码中的什么位置以使其工作?

(原始代码归Pramod Jodhani所有,修改建议归Steven Stern所有。)

1 个回复
SO网友:filipecsweb

您的代码即将完成,但缺少一个关键点。下面是完成此任务所需的全部内容(放入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 );
注意代码,尤其是它的注释!我希望它对你有用。

结束

相关推荐

如何在用户单击按钮时使用AJAX调用PHP函数

我正在开发一个使用自定义模板/主题的wordpress网站,但我遇到了麻烦。我想使用Ajax在用户单击按钮时调用函数。在一个页面上,我有一个这样的按钮:<p class=\'form-submit\'> <input name=\'message_read\' type=\'submit\' class=\'submit button mark-as-read\' value= \'Mark as read\' /> </p>&#