本文介绍了WordPress热门帖子,无需使用插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个网站上尝试获取受欢迎的帖子部分.我尝试过插件,但它们需要wp_head(),这会破坏我在网站上拥有的jCarousel.我已经在functions.php中使用以下代码实现了getPostViews的代码:

I'm working on a site trying to get a popular posts section on it. I've tried plugins but they require wp_head() and that destroys the jCarousel that I have on the site. I've implemented the code to getPostViews with the following in functions.php:

function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}

function setPostViews($postID) {
    $count_key = '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);
    }
}

但是,当我在循环中插入以下内容进行显示时,我的网站崩溃了,告诉我需要再次安装wordpress.当我完成安装时,我得到了一个数据库错误列表.但是,如果我在当天晚些时候重新删除代码后又重新访问该网站,则该网站将再次正常运行.

However when I insert the following in a loop to display it, my site goes down telling me that I need to install wordpress again. When I go through with the installation, I get a list of database errors. However, if I go back to it some time later in the day with the code removed, the site works again.

<? query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC'); ?>

如何使用此功能使热门帖子起作用?还是我应该寻找另一种方法来获得受欢迎的帖子上班?感谢您的帮助.

How can I get the popular posts to work using this? Or is there another way I should be looking at to get popular posts to work? Thanks for the help.

推荐答案

将此添加到functions.php中:

Pleace this in functions.php:

function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}
function setPostViews($postID) {
    $count_key = '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);
    }
}

接下来,将其放置在您要返回带有视图的帖子列表的位置:

Next, place this where you'd like to return the post list w/views:

<ul>
    <?php
    global $post;
    $args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :  setup_postdata($post); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <li><?php setPostViews(get_the_ID()); echo getPostViews(get_the_ID());; ?></li>
    <?php endforeach; ?>
</ul>

这篇关于WordPress热门帖子,无需使用插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:24