WordPress:仅根据分类显示帖子
我正在运行带有自定义分类法的 WordPress,通过我的主题中的functions.php 添加。我在主题的另一个模板中调用某种帖子类型的帖子,但希望通过仅显示这些帖子(如果它们的分类值等于调用它的当前帖子名称)来限制它显示的帖子。
例如:如果我在“红树”专辑页面上,并且我正在调用“歌曲”帖子类型下的所有帖子,那么它会调用每首歌曲。我有标记为“光盘”的自定义分类法,并且如果您位于红树页面上,我想限制为仅显示选择“红树”作为其分类值的帖子。
所以我只需要说:如果帖子的“光盘”分类值与当前页面名称相同,则继续,如果不是,则不显示任何内容。
这就是我调用“歌曲”帖子类型的帖子的方式:
<?php
$pages = get_posts('numberposts=9999&post_type=song&post_status=publish&order=ASC&orderby=date');
$i = 1;
foreach( $pages as $page ) {
$content = $page->post_title;
if( empty($content) ) continue;
$content = apply_filters('the_content', $content);
if ($i%2===0) { ?><tr class="gigpress-row gigpress-alt">
<?php } else { ?><tr class="gigpress-row"><?php } ?>
<td><?php echo $page->post_title ?></td>
<td><?php echo get_post_meta($page->ID, "p30-length", true); ?></td>
<td><a href="http://itunes.com/<?php echo get_post_meta($page->ID, "p30-itunes-song", true); ?>">BUY</a></td>
</tr>
<?php $i++;
} ?>
谢谢, 韦德
I am running WordPress with custom taxonomy, added through functions.php in my theme. I am calling posts of a certain post type in another template in my theme but want to restrict which ones it shows by only showing those posts if their taxonomy value equals the current post name that is calling it.
So for example: if I'm on the "Red Tree" album page and I'm calling all posts under the "songs" post type, it would call every song. I have custom taxonomy labeled as "disc" and I want to restrict to only show posts with "Red Tree" select as their taxonomy value if you are on the red tree page.
So I just need to say: if the post's "disc" taxonomy value is the same as the current page name, then continue, if not, don't display anything.
This is how I am calling posts of the "song" post type:
<?php
$pages = get_posts('numberposts=9999&post_type=song&post_status=publish&order=ASC&orderby=date');
$i = 1;
foreach( $pages as $page ) {
$content = $page->post_title;
if( empty($content) ) continue;
$content = apply_filters('the_content', $content);
if ($i%2===0) { ?><tr class="gigpress-row gigpress-alt">
<?php } else { ?><tr class="gigpress-row"><?php } ?>
<td><?php echo $page->post_title ?></td>
<td><?php echo get_post_meta($page->ID, "p30-length", true); ?></td>
<td><a href="http://itunes.com/<?php echo get_post_meta($page->ID, "p30-itunes-song", true); ?>">BUY</a></td>
</tr>
<?php $i++;
} ?>
Thanks,
Wade
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有太多使用自定义分类法,但如果我没记错的话,最好的选择是使用 query_post 来查询分类法。
I have not used custom taxonomies too much but if I remember correctly the best option is to use query_post to query taxonomies.