我是 WordPress 开发的新手,所以我在显示特定类别的某些帖子时遇到了问题......
就像,我正在将一些图像添加(发布)到 post_picture 类别的数据库中......
现在在前端我有一个名为 Picture Mania 的页面,我只想显示我添加到 post_picture 类别中的那些图像...
为此,我首先安装了php-exec 插件,现在我尝试通过此代码在该页面上检索我想要的类别图像
<?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;
?>
Run Code Online (Sandbox Code Playgroud)
它工作正常,但显示所有类别图像,所以结论是我没有得到我想要的输出......
非常感谢您的回答......并提前感谢您的帮助
您可以传递类别 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;
?>
Run Code Online (Sandbox Code Playgroud)
或者您也可以在数组中使用类别名称:
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );
Run Code Online (Sandbox Code Playgroud)
query_post Reference: http://codex.wordpress.org/Function_Reference/query_posts
Although WP_Query is recommended approach instead of query_posts
| 归档时间: |
|
| 查看次数: |
9463 次 |
| 最近记录: |