【问题标题】:Wordpress front-page.php templateWordpress front-page.php 模板
【发布时间】:2018-02-01 18:05:06
【问题描述】:

我将首页设置为静态页面,并尝试构建我的自定义模板。如何在 front-page.php 中实际显示选定的首页?我用谷歌搜索和搜索,但似乎无法弄清楚如何做到这一点。

front-page.php 实际加载的方式应该是这样,但我似乎找不到关于如何准确显示指定为静态主页的页面的文档。有什么建议吗?

我试过了

<?php while ( have_posts() ) : the_post(); ?>
    <?php get_template_part( 'content', 'page' ); ?>
    <?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>

但这似乎不起作用......

【问题讨论】:

  • 如果您在设置 -> 阅读中将页面设置为主页,则 wordpress 将自动应用模板 front-page.php 而无需在模板菜单中选择它。然后,标准循环将使用该特定帖子而无需任何自定义查询。
  • 你是绝对正确的。我正在循环调用模板 content-page.php,它实际上包含帖子的信息。

标签: php wordpress


【解决方案1】:

您的静态页面使用页面模板(通常为 page.php 作为默认模板)

如果您愿意,您可以为主页创建一个新的。请参阅:Creating_Your_Own_Page_Templates 复制 page.phphomepage.php 并更改模板名称

示例模板(homepage.php):

<?php
/*
Template Name: Homepage
*/

//the content of page.php and now you can do what you want.
?>

【讨论】:

    【解决方案2】:
    $id = 0; /* The id of your page */
    $page = get_page($id);
    echo apply_filters('the_content', $page->post_content);
    

    如果是静态页面,我不应该使用循环。

    【讨论】:

      【解决方案3】:
      【解决方案4】:

      我遗漏了一些明显的东西。我使用的循环是从 wordpress 模板中复制出来的。它实际上调用了另一个模板文件。我应该使用的是:

      <?php while ( have_posts() ) : the_post(); ?>
                  <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                      <header class="entry-header">
                          <h1 class="entry-title"><?php the_title(); ?></h1>
                      </header>
      
                      <div class="entry-content">
                          <?php the_content(); ?>
                          <?php wp_link_pages(array('before' => '<div class="page-links">' . __('Pages:', 'twentytwelve'), 'after' => '</div>')); ?>
                      </div><!-- .entry-content -->
                      <footer class="entry-meta">
                          <?php edit_post_link(__('Edit', 'twentytwelve'), '<span class="edit-link">', '</span>'); ?>
                      </footer><!-- .entry-meta -->
                  </article><!-- #post -->
      <?php   endwhile;?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-25
        相关资源
        最近更新 更多