2012-02-07 83 views
0

我正在一个网站上,我想知道是否有可能有一个循环,显示相关的职位(在侧边栏或其他地方),但只有当有任何相关的职位匹配标准。例如:我正在阅读关于闪电的一个页面,侧边栏应显示类别“thiscategory”(本例中为闪电)和“白皮书”(总是固定的)中的所有帖子。WordPress的条件类别的PHP循环

我试过下面的循环,但它给了我一个语法错误:

 <!-- Start the Loop. --> 

     <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

     <?php if (! in_category('whitepaper')) { ?> 

     <!-- don't display anything if it's nog in category whitepaper --> 

     <?php } else { 

     $mycat = get_the_category; 

     if (in_category($mycat) { ?> 
     bla 
      <?php } ?> 

     <?php } ?> 

     <?php endwhile; ?> 
     <?php endif; ?> 

任何帮助,不胜感激!

+1

什么是语法错误?这个选择语句是'iff(!in_category('whitepaper'))'是毫无意义的,因为你对它没有做任何事情,再加上你应该始终使用真实条件作为else条件的第一个if和false条件。这里是你的代码清理:http://pastie.org/3335622 – Flukey 2012-02-07 17:17:49

+0

此外,为了便于阅读,请使用缩进和新行来布置代码......当你使用'endwhile'时尤其如此和'endif'语法而不是大括号 – 2012-02-07 17:42:27

回答

2

你想要做的是使用query_posts()。

<?php 
//Grab the two category ID you are interested in. 
$white_paper = get_cat_ID('whitepaper'); 
$curr_cat = get_query_var('cat'); 

//Query posts for the categories you want 
query_posts("cat=$white_paper,$curr_cat"); 

//Now loop as normal 
if (have_posts()) : while (have_posts()) : the_post(); 
?> 
    <div class="entry"><?php the_content(); ?></div> 
<?php endwhile; ?> 
<?php endif; ?>