【问题标题】:How do I combine pages (and their templates) into one page?如何将页面(及其模板)合并到一个页面中?
【发布时间】:2014-01-30 18:57:05
【问题描述】:

我希望 CMS 具有不同的页面(例如“职业”、“工作”、“团队”),每个页面都有自己的模板,然后将它们组合成一个大的可滚动页面(例如“我们的公司”)那会有一个模板。我该怎么做?

我知道 曾经 是一个函数 get_page,但它已被弃用(并替换为 get_post,这不是同一件事),但这不会检索页面的模板。

  1. 我想要页面和模板,所以我可以将两者都输出到主页中。

  2. 我也想要它,所以如果有人在导航菜单中单击以转到“工作”或“团队”,它会将他们带到“我们的公司”页面,但带有查询字符串,以便我可以滚动它们到页面的那个部分

这可能吗?

【问题讨论】:

    标签: php wordpress templates wordpress-theming custom-wordpress-pages


    【解决方案1】:

    首先为主页模板选择默认模板并在那里编写全局元素。在这个模板中使用 get_template 部分来包含页面

    <!--custom query for pages-->
    <?php
    $args= array('post_type'=>'page');
    $query= new WP_Query($args);  
    $query->while(have_posts()):query->the_post(); 
     $temp_name= get_page_template_slug( $post->ID );       
     $temp_name_exp =explode('.',$temp_name);
     get_template_part($temp_name_exp[0]); 
    
    endwhile;
    endif;
    ?>
    

    在职业、博客等页面中

    <?php
    /*
    Template name: Career or blog or something else
    */ 
    ?>
    <?php the_tiele();
          the_content();
     ?>
    

    对于 “我也想要它,所以如果有人在导航菜单中单击以转到“工作”或“团队”,它会将他们带到那个“我们的公司”页面,但带有一个查询字符串,因此我可以将他们滚动到该部分页” 将每个页面包装器分配给页面 slug 示例 &lt;section class="&lt;?php echo $post-&gt;post_name; ?&gt;"&gt; 并编写一个函数将您的视图页面链接重定向到 http://yoursiteurl/#page-slug

    【讨论】:

      【解决方案2】:

      编辑

      为了将一个页面的内容导入另一个页面,请使用以下函数:

      function show_post($path){
       $post = get_page_by_path($path);
       $content = apply_filters('the_content', $post->post_content);
       echo $content;
      }
      

      然后为“我们的公司”页面创建一个模板(例如template-our_company.php),您将在其中调用函数(例如&lt;?php show_post('careers'); /* Shows the content of the "Careers" page using the slug. */ ?&gt;)。

      所以模板文件应该包含如下内容:

      <?php
      show_post('careers'); 
      show_post('jobs'); 
      show_post('team'); 
      ?>
      

      对于您的第二个问题,您需要像这样调整 template-our_company.php 文件:

      <?php
      <div id="careers"></div>
      show_post('careers'); 
      <div id="jobs"></div>
      show_post('jobs'); 
      <div id="team"></div>
      show_post('team'); 
      ?>
      

      然后在菜单仪表板中,只需将导航链接调整为“/our-company/#careers”等内容。

      编辑 2

      为了在另一个模板中检索具有指定模板的页面内容,您可以执行以下操作: 创建模板(文件 careers.php 和 jobs.php) 以及将使用这些模板的帖子

      /*
      Template Name: Careers
      */
      

      ...

      /*
      Template Name: Jobs
      */
      

      然后在“父”模板中,可以查询选择了上述指定模板的帖子 未经测试的代码

      $args = array(
          'meta_query' => array(
              'relation' => 'OR',
              array(
                  'key' => '_wp_page_template',
                  'value' => 'careers.php',
                  'compare' => '='
              ),
              array(
                  'key' => '_wp_page_template',
                  'value' => 'jobs.php',
                  'compare' => '='
              )
          )
      );
      
      $myposts = get_posts( $args );
      foreach ( $myposts as $post ) : setup_postdata( $post );
          the_content();
          // or add anything else
      endforeach; 
      wp_reset_postdata();
      

      【讨论】:

      • 谢谢!然后我如何将职业内容仅应用于职业模板部分? (其他人也一样)
      • 我编辑了我的答案。像往常一样创建页面,不附加任何模板,然后“我们公司”页面使用我提到的模板(template-our_company.php),并按照上面的说明进行操作
      • 那行不通。我需要这个,以便以后可以添加新页面并为该页面的内容选择特定的模板。这就是为什么我希望每个页面都有一个模板,但能够在组合页面上包含该内容/模板。例如,我可以添加“我们的超级明星”并给它模板“人物布局”。在您当前的答案中,没有办法做到这一点。我必须手动更改组合页面的代码/模板。
      • 页面模板只能以一种方式工作,正如 Codex 所提到的,您的网站页面看起来大致相同 (codex.wordpress.org/Page_Templates) - 它提供了结构,以便页面的内容可以相应地显示跨度>
      • 你的意思是他们只有一种方式?
      【解决方案3】:

      @user3418748 的回答对我来说是一个好的开始,但就我而言,我需要加载特定的页面,我发现仅使用 get_template_part() 本身并不会加载任何内容,因为我是在循环之外执行的。为了让它工作,您需要首先将全局 $post 变量设置为您要显示的页面/帖子。这是我使用的函数(将mytemplate 替换为您的模板名称):

      function mytemplate_show_page($path) {
          global $post;
          $post = get_page_by_path($path);
          $tpl_slug = get_page_template_slug($post->ID);
          $tpl_slug_exp = explode('.', $tpl_slug);
          get_template_part($tpl_slug_exp[0]);
      }
      

      【讨论】:

        猜你喜欢
        • 2013-06-13
        • 2019-10-30
        • 1970-01-01
        • 2022-12-03
        • 2020-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多