Wordpress: looping through alternating post types

2019-08-02 21:18发布

问题:

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 />';
       }    

    ?>

回答1:

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>