admin管理员组文章数量:1296875
I want to display a page with ID=1149 in a particular place on another page. For that I've tried with this:
<div class="col-md-4 sidebarl">
<?php
$args = array(
'post_type' => 'page',
'post__in' => array(1149)
);
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
}
?>
</div>
But I am not getting anything, an empty page. Am I missing something?
I want to display a page with ID=1149 in a particular place on another page. For that I've tried with this:
<div class="col-md-4 sidebarl">
<?php
$args = array(
'post_type' => 'page',
'post__in' => array(1149)
);
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
}
?>
</div>
But I am not getting anything, an empty page. Am I missing something?
Share Improve this question asked Mar 29, 2021 at 16:27 Dejan DozetDejan Dozet 1135 bronze badges3 Answers
Reset to default 1I am not getting anything, an empty page. Am I missing something?
Yes, you are.
$query->the_post()
does not display anything — it just moves the pointer/key in the posts array ($query->posts
) to the next one, and setups the global post data ($GLOBALS['post']
). So yes, in response to your answer, the_content()
would be what you would use to display the content of the current post in the loop.
But actually, you could simply use the page_id
parameter like so, to query a post of the page
type and having a specific ID:
$args = array(
// Instead of using post_type and post__in, you could simply do:
'page_id' => 1149,
);
Also, you should call wp_reset_postdata()
after your loop ends so that template tags (e.g. the_content()
and the_title()
) would use the main query’s current post again.
while ($query->have_posts()) {
$query->the_post();
the_content();
}
wp_reset_postdata();
And just so that you know, you could also use get_post()
and setup_postdata()
like so, without having to use the new WP_Query()
way:
//global $post; // uncomment if necessary
$post = get_post( 1149 );
setup_postdata( $post );
the_title();
the_content();
// ... etc.
wp_reset_postdata();
I am not sure if this is the correct answer, but I did it this way:
<div class="col-md-4 sidebarl">
<?php
$args = array(
'post_type' => 'page',
'post__in' => array(1149)
);
$query = new WP_Query($args);
while ($query->have_posts()) {
$query->the_post();
the_content();
}
?>
</div>
You can try using the function "get_template_part()". But to do this, you need to create a php-template for your current page and php-template for page 1149. Then, in your current page template, you include another template:
/**
* Template Name: My current page template
* Template Post Type: page
*/
<?php get_header(); ?>
<?php the_content(); ?>
...
<?php
/* include template from page 1149 */
get_template_part( 'page-1149' );
?>
...
https://developer.wordpress/reference/functions/get_template_part/
本文标签: Get page by its ID and display it on another one
版权声明:本文标题:Get page by its ID and display it on another one 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741644597a2390112.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论