• On the question: Helper function to get popular post ID’s | www.remarpro.com, the solution:

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

    /** 获取插件 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="<?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="<?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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @anno1800,

    Please replace:

    $query = new Query(...);

    with:

    $query = new \WordPressPopularPosts\Query(...);

    Also replace:

    if (in_array( 'wordpress-popular-posts/wordpress-popular-posts.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {

    with:

    if ( function_exists( 'wpp_get_views' ) ) {
    Thread Starter 响当当是我大姐头

    (@anno1800)

    Thank you for your reply, I didn’t turn around for a while, I just thought about checking if the plugin was activated, but I didn’t expect to indirectly check whether the plugin is activated by judging whether the unique features of the plugin are defined or not. (^^)
    (Regarding the wp_query mentioned earlier, it was correct at first, but then I used phpstrom auto-change to get rid of it, and I didn’t check it.) )<font _mstmutation=”1″></font>

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Helper function to get popular post ID’s’ is closed to new replies.