如何在自定义帖子类型中显示最多查看的帖子
我使用WordPress的keremiya主题。如果“most_viewed”选项打开,我试图在我的自定义文章类型中显示我查看次数最多的文章。我的自定义帖子类型的名称是watch。我怎样才能用我现在的代码做到这一点?我也使用名为wp-post views的插件来显示我侧边栏中的视图。这是我的查询。如何在自定义帖子类型中显示最多查看的帖子
<?php if(get_option('most_viewed') == 'On'): ?>
<div class="sidebar-right">
<h2><?php echo get_option('my_title'); ?></h2>
<div class="fimanaortala">
<?php $tavsayi = get_option('keremiya_tavsiyesayi'); $tavkat = get_option('keremiya_tavsiyekat');?>
<?php query_posts('showposts='.$tavsayi.'&v_orderby=desc&cat='.$tavkat.'') ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="filmana">
<div class="filmsol">
<?php keremiya_resim('80px', '70px', 'izlenen-resim'); ?>
</div>
<div class="filmsag">
<div class="filmsagbaslik">
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
</div>
<div class="filmsagicerik">
<?php if(function_exists('the_views')) { the_views(); echo " "; } ?>
<p><?php nezaman_yazildi(); ?></p>
</div>
<div class="filmizleme">
<a href="<?php the_permalink() ?>"><img src="<?php bloginfo('template_directory'); ?>/images/filmizle.png" alt="film izle" height="21" width="61" /></a>
</div>
</div>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
您的解决方案(或尝试,至少)是基于名为WP-PostViews的插件,我不知道该插件。所以,我不能真的帮你。但是,我可以帮助您解决它,而无需使用此插件或任何其他插件。所以在这里我们去:
WordPress的有一些东西叫metadata。从这个非常自己的链接:
元数据API是一种简单和标准化的方式来检索和操作各种WordPress对象类型的元数据。对象的元数据由简单的键值对表示。对象可能包含多个元数据条目,这些元数据条目共享相同的密钥,并且其值不同。
这意味着您可以为您的自定义帖子类型创建元数据,其中包含已被查看的次数。为此,我们创建一个函数:
<?php
function set_views($post_ID) {
$key = 'views';
$count = get_post_meta($post_ID, $key, true); //retrieves the count
if($count == ''){ //check if the post has ever been seen
//set count to 0
$count = 0;
//just in case
delete_post_meta($post_ID, $key);
//set number of views to zero
add_post_meta($post_ID, $key, '0');
} else{ //increment number of views
$count++;
update_post_meta($post_ID, $key, $count);
}
}
//keeps the count accurate by removing prefetching
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
?>
这将根据帖子ID在每次查看帖子时增加一个计数器。当然,我们必须在代码中的某个地方调用这个函数,以便它真正运行。你可以通过两种方式来做到这一点:你可以在单个模板上调用这个函数(我相信它叫做single-watch.php),或者你可以添加一个简单的跟踪器。我赞成第二种选择,因为它可以让您的单一后循环更清洁。您可以通过这种方式实现这种跟踪:
<?php
function track_custom_post_watch ($post_ID) {
//you can use is_single here, to track all your posts. Here, we're traking custom post 'watch'
if (!is_singular('watch')) return;
if (empty ($post_ID)) {
//gets the global post
global $post;
//extracts the ID
$post_ID = $post->ID;
}
//calls our previously defined methos
set_views($post_ID);
}
//adds the tracker to wp_head.
add_action('wp_head', 'track_custom_post_watch');
?>
你走了。现在,WordPress检查用户是否正在访问对应于单个帖子的手表。如果是,则增加柜台。
现在剩下的唯一事情就是查询具有最多视图数量的帖子。这很容易使用WP_Query
来实现。当你创建你的循环,做这样的事情:
<?php
$query = new WP_Query(array(
'post_type' => 'watch', //your post type
'posts_per_page' => 1,
'meta_key' => 'views', //the metakey previously defined
'orderby' => 'meta_value_num',
'order' => 'DESC'
)
);
while ($query->have_posts()) {
$query->the_post();
//whatever code you want
}
?>
我一直在我的答案PHP,这样你就可以适应您的加价要求。我也假设你的帖子类型确实叫手表。我希望这一切都能帮助你。如果您想以稍微不同的方式查询您的帖子,我建议您阅读WP_Query文档。干杯。
当我试图将此添加到我的边栏我收到死亡的白色屏幕。哈哈:D我完全失去了。我很新的php –
我的意思是它没有工作不是死亡的白色屏幕。 –
你应该把第一个两个方法放到'functions.php'文件中。第三个是wp_loop的一个非常原始的版本,它应该可以在你的边栏中正常工作。只要不要忘记'wp_reset_query();'在完成循环 –