admin管理员组

文章数量:1327843

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.

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.

Share Improve this question edited Aug 1, 2020 at 13:23 Justin Freeman asked Jul 31, 2020 at 10:35 Justin FreemanJustin Freeman 11 bronze badge 3
  • 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 Commented Jul 31, 2020 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 Commented Jul 31, 2020 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 Commented Aug 1, 2020 at 8:35
Add a comment  | 

1 Answer 1

Reset to default -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();
?>

本文标签: How to implement template file and the loop