最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to implement template file and the loop

programmeradmin6浏览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.

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();
?>
发布评论

评论列表(0)

  1. 暂无评论