wp_query用于显示帖子必须查看和使用短代码

时间:2017-03-06 11:28:26

标签: php arrays wordpress function

我已尝试使用此功能显示最后的视图。但这是错误的。

我也将此用于其他事情,例如按类别显示帖子,在这种情况下效果很好。

所以也许我在new WP_query

的数组中有错误
function must_post_read()
{
    global $post;
    $html = "";

    $my_query_2 = new WP_Query(array(
        'meta_key' => 'post_views_count',
        'orderby' => 'meta_value_num',
        'posts_per_page' => 5
    ));

    if ($my_query_2->have_posts()) : 
        while ($my_query_2->have_posts()) : $my_query_2->the_post();

            $html .= "<p class=\"title\">" . get_the_title() . " </p>";
            $html .= "<p>" . get_the_excerpt() . "</p>";
            $html .= "<a href=\"" . get_permalink() . "\" class=\"readmore\">Read more</a>";

        endwhile;
        wp_reset_postdata();
    endif;
    return $html;
}

add_shortcode('must_post', 'must_post_read');

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

首先,我们需要确保帖子有一个自定义library(ggplot2) library(magrittr) mydata = data.frame(expand.grid(Tag = c('A','B','C'),Year = 2010:2011,PNo = paste0("X-",1:4)),Value = round(runif(24,1,20))) mydata$dist = ifelse(mydata$Tag == 'A',0,ifelse(mydata$Tag=='B',2,7)) mydata %>% ggplot(aes(x = dist,y = Value,fill = factor(Year))) +geom_bar(stat='summary',position = 'dodge',fun.y='mean',width = 1) + facet_wrap(~PNo,ncol=2) + theme(axis.text.x = element_blank(),axis.ticks.x = element_blank()) + geom_label(data = mydata %>% filter(PNo %in% c('X-3','X-4')),aes(x = dist,y=0,label = Tag),size=6,inherit.aes=F,color = 'red') 字段,您将在其中进行缩短。

post_meta

<强> USAGE
在PHP中

function whpp_track_post_views($post_id) {
    if (!is_single())
        return;
    if (empty($post_id)) {
        global $post;
        $post_id = $post->ID;
    }
    whpp_set_post_views($post_id);
}

add_action('wp_head', 'whpp_track_post_views');

function whpp_set_post_views($post_id) {
    $count_key = 'whpp_track_post_views';
    $count = get_post_meta($post_id, $count_key, TRUE);
    if ($count == '') {
        $count = 0;
        delete_post_meta($post_id, $count_key);
        add_post_meta($post_id, $count_key, '0');
    } else {
        $count++;
        update_post_meta($post_id, $count_key, $count);
    }
}

//To keep the count accurate, lets get rid of prefetching
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

//Short code function
function wh_must_post_read() {
    $html = '';

    $args = [
        'meta_key' => 'whpp_track_post_views', //<--  Check This
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'posts_per_page' => 5
    ];

    $my_query_2 = new WP_Query($args);

    if ($my_query_2->have_posts()) :
        while ($my_query_2->have_posts()) : $my_query_2->the_post();

            $html .= "<p class=\"title\">" . get_the_title() . " </p>";
            $html .= "<p>" . get_the_excerpt() . "</p>";
            $html .= "<a href=\"" . get_permalink() . "\" class=\"readmore\">Read more</a>";

        endwhile;
        wp_reset_postdata();
    endif;
    return $html;
}

add_shortcode('wh_must_post', 'wh_must_post_read');

在WP编辑器中

echo do_shortcode('[wh_must_post]');

请注意:要查看此代码的实际操作,请访问一些发布单页,以便[wh_must_post] mehord将whpp_set_post_views() meta_key添加到该帖子。

以上所有代码都在您的活动子主题(或主题)的function.php文件中。或者也可以在任何插件php文件中。
代码已经过测试并且有效。

相关文章:Display Popular Posts by Views in WordPress without a Plugin
相关问题:How to change the order of posts by number of views not by date in wordpress

希望这有帮助!