简体   繁体   中英

Wordpress conditional category php loop

I'm working on a website, and I was wondering if it's possible to have a loop that shows related posts (in a sidebar or somewhere else), but only if there are any related posts matching the criteria.

For instance: I'm reading a page about lightning, the sidebar should show all the posts in the category "thiscategory"(lightning in this case) AND "whitepaper"(always fixed).

I've tried the following loop, but it gives me a syntax error:

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

Any help is greatly appreciated!

What you want to do is use 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; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM