I am using multiple loops like this
<?php
$posts = get_posts('numberposts=1&order=DESC&orderby=post_date&category='.get_cat_ID( 'سیاست' ));
foreach ($posts as $post) : start_wp(); ?>
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<div class="content">
<h2><a href="<?php the_permalink()?>"><?php the_title()?></a></h2>
<p><?php the_excerpt(); ?></p>
<hr/>
<?php
endforeach;
?>
In most part of my theme like sidebar, footer, slidershow with different category, the problem that I've faced with is that when I'm using the following loop in archive.php or category.php to show the post data(like title, excerpt and feature image) based on category it doesn't show me anything. When I remove all above loop, it works. This is the loop in my archive or category.php to show post excerpt and title:
<?php if(have_posts()):?>
<?php while ( have_posts() ) : the_post(); ?>
<?Php the_title(); ?><br/>
<?php endwhile; ?>
<?php endif;?>
I remember a time that I had this problem and solved by using wp_reset_query
after each loop but it doesn't work now. Maybe wp_reset_query
doesn't work for foreach loop.
WP_Query()
and usewp_reset_postdata()
at the end of each query block. It's more dynamic and a nice one. :)start_wp()
has been deprecated since 1.5.setup_postdata()
.