1

I've 2 post types and I want to output them alternately but without using many loops so I found this solution which does that.

However, it is not ideal as I need to output the_post_thumbnail which I find I am unable to do using this method (echo $smallPosts->posts[$i]->post_thumbnail; does nothing). Additonally I've read post_content is not the same as the_content(); - with the latter what I want to use.

Any suggestions on how I can loop through the alternating post types and have more control over the output so I can use the_post_thumnail etc.?

Below is my code that does work but just doesn't quite do what I require.

    <?php $args = array(
        'post_type' => 'small_post',
        'posts_per_page' => 3
      );
    $smallPosts = new WP_Query($args);

    $args = array(
        'post_type' => 'full_post',
        'posts_per_page' => 3
      );
    $fullPosts = new WP_Query($args);



     for ($i = 0; $i < 3; $i++) {
        if ($smallPosts->post_count > $i)

            echo $smallPosts->posts[$i]->post_title;
            echo '<br />';
            echo $smallPosts->posts[$i]->post_content;
            echo '<br />';

        if ($fullPosts->post_count > $i) 
            echo $fullPosts->posts[$i]->post_title;
            echo '<br />';
            echo $fullPosts->posts[$i]->post_content;
            echo '<br />';
       }    

    ?>
Community
  • 1
  • 1
icabob91
  • 13
  • 6
  • 1
    I am not sure I fully understand your question, can't you just go `echo $smallPosts->posts[$i]->post_thumbnail;`? Could you please add this code to the bottom of your code and tell us what it outputs: `echo '
    ', var_dump($smallPosts), '
    '; echo '
    '; echo '
    ', var_dump($fullPosts), '
    ';die;`
    – Jethro Hazelhurst Nov 01 '16 at 11:23
  • 1
    Hi @JethroHazelhurst, I tried echo $smallPosts->posts[$i]->post_thumbnail; but it does nothing. Added the code and the output is here https://codepen.io/kgconnect16/pen/ameLLQ – icabob91 Nov 01 '16 at 11:45
  • Hi, it doesn't look like there is any property called post_thumbnail, or any thumbnail property, so you won't be able to use functionality that is not there... how are you adding your thumbnail images? – Jethro Hazelhurst Nov 01 '16 at 12:01
  • @JethroHazelhurst - that's what I thought (there's not thumb functionality) but can't think of another way to output the posts like this without just doing lots of while loops which is fairly inefficient. Not sure what you mean "how are you adding your thumbnail images?" apart from I'm just uploading them as a featured image on wp. – icabob91 Nov 01 '16 at 12:16

1 Answers1

0

This is my solution which outputs both post types and using the time published they can be alternated and I can use the_thumbnail( ); and other functions I need. Additionally I've used if statements to add classes as the different post types need to be styled differently.

                 <ul>                       
                    <?php

                        $args = array( 
                            'posts_per_page' => 10, 
                            'post_type' => (array('small_post','full_post')),
                            );
                        query_posts($args); ?>

                        <?php if ( have_posts() ) : ?>

                        <?php while ( have_posts() ) : the_post();  ?>
                            <?php if ( get_post_type( get_the_ID() ) == 'small_post' ) { ?>
                            <li class="article small-post" style="">
                                <?php if(has_post_thumbnail()) :?>
                                    <a href="<?php the_permalink(); ?>">
                                        <?php the_post_thumbnail(''); ?>
                                    </a>
                                <?php endif;?>
                                <a href="<?php the_permalink(); ?>">
                                    <h3>
                                        <?php the_title(); ?>
                                    </h3>
                                </a>
                                <p><?php the_excerpt(); ?></p> 
                            </li>
                        <?php } ?>      

                        <?php if ( get_post_type( get_the_ID() ) == 'full_post' ) { ?>
                            <li class="article full-post" style="">
                                <?php if(has_post_thumbnail()) :?>
                                    <a href="<?php the_permalink(); ?>">
                                        <?php the_post_thumbnail(''); ?>
                                    </a>
                                <?php endif;?>
                                <a href="<?php the_permalink(); ?>">
                                    <h3>
                                        <?php the_title(); ?>
                                    </h3>
                                </a>
                                <p><?php the_excerpt(); ?></p> 
                            </li>
                        <?php } ?>                      
                        <?php endwhile; ?>


            <?php wp_reset_postdata(); ?>

            <?php else : ?>
            <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
            <?php endif; ?>

           </ul>
icabob91
  • 13
  • 6