0

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.

5
  • I always use WP_Query() and use wp_reset_postdata() at the end of each query block. It's more dynamic and a nice one. :) Commented Feb 24, 2014 at 15:13
  • 2
    start_wp() has been deprecated since 1.5.
    – s_ha_dum
    Commented Feb 24, 2014 at 15:20
  • i'm just done it but it doesn't worked Commented Feb 24, 2014 at 15:20
  • so what can i used instead of start_wp() Commented Feb 24, 2014 at 15:21
  • 2
    "so what can i used instead of start_wp()" - setup_postdata(). Commented Feb 24, 2014 at 15:57

1 Answer 1

1

The proper way to set up a secondary query, using your example code. Instead of this:

$posts = get_posts('numberposts=1&order=DESC&orderby=post_date&category='.get_cat_ID( 'سیاست' ));

Do this:

// Query args
$custom_cat_args = array(
    'posts_per_page' => 1,
    'order' => 'DESC',
    'orderby' => 'post_date',
    'cat' => get_cat_ID( 'سیاست' )
);
// Instantiate query
$custom_cat_query = new WP_Query( $custom_cat_args );
// Instantiate loop
if ( $custom_cat_query->have_posts() ) : while ( $custom_cat_query->have_posts() ) : $custom_cat_query->the_post();
    // Normal loop markup here
// Close loop
endwhile; endif;
// Restore $post global
wp_reset_postdata();

Using this method, your custom query will play nicely with your main loop query, and any other secondary queries.

4
  • i i don't know why when i'm using this code. it doesn't filter my post base on specific category Commented Feb 24, 2014 at 17:40
  • ofcourse when i am changing posts_per_page to 15 or more it doesn't filter the post. it display all of my post Commented Feb 24, 2014 at 17:54
  • My mistake; you need to use 'cat' instead of 'category' with WP_Query(). Commented Feb 24, 2014 at 19:21
  • @Hussein you should mark Chip's answer as accepted by clicking on the check mark next to the answer. This will not only help you, but it will help others like you in the future as well Commented Apr 26, 2014 at 6:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.