1
votes

I have some task which try to solve may be not in smartest way, but so far I got only this idea. I have 5 custom post types. In each of this - one post. Want these posts loop and appear on my index.php .Also these posts has the_field() functions from ACF plugin. The result not what I expected. This is the code in index.php

<?php

        $posttypes = array( 'plays','watch','news','shop','contact' );
        $args = array(
           'post_type' => $posttypes
        , 'posts_per_page' => 5 );
        $the_query = new WP_Query( $args );
        ?>
        <?php if ( $the_query->have_posts() ) : ?>

        <div id="owl-demo" class="owl-carousel owl-theme">
        <?php foreach($posttypes as $posttype){?>
            <?php $the_query->the_post(); ?>
        <div class="item">
            <?php get_template_part( 'single', $posttype ); ?>
        </div>
            <?php };?>

            <?php wp_reset_postdata(); ?>

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

Also I will post one of the post's code, like single_news.php

<?php get_header();

while ( have_posts() ) : the_post() ;?>

    <div class="container-fluid "> <!-- play section -->

                    <div class="row">
                        <div class="col-xs-12 col-sm-2 col-md-2 col-lg-2">
                            <h2 class="" style="text-transform:uppercase"><?php the_field('name');?></h2>
                         </div>
                    </div>
        <div class="row">
            <div class="col-xs-12 col-sm-2 col-md-2 col-lg-2">
                <h2 class="" style="text-transform:uppercase"><?php the_field('news_name');?></h2>
            </div>
        </div>
</div> <!-- row -->
        </div>

Actually the end of this file I couldnt post but it itself correct with closing dives and endwhile.

1

1 Answers

1
votes
In args you have post_per_page set to 5 – this means it will only return the 5 latest posts, regardless of Custom Post Type (CPT).
Try '-1' (return everything) for debugging.

`$args = array(
  'post_type' => $posttypes, 
  'posts_per_page' => -1
);`

Do you want it to return 1 latest post from each CPT?

Maybe try this...

<?php while( $the_query->have_posts() ) : ?>
    <?php $the_query->the_post(); ?>
    <div class="item">
        <?php get_template_part( 'single', get_post_type() ); ?>
    </div>
<?php endwhile; ?>


Or run query for each posttype...

<?php foreach( $posttypes as $posttype ) : ?>
    <?php
    $args = array(
        'post_type' => $posttype,
        'posts_per_page' => 5 );
    $the_query = new WP_Query( $args );
    ?>
    <?php while( $the_query->have_posts() ) : ?>
        <?php $the_query->the_post(); ?>
        <div class="item">
            <?php get_template_part( 'single', $posttype ); ?>
        </div>
    <?php endwhile; ?>
<?php endforeach; ?> 
//I think endforeach exists, otherwise use curly braces

Not sure exactly what you want without seeing it visually.