个性化阅读
专注于IT技术分析

如何将页面(及其模板)合并为一页?

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

我知道过去曾经有一个函数get_page, 但是已经过时了(并用不一样的get_post代替了), 但是它不会检索页面的模板。

  1. 我同时需要页面和模板, 因此我可以将它们都输出到主页中。
  2. 我也希望这样, 如果有人在导航菜单中单击以转到”工作”或”团队”, 它将带他们到”我们的公司”页面, 但是带有查询字符串, 因此我可以将其滚动到页面的该部分

这可能吗?


#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示例<section class =” <?php echo $ post-> post_name;?>”>并编写函数以将你的视图页面链接重定向到http:// yoursiteurl /#page- ug


#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), 在其中你将调用该函数(例如<?php show_post(‘careers’); / *显示”职位”页面(* /?>)。

因此, 模板文件应包含以下内容:

<?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'); 
?>

然后在菜单面板中, 只需将导航链接调整为” /我们的公司/#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();

#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]);
}
赞(0)
未经允许不得转载:srcmini » 如何将页面(及其模板)合并为一页?

评论 抢沙发

评论前必须登录!