Variable/Code to use in template so content of the page shows at the spot

Learning some wordpress, creating my first custom wordpress site. I created template for my home page and then I want to create one template for a good bunch of sub pages. So I created a file page-sub.php. In pages I can see this template, so I assigned it to my sub pages. But I'm missing the code/variable to show the content of the page that uses the template. Can you guys advise what do I need? Below is my code in the template so far.

<?php
 /*
   Template Name: Sub Page Template
 */

  get_header();
?>

<div class="main">
  <div class="container">

     <?php
        if ( is_singular() ) :
            the_title( '<h1 class="entry-title">', '</h1>' );
        endif;

        the_content();
     ?>

   </div>
</div>
<?php 
    get_footer();
?>

1 answer

  • answered 2018-07-11 04:17 itivae

    I think you need to use the Wordpress Post loop.

    <?php 
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post(); 
            //
            // Post Content here
            //
        } // end while
    } ?>
    

    Perhaps this is what you're looking for. https://codex.wordpress.org/The_Loop

    As far as your code goes I might try to make you html outside the php for example to do what I think you want to do is this.

    <?php 
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post(); 
            <h1 class="entry-title"><?php the_title(); ?></h1>
            <?php the_content(); ?>
        } 
    } ?>
    

    I am not 100% on why you are using the singular if statement. Is there something specific you want to do with that? Something like this?

    <?php 
        if ( have_posts() ) {
            while ( have_posts() ) {
                the_post(); 
                if ( is_singular() ) : ?>
                    <h1 class="entry-title"><?php the_title(); ?></h1>
                <?php endif; ?>
                <?php the_content(); ?>
            } 
        }
    ?>