WordPress - 在选定帖子的 sidebar.php 中输出相关帖子
这看起来应该很容易,我想在事件帖子上输出最新的三个事件,在新闻帖子上输出最新的三个新闻项目,我已经在非常基本的意义上做到了这一点,但是我可以工作了解如何执行以下操作:
在事件中的帖子上时显示最新的三个事件,在新闻中的帖子上时显示最新的三个新闻帖子。我在这里查看了 http://codex.wordpress.org/Conditional_Tags 并认为 is_category(' news') 和 is_category('events') 可以解决这个问题,但是这些条件似乎只在类别存档页面上可用。
我目前有以下代码,可以简单地检查您是否在单个帖子页面上(我只使用新闻和事件的帖子,其他所有内容都是一个页面):
<?php
if ( is_single() ) {
query_posts('category_name=news&order=DSC&offset=1&orderby=ID&posts_per_page=2');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<article>
<h3><a href='<?php the_permalink() ?>'><?php the_title(); ?></a></h3>
<span class="date"><?php the_time(get_option('date_format')); ?></span>
<?php the_excerpt($strip_teaser); ?>
<a class="more" href='<?php the_permalink() ?>'>Read more</a>
</article>
<?php endwhile; endif; wp_reset_query();?>
<?php
}
else {
}
?>
This seems like something that should be really easy, I want to output the latest three events when on an event post and the latest three news items when on a news post, I've got this working in a very basic sense however I can work out how to do the following:
When on a post in events show the latest three events, when on a post in news show the latest three news posts. I've looked on here http://codex.wordpress.org/Conditional_Tags and thought that is_category('news') and is_category('events') would do the trick however these conditionals seem to only be available on category archive pages.
I currently have the following code which simple checks you're on a single post page (I'm only using posts for news and events, everything else is a page):
<?php
if ( is_single() ) {
query_posts('category_name=news&order=DSC&offset=1&orderby=ID&posts_per_page=2');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<article>
<h3><a href='<?php the_permalink() ?>'><?php the_title(); ?></a></h3>
<span class="date"><?php the_time(get_option('date_format')); ?></span>
<?php the_excerpt($strip_teaser); ?>
<a class="more" href='<?php the_permalink() ?>'>Read more</a>
</article>
<?php endwhile; endif; wp_reset_query();?>
<?php
}
else {
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
条件注释应该有效,您只差一个字母(in 与 is)
您想要
in_category()
,而不是is_category()
http://codex.wordpress.org/Template_Tags/in_category
Conditional comments should work, you were just one letter off (in vs. is)
You want
in_category()
, notis_category()
http://codex.wordpress.org/Template_Tags/in_category