WordPress 在不使用 get_posts() 的情况下获取帖子数? [英] WordPress get count of posts without using get_posts()?

查看:21
本文介绍了WordPress 在不使用 get_posts() 的情况下获取帖子数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一个专门用于获取符合条件的帖子计数的函数调用.我相信 get_posts() 函数对于这个操作来说太昂贵了.我只是想决定在有预定义数量的帖子要显示时是否显示查看更多帖子"链接...

Need a function call that is designed solely to get the count of posts matching a criteria. I believe the get_posts() function is too expensive for this operation. I'm merely trying to decide whether or not to show a "View More Posts" link when there are a predefined number of posts to display...

例如,要显示的帖子链接的默认数量是 3.如果帖子总数超过 3,我只想显示查看更多帖子"链接.

For example, the default number of post links to display is 3. I only want to show a "View More Posts" link if the total number of posts exceeds 3.

这是我的代码...

$cat1=get_cat_ID('test');
$cat2=get_cat_ID('test2');
$myposts = get_posts(array('cat' => "$cat1,-$cat2",'showposts' => 3));
$myposts2 = get_posts(array('cat' => "$cat1,-$cat2"));
    $mypostcount = count($myposts2);
foreach($myposts as $idx=>$post)
?>
<li><a>Post Info Goes here</a></li>
<?php 
if ($mypostscount > 3){ ?>Show View All Link<?php ?>

推荐答案

你的问题不是很清楚,所以这里有两种方法.

You're question wasn't completely clear, so here's two methods.

首先,如果用户正在查看一个类别页面并且您想要显示此信息,您可以简单地使用以下内容:

First, if the user is viewing a category page and you want to display this information, you can simply use the following:

$myCount = $wp_query->found_posts;

这将返回为上次查询找到的帖子数.

That will return the number of posts found for the last query.

如果你想计算每个类别的帖子数量,比如主页,我会简单地通过 PHP/MySQL 进行处理.举个例子:

If you want to count the number of posts for each category, say for like a homepage I would go about it simply through PHP/MySQL. Here's an example:

SELECT COUNT( DISTINCT cat_posts.ID ) AS post_count 
FROM wp_term_taxonomy AS cat_term_taxonomy 
INNER JOIN wp_terms AS cat_terms ON cat_term_taxonomy.term_id = cat_terms.term_id 
INNER JOIN wp_term_relationships AS cat_term_relationships ON cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id 
INNER JOIN wp_posts AS cat_posts ON cat_term_relationships.object_id = cat_posts.ID 
WHERE cat_posts.post_status = 'publish' AND cat_posts.post_type = 'post' AND cat_term_taxonomy.taxonomy = 'category' AND cat_terms.term_id = '13'

我刚刚对其进行了测试,它运行正常.从查询中获得返回值后,只需简单地获取该行,然后执行以下操作:

I just tested it and it worked properly. Once you get the return from the query just simply grab the row and then do as follows:

echo $row['post_count'];

或者任何你喜欢的数据.更改类别所需要做的只是更改最后一个 WHERE 子句的 term_id

or whatever you like with the data. All you need to do to change categories is simply change the last WHERE clause's term_id

cat_terms.term_id = '13'

将 13 改为您要数的猫.

Change 13 to the cat you would like to count.

如果您想按类别名称进行操作,您可以更改最后一部分

If you would instead like to do it by category name you could change the last part from

cat_terms.term_id = '13'

cat_terms.slug IN ('cookies', 'uncategorized') or cat_terms.slug IN ('cookies')

第一个将从多个类别中进行选择,第二个将仅从一个类别中进行选择.希望这会有所帮助,

The first one will select from multiple categories, the second from only one. Hope this helps,

这篇关于WordPress 在不使用 get_posts() 的情况下获取帖子数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆