仅显示特定类别的帖子

时间:2013-09-23 10:58:32

标签: php wordpress

我是wordpress开发的新手,所以我遇到了显示特定类别帖子的问题......

就像,我正在为post_picture类别添加(发布)一些图像到数据库......

现在在前端我有一个页面名称Picture Mania,我只想显示我添加到post_picture类别中的那些图像......

为此目的首先我安装了php-exec plugin,现在我试图通过此代码在该页面上检索我的欲望类别图像

<?php query_posts('post_picture =post_picture&showposts=5');
while (have_posts()) : the_post();
  // do whatever you want
?>
<?php get_template_part( 'content', get_post_format() ); ?>
                <?php cup_post_nav(); ?>
                <?php comments_template(); ?>
<b><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
?>

它工作正常,但显示所有类别的图像,所以结论我没有得到我的愿望输出......

将非常感谢答案....并提前感谢您的帮助

3 个答案:

答案 0 :(得分:2)

您可以传递类别ID (cat = 3),例如

<?php query_posts('cat=3&showposts=5');
while (have_posts()) : the_post();
  // do whatever you want
?>
<?php get_template_part( 'content', get_post_format() ); ?>
                <?php cup_post_nav(); ?>
                <?php comments_template(); ?>
<b><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
?>

或者您也可以在文章中使用类别名称

query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );

query_post参考:http://codex.wordpress.org/Function_Reference/query_posts

虽然建议使用WP_Query而不是query_posts

答案 1 :(得分:1)

要显示特定类别的帖子,您可以在cat=YOUR_CATEGORY_ID中添加query_posts(),例如:

query_posts('post_picture=post_picture&showposts=5&cat=$your_Category_Id');

,如query_posts()示例

答案 2 :(得分:1)

试试这个:

<?php query_posts('cat=your-post_picture-category-id&showposts=5');

while (have_posts()) : the_post();
// do whatever you want
?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php cup_post_nav(); ?>
<?php comments_template(); ?>
<b><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"> 
<?php the_title(); ?></a>