显示特定类别中的the_content()
问题描述:
我需要了解如何在单击特定类别链接时在页面/部分中列出我的内容数据。显示特定类别中的the_content()
几个小时后,我意识到从stackoverflow的帮助是我需要的!
功能提醒一点:http://www.hashbangcode.com/blog/wordpress-category-post-list-493.html
我需要从具体类别,并不是所有的类别与标题列出我的数据(标题,the_content等)。
答
虽然这个职位给你一个良好的开端,这里要说的是,我会用
// Set the desired category
$category = 1;
// Make query for posts in the category
$my_query = new WP_Query();
$my_query->query(
array(
'cat' => $category,
// Does not show sticky posts; use 'caller_get_posts' if using < WP 3.1
'ignore_sticky_posts' => 1
)
);
// Make sure some posts were found.
if($my_query->have_posts())
{
// Loop through each post found.
while($my_query->have_posts())
{
// Setup the post data to use
$my_query->the_post();
global $post;
// Echo out the title; Note that no formatting has been done
the_title();
the_content();
}
}
现在的代码,你也可以拿到冠军,或者:
$title = get_the_title($post->ID);
$title = $post->post_title;
此外,还可以获取帖子内容:
$content = $post->post_content;
此外,您可以使用这些参数中的任何一个来获取类别:
cat (int) - use category id.
category_name (string) - use category slug (NOT name).
category__and (array) - use category id.
category__in (array) - use category id.
category__not_in (array) - use category id.
更多关于WP_Query类可以在这里找到:http://codex.wordpress.org/Function_Reference/WP_Query
什么在$类别VAR写? :-s我需要自己动态地找到它: -/ – william 2011-04-23 22:39:06
不应该是一个问题。在什么情况下你会使用这段代码?类别ID很可能被找到,我只需要知道您何时可以执行代码来帮助您找出如何找到类别ID。 – tollmanz 2011-04-23 23:02:48
我在我的主题中使用category.php来显示数据。 Category.php代码与您的代码。 http://pastie.textmate.org/private/gd0umxohmpixugaf3mbpdw 而链接来自sidebar.php,看起来像: http://pastie.textmate.org/private/qvdwvoe5leb6j1hveimhxw – william 2011-04-24 11:12:45