1

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

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

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

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

这可能吗?

4

3 回答 3

2

首先为主页模板选择默认模板并在那里写下你的全局元素。在这个模板中使用 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-slug

于 2014-11-25T15:24:43.303 回答
1

编辑

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

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'); /* Shows the content of the "Careers" page using the slug. */ ?>)。

所以模板文件应该包括这样的内容:

<?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();
于 2014-02-01T16:23:41.550 回答
0

@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]);
}
于 2016-07-04T19:14:10.263 回答