如何在 WordPress 中获取某些帖子
我想实现一个功能来获取 WordPress 中的某些帖子。
INPUT
page_number
Category_name
VARIABLE
items_per_page = 10
OUTPUT
posts 数组,类似于 query_posts() 函数的结果。
这是我的代码:
$page_number = $_GET["page_number"]
$category_name = $_GET["category_name"]
function app_get_posts($page_number, $category,$items_per_page = 10)
{
global $wpdb;
$select ="SELECT POSTS FROM wp_posts WHERE CATEGORY = ".$category." LIMIT (".$page_number." - 1) * ".$items_per_page.",".$page_number." * ".$items_per_page; //it didn't work.
return $wpdb->query($select);
}
当我调用函数app_get_posts('2','tech')时,它将返回“tech”类别中的第10~19篇帖子,当我调用app_get_posts('3','wordpress')时,它将返回“wordpress”类别中的第 20~29 个帖子。
所以我想知道是否有办法解决这个问题。 提前谢谢。
I want to implement a function to get certain posts in wordpress.
INPUT
page_number
category_name
VARIABLE
items_per_page = 10
OUTPUT
posts array, like the result of the query_posts() function.
Here is my code:
$page_number = $_GET["page_number"]
$category_name = $_GET["category_name"]
function app_get_posts($page_number, $category,$items_per_page = 10)
{
global $wpdb;
$select ="SELECT POSTS FROM wp_posts WHERE CATEGORY = ".$category." LIMIT (".$page_number." - 1) * ".$items_per_page.",".$page_number." * ".$items_per_page; //it didn't work.
return $wpdb->query($select);
}
When I call the function app_get_posts('2','tech'), it will return the 10th~19th posts in the "tech" category, when I call app_get_posts('3','wordpress'), it will return the 20th~29th posts in the "wordpress" category.
So I am wondering if there is a way to figure out this problem.
Thx in advence.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜上面的方法不起作用的原因是
wp_posts
表没有CATEGORY
或POSTS
字段。我修改了您的代码,以便您可以根据分类
返回帖子:I guess the reason why the above won't work is because the
wp_posts
table not has aCATEGORY
orPOSTS
field. I modified you code so you can return posts based ontaxonomy
: