问题
I want the CMS to have the different pages (e.g. "Careers", "Jobs", "Team") with each having its own template, but then to combine them into one big scrollable page (e.g. "Our Company") that would have a template. How would I do this?
I know there used to be a function get_page but that's been deprecated (and replaced with get_post
which is not the same thing), but that doesn't retrieve the page's template.
I want both the page and the template so I can output both into the main page.
I also want it so if someone clicks in the navigation menu to go to "Jobs" or "Team", it will take them to that "Our Company" page, but with a querystring so I can scroll them to that part of the page
Is this possible?
回答1:
First for main page template choose default template and write you global elements there. And in this template use get_template part to include pages
<!--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;
?>
and in career, blog etc pages
<?php
/*
Template name: Career or blog or something else
*/
?>
<?php the_tiele();
the_content();
?>
for
"I also want it so if someone clicks in the navigation menu to go to "Jobs" or "Team", it will take them to that "Our Company" page, but with a querystring so I can scroll them to that part of the page"
assign each pages wrapper to page slug example <section class="<?php echo $post->post_name; ?>">
and write a function to redirect your view page link to http://yoursiteurl/#page-slug
回答2:
EDIT
In order to get one page's content into another use the following function:
function show_post($path){
$post = get_page_by_path($path);
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
and then create a template for the "Our company" page (like template-our_company.php
) in which you will make a call to the function (e.g. <?php show_post('careers'); /* Shows the content of the "Careers" page using the slug. */ ?>
).
So the template file should include something like this:
<?php
show_post('careers');
show_post('jobs');
show_post('team');
?>
For your 2nd question, you need to adjust the template-our_company.php file like this:
<?php
<div id="careers"></div>
show_post('careers');
<div id="jobs"></div>
show_post('jobs');
<div id="team"></div>
show_post('team');
?>
and then in the Menu dashboard, just adjust the navigation link to something like "/our-company/#careers" etc.
EDIT 2
In order to retrieve the content of pages with specified templates in another template, you can do the following: Create the templates (files careers.php and jobs.php) and the posts that will be using those templates
/*
Template Name: Careers
*/
...
/*
Template Name: Jobs
*/
Then in the "parent" template, you can query the posts that have the above specified templates selected untested code
$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's answer was a good start for me, but in my case I needed to load specific pages, and I found that just using get_template_part()
by itself wasn't loading any content because I was doing it outside a loop. In order to get this to work you need to first set the global $post
variable to the page/post you want to display. Here's the function I used (replace mytemplate
with the name of your tempalte):
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]);
}
来源:https://stackoverflow.com/questions/21465176/how-do-i-combine-pages-and-their-templates-into-one-page