How can I render a child page on the parent page in wordpress? I'am building a landing page website, and the idea is to use child pages to make landing page structure.
Now I'am using this code in my parent page template:
$args = array(
'post_type' => 'page',
'post_parent' => $post->ID,
'orderby' => 'rand',
'posts_per_page' => 1,
'no_found_rows' => true
);
$child = new WP_Query($args);
var_dump($child->posts);
But it just gives me an array, and I need fully rendered HTML of my child pages.
Thank you in advance)
Try this code here:
$args = array(
'hierarchical' => 0,
'child_of' => $post->ID,
'parent' => $post->ID,
'sort_column' => 'menu_order, ID',
);
$pages = get_pages( $args );
foreach ( $pages as $post ) : setup_postdata( $post );
// child page html content here
endforeach;
//reset to the main page
wp_reset_postdata();
Finally I've found the proper solution, inspired by Matt Browne's answer
functions.php
function eatwings_show_page($pageid)
{
global $post;
$post = get_page($pageid);
$tpl_slug = get_page_template_slug($post->ID);
$tpl_slug_exp = explode('.', $tpl_slug);
get_template_part($tpl_slug_exp[0]);
}
parent-page-template.php:
$args = array(
'post_type' => 'page',
'post_parent' => $post->ID,
'orderby' => 'menu_order, ID',
'posts_per_page' => 1,
'no_found_rows' => true
);
$child = new WP_Query($args);
foreach($child->posts as $childpage)
{
eatwings_show_page($childpage->ID);
}
Related
In creating my theme, I want to show one sticky post in a loop but unfortunately, all sticky posts (there are 5) are displaying. I just want to show 1 or two but I am unable to do so through my coding.
I don't know what I am missing or what I am doing wrong.
<?php
$query = new WP_Query(array(
'post_per_page' => 1,
'post__in' => get_option('sticky_posts'),
'paged' => $paged,
));
?>
To get the last sticky post:
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 1,
'post__in' => $sticky,
'ignore_sticky_posts' => 1
);
query_posts( $args );
if ( $sticky[0] ) {
// insert here your stuff...
}
I need get all posts of site. I try do it in widget, that I made myself, but result is empty.
global $post;
$args = array(
'post_type' => 'post',
'post_status' => 'publish'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ($query->have_posts()) {
$query->the_post();
var_dump($post->ID);
}
}
And when I add in argument array parameter cat then return posts, but I need get all posts from all categories, not just from specified categories.
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => 22
);
Make sure the global $post is in the same function/scope.
If it still not working, instead of var_dump($post->ID) try:
var_dump($query->post->ID)
And don't forget to call wp_reset_postdata() after the while loop.
Try this:
$args = array(
'post_type' => 'post',
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
the_content();
endwhile;
wp_reset_postdata();
endif;
set posts_per_page to -1, this will return all posts from db.
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) {
// do stuff
}
I understood problem, on site (where I working in current time) was installed polylang, and when I specify for function get_posts in arguments 'lang' => pll_current_language() it return all posts for current language.
$results = get_posts(
array(
'numberposts' => 9999,
'orderby' => 'rand',
'order' => 'ASC',
'post_type' => 'post',
'post_status' => 'publish',
'lang' => pll_current_language()
)
);
I created a query to call all child pages of the current parent page. This works great, however each child page has a custom template. I don't know how to add a custom query parameter to account for the template. Currently I query the_content for each child page, however that doesn't account for the template.
Can anyone help me modify the query?
<?php $children = get_pages(
array(
'sort_column' => 'menu_order',
'sort_order' => 'ASC',
'hierarchical' => 0,
'parent' => $post->ID,
'post_type' => 'projects',
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => 'template-city.php', // template name as stored in the dB
)
)
));
foreach( $children as $post ) {
setup_postdata( $post ); ?>
<div class="section-container">
<?php the_content(); ?>
</div>
<?php } ?>
You can use get_post_meta !
With template_redirect action :
function my_page_template_redirect()
{
global $post;
if(get_post_type($post) == 'projects' )
{
$tpl = get_post_meta($post->ID, '_wp_page_template');
if ($tpl) {
include( get_template_directory() . $tpl );
exit();
}
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );
I think get_pages function doesn't use meta_query, you need to do something like this:
$children = get_pages(
array(
'sort_column' => 'menu_order',
'sort_order' => 'ASC',
'hierarchical' => 0,
'parent' => $post->ID,
'post_type' => 'projects',
'meta_key' => '_wp_page_template',
'meta_value' => 'template-city.php',
));
Or use get_posts function that uses meta_query.
I have display all page content in current page but not current page content to be display so bellow code give me all page content but how can i filter it ?
Thanks.
$pages = get_pages();
foreach ($pages as $page_data) {
$content = apply_filters('the_content', $page_data->post_content);
$title = $page_data->post_title;
echo $content;
}
Try this code: it will exclude the current page by taking its ID.
<?php
$args = array(
'post_type' => 'page',
'numberposts' => -1,
'sort_order' => 'ASC',
'sort_column' => 'post_title',
'post_status' => 'publish',
'exclude' => get_the_ID()
);
//$allpages = get_pages($args ); ?>
<?php wp_list_pages( $args ); ?>
I made an Wordpress theme, with pages and posts.
The loop of posts show me a short brief of post and a Continue reading link.
I like this, but how can I make the theme show in the post brief of the loop image(s) attached to post at beginning, if any.
Thank you!
You can get your attached images by using:
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => 1,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_parent' => $post->ID
);
$images = get_posts($args);
and display it like this:
echo wp_get_attachment_image($images[0]->ID, $size='attached-image');
This for getting all attachement images with your post.
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ($attachments) {
foreach ( $attachments as $post ) {
$img = wp_get_attachment_image_src($post->ID, 'medium');
$fullsize = wp_get_attachment_image_src($post->ID, 'full');
}
}
You should add in your loop:
<?php
if(has_post_thumbnail()) {
$theimage = wp_get_attachment_image_src( get_post_thumbnail_id ( $post->ID ), 'thumbnail' );
}
?>
<img class="img_class" src="<?php echo $theimage[0]; ?>" />
Where "thumbnail" correspond to the size you want to show.
Remember that there is also a WordPress specific site in StackExchange