Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress条件类别php循环_Php_Wordpress_Loops - Fatal编程技术网

Wordpress条件类别php循环

Wordpress条件类别php循环,php,wordpress,loops,Php,Wordpress,Loops,我在一个网站上工作,我想知道是否有可能有一个循环来显示相关的帖子(在侧边栏或其他地方),但前提是有符合条件的相关帖子 例如:我正在阅读一个关于lightning的页面,侧边栏应该显示类别“thiscategory”(本例中为lightning)和“whitepaper”(始终固定)中的所有帖子 我尝试了以下循环,但它给了我一个语法错误: <!-- Start the Loop. --> <?php if ( have_posts() ) : whil

我在一个网站上工作,我想知道是否有可能有一个循环来显示相关的帖子(在侧边栏或其他地方),但前提是有符合条件的相关帖子

例如:我正在阅读一个关于lightning的页面,侧边栏应该显示类别“thiscategory”(本例中为lightning)和“whitepaper”(始终固定)中的所有帖子

我尝试了以下循环,但它给了我一个语法错误:

     <!-- 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; ?>


您要做的是使用query_posts()



语法错误是什么?这个选择语句是
iff(!in_category('whitepaper'))
是毫无意义的,因为您没有对它做任何事情,而且您应该始终使用true条件作为第一个if,使用false条件作为else部分。这里是你的代码清理:此外,为了可读性,请使用缩进和新行来布局代码。。。当您使用
endwhile
endif
语法而不是大括号时,尤其如此
<?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; ?>