Showing Custom Post Type with his templates on a custom page template

admin2025-06-03  6

So here's my question:

I need to build a custom post type to add employees to a website. The problem is, it need to be shown:

1 - Image on the Right 2 - Image on the Left ... and so on.

For this I made a page template where I need this custom post type content to be shown. To style the custom post type differently I made two different post type templates and I created two posts with those templates.

Now my problem is: How can I show this custom post types with his templates inside my custom page template?

I tried with this code:

<?php
  $args = array( 'post_type' => 'equipa', 'posts_per_page' => 10 );
  $loop = new WP_Query( $args );
  while ( $loop->have_posts() ) : $loop->the_post();
    the_post();
  endwhile;
 ?>

But it's throwing me these errors:

Notice: Undefined offset: 1 in C:\xampp\htdocs\WP\wp-includes\class-wp-query.php on line 3071

Notice: Undefined offset: 2 in C:\xampp\htdocs\WP\wp-includes\class-wp-query.php on line 3071

Anyone have any idea on what i'm doing wrong here?

So here's my question:

I need to build a custom post type to add employees to a website. The problem is, it need to be shown:

1 - Image on the Right 2 - Image on the Left ... and so on.

For this I made a page template where I need this custom post type content to be shown. To style the custom post type differently I made two different post type templates and I created two posts with those templates.

Now my problem is: How can I show this custom post types with his templates inside my custom page template?

I tried with this code:

<?php
  $args = array( 'post_type' => 'equipa', 'posts_per_page' => 10 );
  $loop = new WP_Query( $args );
  while ( $loop->have_posts() ) : $loop->the_post();
    the_post();
  endwhile;
 ?>

But it's throwing me these errors:

Notice: Undefined offset: 1 in C:\xampp\htdocs\WP\wp-includes\class-wp-query.php on line 3071

Notice: Undefined offset: 2 in C:\xampp\htdocs\WP\wp-includes\class-wp-query.php on line 3071

Anyone have any idea on what i'm doing wrong here?

Share Improve this question asked Feb 8, 2019 at 11:55 Ariano ÂngeloAriano Ângelo 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

So I found the solution: You just insert both templates (made like every normal template for custom post types) and then come to the page-template and do this:

        <?php
        $args = array( 'post_type' => 'equipa', 'order' => 'ASC' );
        $loop = new WP_Query( $args );
        $c = 0;
        while ( $loop->have_posts() ) : $loop->the_post();
            if ($c % 2 == 0) {
                include 'template-post-left.php';
            } else {
                include 'template-post-right.php';
            }
            $c++    ;
        endwhile;
        ?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748918704a314812.html

最新回复(0)