If Else with a twist - maybe
Can i make a If Else-statement that finishes a div before doing "else"? I want to cycle through my posts and put the posts from "specialCat" in one div.
<div id="the-div-i-want-to-close开发者_开发问答">
<?php if (in_category('specialCat')) { ?>
<h1><?php the_title(); ?></h1>
<?php } else { ?>
<h1><?php the_title(); ?></h1>
<?php } ?>
Thanks a lot :)
Your best bet is to modify the query calling your special category then finishing the loop with the rest of the posts.
<?php
$args = array(
'category_name' => 'special-cat', // Use Category slug
'posts_per_page' => -1 // Get all the post in SpecialCat
);
$special_query = new WP_Query( $args );
?>
<div id="the-div-i-want-to-close">
<?php
while ( $special_query->have_posts() ) : $special_query->the_post();
$no_repeat[] = $post->ID ?>
<h1><?php the_title(); ?></h1>
<?php endwhile; ?>
</div> <?php // Close your special div after looping through all SpecialCat posts
// Start the loop again
if (have_posts() ) : while (have_posts() ) : the_post();
if (in_array( $post->ID, $no_repeat ) ) continue; // Exclude the posts we already got above ?>
<h1><?php the_title(); ?></h1>
<?php endwhile; endif; ?>
You can include HTML within a for each loop, so long as you close the PHP tags (or include the HTML in an echo statement).
Would this do it for you?
<div id="the-div-i-want-to-close">
<?php $isOpen = true; ?>
<?php if (in_category('specialCat')) { ?>
<?php the_title(); ?>
<?php } else { ?>
<?php if($isOpen) { ?>
</div>
<?php $isOpen = false; ?>
<?php } ?>
<?php the_title(); ?>
<?php } ?>
精彩评论