0

I am trying to understand how to use a custom template file, in this case simply to list all posts. Based on my research, and initial responses to this post, the code looks like this:

 <?php
 /**
 * Template Name: Basic Test
 */
 get_header();
 
 if ( have_posts() ) : 
     while ( have_posts() ) : the_content();
        the_title();
     endwhile;
 else :
     _e( 'Sorry, no posts matched your criteria.', 'textdomain' );
 endif;

 get_sidebar();
 get_footer();
 ?>

When I create a new page that uses this template file, the page displays the header, footer, sidebar, page title, and nothing else. I assume I am missing something, but I don't know what. Neither posts nor the error message are displayed.

  • Is the page completely blank? It should at least output the header, so if it's blank you need to have a search for how to turn on error reporting so that you can see when you've got a PHP error, otherwise developing pages like this will be very hard. – mozboz Jul 31 at 13:54
  • This will only display the content of the page in question, not all of your posts, if it's blank it may be because there's no content. Also, you're not calling the content, you'd have to use the_content();. But like mozboz says, can you clarify what's not being displayed? – Tony Djukic Jul 31 at 16:14
  • Thank you for the responses. I do have a header, footer, sidebar, and, after removing the extraneous (); I have a page title. – Justin Freeman Aug 1 at 8:35
-2

I have seen that there is extra (); so maybe this syntax error prevent display in front-end please try below code -

<?php
/**
* Template Name: Basic Test
*/
get_header();

if ( have_posts() ) : 
    while ( have_posts() ) : the_post();
       the_title();
    endwhile;
else :
    _e( 'Sorry, no posts matched your criteria.', 'textdomain' );
endif;

get_sidebar();
get_footer();
?>
| improve this answer | |
  • And also you can use the_content(); to output the actual content of the page. the_title() is the Title of the Post, the_content() is the Content of the Post etc.. – t2pe Jul 31 at 12:28
  • This doesn't seem to help. And when I add the line "the_title()" outside the if ... endif I get the title of the page, not of any post. Is this supposed to happen? – Justin Freeman Jul 31 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.