繁体   English   中英

热门帖子未通过WP_query显示

[英]Popular Posts Not Showing Through WP_query

我想根据用户要求制作侧边栏,但在此侧边栏中,用户想要显示博客的热门帖子,并且我进行了更多搜索以显示它,但所有内容都是徒劳的。 :(这是我的代码!

<div class="ms_recent">
    <h5>Popular posts</h5>
    <?php
    $blog_papular = array(
        'posts_per_page'=>5,
        'meta_key' => 'wpb_post_views_count',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
    );

    $wp_query_papular=new WP_Query($blog_papular);

    while($wp_query_papular->have_posts()) {
        $wp_query_papular->the_post();
        ?>
        <div class="ms_articles">
            <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail('footer_page_image'); ?>
                <span><?php echo get_the_date('F j'); ?></span>
                <p><?php echo excerpt('8'); ?></p></a>
        </div>
        <?php
    }
    ?>
</div>

在这里有人知道我弄错了吗?

在越来越多地挖掘WordPress之后,我知道了我的错。 缺点是元密钥值未存储在数据库中,这就是为什么我的热门帖子未显示的原因。 我仅需执行以下几个步骤即可:

我们需要做的第一件事是创建一个函数,该函数将检测帖子查看次数并将其存储为每个帖子的自定义字段:

 function wpb_set_post_views($postID) {
$count_key = 'wpb_post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
    $count = 0;
    delete_post_meta($postID, $count_key);
    add_post_meta($postID, $count_key, '0');
}else{
    $count++;
    update_post_meta($postID, $count_key, $count);
}
}

remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

其次,将以下代码粘贴到我的单个发布循环中:

wpb_set_post_views(get_the_ID());

然后,我的博客循环将显示受欢迎的帖子:

 <?php
    $blog_papular = array(
        'posts_per_page'=>5,
        'meta_key' => 'wpb_post_views_count',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
    );
    $wp_query_papular=new WP_Query($blog_papular);
    while($wp_query_papular->have_posts())
    {
        $wp_query_papular->the_post();
        ?>
        <div class="ms_articles">
            <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail('footer_page_image'); ?>
                <span><?php echo get_the_date('F j'); ?></span>
                <p><?php echo excerpt('8'); ?></p></a>
        </div>
        <?php
    }
    ?>

享受! ;) 对不起,我的英语不好..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM