0

我在 Wordpress 中实现静态页面的正确方法面临​​着一个大问题。我已经阅读了将近 5 天,但仍然无法弄清楚它(应该)如何工作。

我面临的问题如下:

当我在定制器中使用“显示最新帖子”选项时,我会看到应有的首页。我得到了主页的文本,然后是最新的帖子。我在这里面临的问题是主页文本是硬编码在我的 home.php 中的。我希望能够在我的 wordpress 编辑器的家庭输入字段中更改它。

所以我明白我应该利用 index.php 并创建一个名为“Home”的页面和一个名为“Blog”的页面。我将这些页面设置为静态页面,我将能够完成我想要的。但我没有。我只是无法完成它。

所以我尝试在我的本地机器上全新安装 WP。设置一个全新的安装,只创建了 2 个页面(主页和博客)。转到设置->阅读->设置静态页面:主页:主页帖子页面:博客。已保存更改。

到主页,我刚刚看到我的主页。上面没有帖子。

我在这里想念什么?

4

1 回答 1

0

您正在使用一个名为“主页”的页面,该页面为空。这是预期的,很好。您真正需要的是创建一个自定义模板(https://developer.wordpress.org/themes/template-files-section/page-template-files/#creating-custom-page-templates-for-global-use) ,并创建您想要的任何自定义布局

扩展答案

例如,创建一个名为 homepage.tpl.php 的模板。把这段代码放在里面:

<?php
/**
 * Template Name: Custom Homepage
 */

get_header(); ?>

<div>
  <?php    
  if ( have_posts() ) :    
     while ( have_posts() ) : the_post();
       the_content();
     endwhile;
  endif;   
  ?>  
</div>

<div>
  <?php
  $wp_query = new WP_Query(array(
    'post_type' => 'post',
    'post_status' => 'publish'
  ));
  if ( $wp_query->have_posts() ) :
    while ( $wp_query->have_posts() ) : $wp_query->the_post();
      the_title();
      /* Post loop content goes here */    
    endwhile;
    wp_reset_postdata();
  endif;
  ?>
</div>

<?php get_footer(); ?>

转到管理面板->页面->单击编辑“主页”。在右侧边栏中选择名为“自定义主页”的模板。就是这样。

于 2018-10-22T11:05:01.257 回答