Helper function to get popular post ID’s

[ad_1]

On the question: Helper function to get popular post ID’s | WordPress.org, the solution:

关于问题:Helper function to get popular post ID’s | WordPress.org 的补充,解决办法是这样:

/** 获取插件 wordpress-popular-posts(热门文章)中的文章ID */
If (in_array( 'wordpress-popular-posts/wordpress-popular-posts.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    function wzm_popular_posts_ids(): array
    {
        $post_IDs = array();
        $query = new Query([
            'range' => 'last30days',
            'limit' => '10',
            'order_by' => 'views',
        ]);
        $popular_posts = $query->get_posts();
        if ( is_array($popular_posts) && ! empty($popular_posts) ) {
            foreach($popular_posts as $popular_post) {
                $post_IDs[] = $popular_post->id;
            }
        }
        return $post_IDs;
    }
}

<?php
if (function_exists('get_popular_posts_ids()')) {
    $posts_ids = get_popular_posts_ids();
    ?>
    <section class="popular-articles">
        <div class="container">
            <header class="popular-header">
                <h2 class="title">本月热门文章</h2>
            </header>
            <div class="popular-content">
                <?php
                $args = array(
                    'post__in' => $posts_ids,
                    'no_found_rows' => true,
                );
                $popular_query = new WP_Query($args);
                if ($popular_query->have_posts()) :
                    while ($popular_query->have_posts()) : $popular_query->the_post();
                        ?>
                        <article class="article-card post-popular">
                            <div class="container">
                                <div class="card-thumbnail">
                                    <a href="https://projectdmc.org/support/topic/helper-function-to-get-popular-post-ids-2/<?php the_permalink(); ?>">
                                        <?php wzm_post_thumbnail(); ?>
                                        <div class="screen-reader-text"><?php the_title(); ?></div>
                                    </a>
                                </div>
                                <div class="card-text">
                                    <header class="header">
                                        <h3 class="title"><a href="https://projectdmc.org/support/topic/helper-function-to-get-popular-post-ids-2/<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                    </header>
                                    <footer class="footer">
                                        <div><?php the_author_link(); ?></div>
                                        <div class="time"><?php wzm_post_datetime(); ?></div>
                                    </footer>
                                </div>
                            </div>
                        </article>
                    <?php
                    endwhile;
                    wp_reset_postdata();
                endif;
                ?>
            </div>
        </div>
    </section>
    <?php
} else {
    echo '<p>The "WordPress Popular Posts" plugin is not active.</p>';
}

参考链接:

插件 – is_plugin_active函数不存在 – WordPress Development Stack Exchange

在functions.php中使用is_plugin_active – WordPress Development Stack Exchange

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer