• Hello,

    I can’t find the solution for displaying my custom post type and the page content. I would like to display at first the content of the regular wordpress page and after that, I would like to display a portfolio (for example) with the custom post types:

    At first I registered the custom post type in the functions.php – no problem. Then I created a page-template with a CPT-Loop.

    <?php
    /**
     * Template Name: Portfolio
     * The template for displaying portfolio posts.
     *
     * @package blalba
     */
    
    get_header(); ?>
    
    	<div id="primary" class="content-area col-md-9">
    		<main id="main" class="site-main" role="main">
    			<?php 
    
                $args = array( 'post_type' => 'portfolio', 'posts_per_page' => 9 );
                $loop = new WP_Query( $args );
                while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
                <?php get_template_part( 'content', 'page' ); ?>
    
    				<?php
    					// If comments are open or we have at least one comment, load up the comment template
    					if ( comments_open() || get_comments_number() ) :
    						comments_template();
    					endif;
    				?>
    
    			<?php endwhile; // end of the loop. ?>
    
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    My Question: How should I code the loop / query to display both (CPT and Page Content)? I found in the WP Codex a multiple Loop… but it doesn’t work for me.

    Thanks,

    David

Viewing 2 replies - 1 through 2 (of 2 total)
  • You can display the page content using the main loop, which doesn’t required a new WP_Query instance as it already exists.

    if ( have_posts() ) {
      while ( have_posts() ) {
        the_post();
        /* page markup here using the_content() etc */
      }
    }
    
    $args = array( /* args */ );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
      while ( $loop->have_posts() ) {
        $loop->the_post();
        /* loop markup here using the_content() etc */
      }
    }
    
    wp_reset_postdata(); // reset to the original page data
    Thread Starter dst89

    (@dst89)

    Hi karpstrucking,

    thanks for your answer.

    I realized your code like this:

    <?php while ( have_posts() ) : the_post(); ?>
    
    				<?php get_template_part( 'content', 'page' ); ?>
    
    				<?php
    					// If comments are open or we have at least one comment, load up the comment template
    					if ( comments_open() || get_comments_number() ) :
    						comments_template();
    					endif;
    				?>
    
    		<?php endwhile; // end of the loop. ?>
    
            <?php wp_reset_postdata(); ?>
    
    		<?php 
    
            $args = array( 'post_type' => 'portfolio', 'posts_per_page' => 9 );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
            <?php get_template_part( 'content', 'page' ); ?>
    
    			<?php
    				// If comments are open or we have at least one comment, load up the comment template
    				if ( comments_open() || get_comments_number() ) :
    					comments_template();
    				endif;
    			?>
    
    		<?php endwhile; // end of the loop. ?>
    
            <?php wp_reset_postdata(); ?>

    … and this works great for me.

    Is this the way as it provides for wordpress?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Query / Loop for Custom Post Type AND Page Content’ is closed to new replies.