posts - Am getting duplicate data from get_posts()

Am trying to display posts only from the "latest" category on my wordpress blog but am strangely getting the s

Am trying to display posts only from the "latest" category on my wordpress blog but am strangely getting the same post displayed twice. What am i doing wrong?

/*
* Theme file:index.php
*/
    global $post;
    $categories=get_categories();
    foreach($categories as $categories_item)
    {
        if(strcasecmp($categories_item->name, 'latest') == 0)//case-insensitive string comparison
        {
            $args = array(
            'numberposts'     => 2,
            'category'        => $categories_item->cat_ID,
            'orderby'         => 'post_date',
            'order'           => 'ASC',
            'post_status'     => 'publish'
            );
                $latest_stuff = get_posts( $args );
                echo "<h2>Latest stuff</h2>";
                foreach($latest_stuff as $latest_stuff_item)
                {
                    setup_postdata($post);
                    echo "<div>".the_content()."<div><hr>";
                }   
        }
    }

Am trying to display posts only from the "latest" category on my wordpress blog but am strangely getting the same post displayed twice. What am i doing wrong?

/*
* Theme file:index.php
*/
    global $post;
    $categories=get_categories();
    foreach($categories as $categories_item)
    {
        if(strcasecmp($categories_item->name, 'latest') == 0)//case-insensitive string comparison
        {
            $args = array(
            'numberposts'     => 2,
            'category'        => $categories_item->cat_ID,
            'orderby'         => 'post_date',
            'order'           => 'ASC',
            'post_status'     => 'publish'
            );
                $latest_stuff = get_posts( $args );
                echo "<h2>Latest stuff</h2>";
                foreach($latest_stuff as $latest_stuff_item)
                {
                    setup_postdata($post);
                    echo "<div>".the_content()."<div><hr>";
                }   
        }
    }
Share Improve this question edited Nov 1, 2011 at 4:16 Dr Deo asked Nov 1, 2011 at 4:03 Dr DeoDr Deo 2351 silver badge9 bronze badges 2
  • Does the post posted on multiple categories? – Sisir Commented Nov 1, 2011 at 6:51
  • try to deactivate all plugins to eliminate that influence. – Michael Commented Nov 1, 2011 at 8:09
Add a comment  | 

3 Answers 3

Reset to default 1

Without looking at the database data (posts, inherits, etc), it's a bit of guesswork.

Try the following:

  • Make sure you have "post_type"=>"post" specified, so that you don't get auto-saved inherits (if it defaults to 'any').
  • If this is an isolated query (not meant to be main loop), use $q = new WP_Query() and run it through that instead of get_posts().
  • What does setup_postdata do? Does it accidentally rewind anything?
  • Try escaping the category loop and apply get_category_by_slug('latest') to "category" (it returns an object with ID) to the call.

In my case, it was WPML returning both languages. The solution to that was suppress_filter in the WP_Query.

$args = array(
  ...
  'suppress_filters' => false
  ...
);

$wp_query = new WP_Query( $args );
....

The problem is that you are using echo the_content();.

This is causing the looped output to appear outside the loop. If you change the_content() to get_the_content() this will work.

Note you might need to pass through the ID into get_the_content() by using $latest_stuff_item->ID

foreach($latest_stuff as $latest_stuff_item)
{
  setup_postdata($post);
  echo "<div>".get_the_content()."<div><hr>";
}  

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745168264a4614766.html

相关推荐

  • posts - Am getting duplicate data from get_posts()

    Am trying to display posts only from the "latest" category on my wordpress blog but am strangely getting the s

    7小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信