WordPress.org

Ready to get started?Download WordPress

Forums

Splitting up the output of get_posts (13 posts)

  1. extatix
    Member
    Posted 3 years ago #

    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?

  2. DigitalSquid
    Member
    Posted 3 years ago #

    Try replacing:

    the_title();

    With:

    echo $post->post_title;

  3. extatix
    Member
    Posted 3 years ago #

    Wonderful! That works, thanks a heep.

  4. jk_
    Member
    Posted 3 years ago #

    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_

  5. jk_
    Member
    Posted 3 years ago #

    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_

  6. alchymyth
    Forum Moderator
    Posted 3 years ago #

  7. jk_
    Member
    Posted 3 years ago #

    @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;?>
  8. alchymyth
    Forum Moderator
    Posted 3 years ago #

    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()' ?

  9. jk_
    Member
    Posted 3 years ago #

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

  10. jk_
    Member
    Posted 3 years ago #

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

  11. jk_
    Member
    Posted 3 years ago #

    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?

  12. extatix
    Member
    Posted 3 years ago #

    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.

  13. jk_
    Member
    Posted 3 years ago #

    @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;?>
  14. extatix
    Member
    Posted 3 years ago #

    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; ?>">

Topic Closed

This topic has been closed to new replies.

About this Topic