doula2426
doula2426
2017-04-04 07:35
浏览 51
已采纳

如何制作自定义WordPress主题节目页面,而不是帖子

I have recently created a custom WordPress theme, following a guide I found online.

However, all of the guides I came across explain how to set up a blogging site and do not explain how to set up a static website page. I am guessing that it is something to do with this code in my index.php:

.

                >.   get_template_part( 'content', get_post_format() );

            >.   endwhile; endif; 
        >.   ?>

When I remove this, it removes the blog layout. However, I do not know what to amend to make a static page layout.

Can anyone help please?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douqiu1604
    douqiu1604 2017-04-04 09:15
    已采纳

    get_post_format() Returns the post format of a post. This will usually be called in the the loop, but can be used anywhere if a post ID is provided. and its usually found on the posts pages/blogs.. therefore Its used in a theme when you want to display blog posts.

    As you you might know that wp have different posts types. let's just take two of them.

    Post (Post Type: 'post')

    Page (Post Type: 'page')

    The get_post_format() is used to get the format of a post,(blog Post) if you open your wp dashboard and start a new post or editing existing post you will see different posts formats types

    enter image description here

    As you can see from the image above, the red highlited part is the post format, that is what you are requesting when u use get_post_format() wp treats that template as it should display posts, but not pages.

    If you want the content of the page you then need to use the_content()

    Therefore this

    get_template_part( 'content', get_post_format() );
    
    endwhile; endif;
    

    becomes :

    get_template_part( 'content', the_content() );
    
    endwhile; endif; 
    

    alternatively :

     <div class="YourContainer">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
      <div class="YourContentDiv">
    
       <?php the_content();?>
    
      </div>
      <?php endwhile; ?>
      <?php else:
            echo "no content found";
       ?>
    <?php endif; ?>
     </div>
    

    Hope this helps, Goodluck

    点赞 评论
  • douyan1921
    douyan1921 2017-04-04 08:34

    If you look at the page template hierarchy you will see which template is used in which circumstances:

    https://developer.wordpress.org/themes/basics/template-hierarchy/

    For example if you create a front-page.php template it will take precedence of other templates.

    If you set a static home page in settings then the template used will depend on the hierarchy first a custom named selectable template if this isn't set then a page-$slug.php template e.g. home-page.php then if that doesn't exist then a page-$id.php e.g 15-page.php if this doesn't exist then page.php will be used then singular.php then index.php

    点赞 评论

相关推荐