Wordpress - 在所选帖子的sidebar.php中输出相关帖子

时间:2012-02-08 09:16:28

标签: php wordpress conditional

这看起来应该非常简单,我想在活动发布时输出最新的三个事件,在新闻帖子上输出最新的三个新闻项目,我已经在非常基本的意义上工作了我可以弄清楚如何执行以下操作:

当在活动中的帖子上显示最新的三个事件时,在新闻节目中显示最新的三个新闻帖子。我在这里查看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 {

  }

  ?>

1 个答案:

答案 0 :(得分:1)

条件评论应该有效,你只有一个字母关闭(in vs.)

您需要in_category(),而不是is_category()

http://codex.wordpress.org/Template_Tags/in_category