• I’m trying to split up the output of get_posts in seperate divs per 2 posts. I.E:

    <div container>
     <div sub>
      post 1
      post 2
     </div><!--- sub --->
     <div sub>
      post 1
      post 2
     </div><!--- sub --->
    </div><!--- container --->

    Im trying to accomplish this with array_chunk() and a double foreach.

    $allposts = get_posts('numberposts=6');
    $postgroup = array_chunk($allposts, 2);
    echo '<div class="container">';
    foreach ($postgroup as $posts)
    { echo '<div class="sub">';
    foreach ($posts as $post) {
    echo '<li>';
    php the_title();
    echo '</li>';
    }
    echo '</div>';
    } echo '</div>';
    }

    This works in so far I do get 6 posts and the divs are in the correct place… but… it’s 6 times the same post.

    Where does my thinking go wrong here?

Viewing 13 replies - 1 through 13 (of 13 total)
  • DigitalSquid

    (@twelvefootsnowman)

    Try replacing:

    the_title();

    With:

    echo $post->post_title;

    Thread Starter extatix

    (@extatix)

    Wonderful! That works, thanks a heep.

    Thanks for the tips!

    But what if you need to retrieve the thumbnail and the permalink of this post?

    Because the_permalink(); and the_post_thumbnail(‘thumbnail’); doesn’t work.

    Thanks in advance,

    Jk_

    Thanks for the tips!

    But what if you need to retrieve the thumbnail and the permalink of this post?

    Because the_permalink(); and the_post_thumbnail(‘thumbnail’); doesn’t work.

    Thanks in advance,

    Jk_

    @alchymyth : Thanks I did it read these topics!

    But the internal function setup_postdata() doesn’t fix my problem…

    <?php
    $args=array(
    'post__in' =>get_option("sticky_posts"),
    'showposts'=>12,
    );
    $recent = get_posts($args);
    $chunked_posts = array_chunk($recent,4);
    ?>
    <?php foreach($chunked_posts as $recent): ?>
        <div>
            <?php foreach($recent as $post): setup_postdata($post);?>
                <div class="thumb-container">
    				<img src="<?php bloginfo('stylesheet_directory'); ?>/images/thumb.jpg" />
    				<div class="overlay-text"><a href="<?php the_permalink(); ?>" title="<?php echo $post->post_title; ?>"><p class="text-overlay"><?php echo $post->post_title; ?></p></a></div>
    			</div>
             <?php endforeach;?>
        </div>
    <?php endforeach;?>

    i checked your code in my test setup (instead of the sticky posts, i used cat=0); and it works at least with the permalink, and the_content(), and post thumbnail;

    what is your output instead of ‘the_permalink()’ ?

    @alchymyth : It outputs the permalink of the first Sticky Post!

    Ohh no.. in fact, it outputs the permalink of the first post within my main loop…

    When I use new WP_Query it outputs an error : Warning: array_chunk() expects parameter 1 to be array, object given

    <?php
    $args=array(
    //'post__in' =>get_option("sticky_posts"),
    'showposts'=>12,
    );
    $recent = new WP_Query($args);
    $chunked_posts = array_chunk($recent,4);
    ?>
    <?php foreach($chunked_posts as $recent): ?>
        <div>
            <?php foreach($recent as $post): setup_postdata($post);?>
                <div class="thumb-container">
    				<img src="<?php bloginfo('stylesheet_directory'); ?>/images/thumb.jpg" />
    				<div class="overlay-text"><a href="<?php the_permalink(); ?>" title="<?php echo $post->post_title; ?>"><p class="text-overlay"><?php echo $post->post_title; ?></p></a></div>
    			</div>
             <?php endforeach;?>
        </div>
    <?php endforeach;?>

    What am I missing, here?

    Thread Starter extatix

    (@extatix)

    Good to see I’m not the only one trying to do this ?? I’m getting the same ‘error’ of course, the WP_query isn’t an array.

    @extatix : Yeah, I realized that!

    Everything is Ok now… The only things that I have to overcome was the_permalink who outputs the link of the first post! Weird.

    My final code :

    <?php
    $args=array(
    'post__in' =>get_option("sticky_posts"),
    'showposts'=>12,
    );
    $my_query = get_posts($args);
    $chunked_posts = array_chunk($my_query,4);
    ?>
    <?php foreach($chunked_posts as $recent): ?>
        <div>
            <?php foreach($recent as $post): setup_postdata($post);?>
                <div class="thumb-container">
    				<?php if ( has_post_thumbnail()) the_post_thumbnail('feature-image'); ?>
    				<div class="overlay-text">
    				<a href="<?php bloginfo('url')?>/<?php echo $post->post_name; ?>" title="<?php echo $post->post_slug; ?>">
    					<span class="thumb-size">
    					<p class="text-overlay"><?php echo $post->post_title; ?></p>
    					</span>
    				</a>
    				</div>
    			</div>
             <?php endforeach;?>
        </div>
    <?php endforeach;?>
    Thread Starter extatix

    (@extatix)

    Hm, lemme see if I can get this to work like I want.

    I changed the permalink line though.

    <a href="<?php echo $post->guid ?>" title="<?php echo $post->post_slug; ?>">

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Splitting up the output of get_posts’ is closed to new replies.