Our current website is using custom posts with parent/child posts.
When viewing a (parent) post, a plugin is used to pull its child posts and those are displayed in a tab on the page.
We are using a new version of that custom theme on several websites now and are no longer using parent/child relationship. Instead we have metaboxes in our custom posts types and all additional information can be filed right there.
I wanted to update this particular website with the latest version of the theme, but since it's using parent/child relationship I wanted to add a few lines of code to the theme in order to achieve the same result and keep the old posts the way they are instead of modifying them all.
Here's what I want to do : I want a very simple way to display all child post (in order) on a parent post page.
I've found a few ideas here and there but none seems to be working so far. (Here's one for example : http://www.wpbeginner.com/wp-tutorials/how-to-display-a-list-of-child-pages-for-a-parent-page-in-wordpress/ as well as this one https://wordpress.stackexchange.com/questions/153042/how-to-display-list-of-child-pages-with-parent-in-wordpress). I don't know if it has to do with the fact that i'm using post rather than pages.
I don't want a list of the child post but rather display the content of those directly. Was thinking the best way to achieve this might be to create a function to retrieve the child post and then echoing the result in the template. That way without having to change our theme it could work with our different website.
EDIT :
So far here's what i've tried within single.php :
$query = new WP_Query( array(
'post_parent' => get_the_ID(),
));
while($query->have_posts()) {
$query->the_post();
the_content(); //Outputs child's content as it is
}
wp_reset_query();`
I then changed the code to :
$new_args = array(
'order' => 'ASC',
'post_parent' => get_the_ID()
);
$new_query = new WP_Query( $new_args);
if ($new_query->have_posts() ) {
while($new_query->have_posts() ) {
$new_query->the_post();
the_content();
}
wp_reset_query();
}
and then since it didn't work either i've changed it to :
$children = get_children( array('post_parent' => get_the_ID()) );
foreach ( $children as $children_id => $children ) {
the_title();
the_content();
}
The latest seems to be able to return some results, it "knows" that the current post has children in it but i'm displaying the title and content of the current post. I'm pretty sure I shouldn't be using the_content() in here.
Ok, try something like this inside your post template within the loop. It should help you to output child posts inside particular post.
<?php
/Somwhere in the loop/
$query = new WP_Query( array(
'post_parent' => get_the_ID(),
'posts_per_page' => 3, //shows only 3 children. If you want to show all of them, comment this line
));
while($query->have_posts()) {
$query->the_post();
/*Output the child markup here*/
the_content(); //Outputs child's content as it is
}
wp_reset_query();
?>
UPDATED:
Ok, you can try to use post_parent__in & array of IDs, this should work too.
<?php
/Somwhere in the loop/
$query = new WP_Query( array(
'post_parent__in' => array(get_the_ID()),
'posts_per_page' => 3, //shows only 3 children. If you want to show all of them, comment this line
));
while($query->have_posts()) {
$query->the_post();
/*Output the child markup here*/
}
wp_reset_query();
?>
If not, here is the way to output contents of the posts you got using get_children function. This should probably work too
<?php
$children = get_children( array('post_parent' => get_the_ID()) );
foreach ( $children as $children_id => $child ) {
echo $child->post_title;
echo str_replace( ']]>', ']]>',apply_filters( 'the_content', $child->post_content )); //mimic the_content() filters
//echo $child->post_content; // if you do not need to filter the content;
}
?>
/* Fetch only one level child pages of a parent page */
$pages = get_pages(
array (
'parent' => 'parent_id',
'post_type => 'page',
'post_status' => 'publish',
);
$ids = wp_list_pluck( $pages, 'ID' );
/* Return page IDs in array format */
/* You can retrieve "post_title", "guid", "post_name" instead of "ID"
/* Fetch all child pages of a parent page*/
$pages = get_pages(
array (
'parent' => '-1',
'child_of' => 'parent_id', /* Return child of child for current page id */
'post_type => 'page',
'post_status' => 'publish',
);
$ids = wp_list_pluck( $pages, 'ID' );
/* Return page IDs in array format */
/* You can retrieve "post_title", "guid", "post_name" instead of "ID" */
Related
I have setup an custom post type with the structure below:
Main post
Children post
Sibling post
I have two main posts called "Vrouwen" en "Mannen"
When i visit one of these main posts i would like to only display the siblings.
Im stuck in achieving this.
But then the "children" are also displayed.
I need to go one level deeper somehow.
All help is appreciated!
I tried the code below.
$mysibling = $post->post_parent;
$mychild = $post->ID;
$mychildmysibling = array( $mychild, $mysibling );
$args = array(
'post_parent' => $mychildmysibling,
'post__not_in' => array( $post->ID ),
'posts_per_page' => -1,
'post_type' => 'collectie'
);
$parent = new WP_Query( $args );
while ( $parent->have_posts() ) : $parent->the_post();
But then the "children" are also displayed. I need to go one level deeper somehow.
First of all post_parent expects a number but you set an array. Secondly, you basically need to make it work only for the main pages? So query should look like this:
// find children for the main post
$children = get_children( array('post_parent' => $post->ID));
// check if the post has any children
if ( ! empty($children) ) {
// get all posts where the parent is set as children to main post
$args = array(
'post_parent__in' => array_keys($children),
'posts_per_page' => -1,
'post_type' => 'collectie'
);
$siblings = new WP_Query( $args );
if ( $siblings->have_posts() ) {
while ( $siblings->have_posts() ) {
$siblings->the_post();
echo get_the_title();
}
}
wp_reset_postdata();
}
I have two loops and queries.
At the top of page I have this code. It only shows the latest post from category named "featured":
<?php
$latest_featured_post = new WP_Query ( array ( 'category_name' => 'featured', 'posts_per_page' => 1 ) );
while ($latest_featured_post->have_posts()) : $latest_featured_post->the_post();
?>
Now I want to exclude that post from the other, main, loop on the same page, because I don't want it to show twice. I tried to achieve that by catching the ID of a latest post in a "featured" category and passing it to the 'post__not_in' argument but I did something wrong. This is my code
<?php
$category_id = get_cat_ID('Događaji');
$exlude_latest_featured_post = array($latest_featured_post->ID);
$args = array(
'category__not_in' => array($category_id),
'post__not_in' => $exlude_latest_featured_post,
);
query_posts( $args );
while (have_posts()) : the_post(); ?>
<?php get_template_part('loop/content'); ?>
I tried to manually pass ID of the post ('post__not_in' => array(1337) for example) and it works. Which means that I made mistake with catching the "featured" latest post ID.
I was searching Google for the answer but I didn't find anything helpful. Hope someone here has time and right answer
Thanks
You can capture the featured post id withing the 1st loop via get_the_id function, then use it in the later loop:
<?php
$latest_featured_post = new WP_Query ( array ( 'category_name' => 'featured', 'posts_per_page' => 1 ) );
while ($latest_featured_post->have_posts()) :
$latest_featured_post->the_post();
$featuredID = get_the_id();
?>
Your latter loop:
$category_id = get_cat_ID('Događaji');
$exlude_latest_featured_post = array($featuredID);
When i visit mysite.com/category/*** It only displays last post of that category.
But i want it to display recent ten posts of that category.
In short...
I want my site http://bishwash.com.np/category/entertainment/ to display posts like http://www.onlinekhabar.com/category/bichitra-world/
you can get the recent posts with wp_recent_posts($arg,ARRAY_A );
<?php $args = array(
'numberposts' => 10,
'offset' => 0,
'category' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' =>,
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
You can use get_posts to retrieve posts.
<ul>
<?php
global $post;
$myposts = get_posts('numberposts=5&category=1');
foreach($myposts as $post) :
?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
Change category=1 to your category ID number.If you to get post from XYZ category and XYZ category had 13 ID number then it would be something like this category=13.
In categroy.php page before
<?php if ( have_posts() ) : ?>
//paste the following code
$catID = the_category_ID();
$args = array( 'numberposts' => '10','category' => $catID);
$recent_posts = wp_get_recent_posts( $args );
setup_postdata($recent_posts);
At the end of loop
wp_reset_postdata();
The number of posts is set globally for all index type pages (including the home page and archives and category) in settings, reading
It seems you have limited it to 1 ? Then it's better design to set it to 10 because that will apply to all pages, and design a specific template for the home page, where you limit the number of posts with the methods given to to.
You can achieve that in two ways :
- build a page template, with a specific query, create a page with this template, and define it as homepage in settings -> reading
- create a home.php file in your theme
In both cases, it's better to have a child theme, not to loose modifications when your theme is updated.
Create the template (sample.php) and write code like
$catquery = new WP_Query( 'cat=3&posts_per_page=10' );
or use CATEGORYNAME
Note: When we click the link it redirects to to the sample.php and the category name is dynamic that means when we click the category its automatically stored to the CATEGORYNAME
I am trying to display random child pages of a parent page, but I get random posts which I dont want to be included in the display area.
$my_query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1', 'pagename=guide') );
So, what I want is to display random child pages of a parent page who slug is guide, but instead I get random posts which is quite different from what I want. Any help would be appreciated. Thanks :)
This is working for me. the post_type is the important part since otherwise it doesn't seem WP will query against the pages. The post_parent should be the integer id of your page.
$query = new WP_Query( array( 'orderby' => 'rand', 'posts_per_page' => 1, 'post_type' => 'page', 'post_parent' => '2' )) ;
if ( $query->have_posts() ) {
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
This worked for me:
$my_wp_query = new WP_Query(); // Setup query object
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page')); // Get all pages
$children = get_page_children( get_the_id(), $all_wp_pages); // Get the children pages from list of all pages
$rand = rand(0, (count( $children) - 1)); // Count children and get a random number from that range
print_r( $children[$rand]); // Print random child object
Hope this helps!
I'm trying to Loop over a bunch of pages under a certain taxonomy. The looping part works great, and I get all of the pages I need (nicely wrapped in WP_Post objects).
However, now I'm facing a different kind of problem. I want to include the page's thumbnail as set in the editor. I've tried any combination of get, the, thumbnail, featured, image, _, -, I could think of, to no avail.
The WP_Post object is rather new, and documentation is lacking.
Can anyone shed light on this mystery? My goal is to eventually show a bunch of <figure> elements, containing an image, a title, and a short description of each object.
The following is just a proof of concept in form of shortcode. It dumps a code block with all posts that have a Featured Image.
Function reference: has_post_thumbnail, get_the_post_thumbnail
add_shortcode( 'all-post-thumbs', 'so_14007170_dump_post_thumbs' );
function so_14007170_dump_post_thumbs( $atts, $content )
{
// Query
$posts = get_posts( array(
'post_type' => 'post',
'numberposts' => -1,
'post_status' => 'publish'
) );
// Build an array of post thumbnails
$thumbs = array();
foreach( $posts as $post)
{
if( has_post_thumbnail( $post->ID) )
$thumbs[] = array( $post->post_title, htmlentities(get_the_post_thumbnail( $post->ID ) ) );
}
// Build output and return
$echo = '<pre>'. print_r( $thumbs, true ) . '</pre>';
return $echo;
}
Result in frontend:
Posts with featured image:
Not sure what do you want but if you want to get all images of a certain page then you can use
$parent='your page id';
$args=array(
'post_parent' => $parent,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC',
'numberposts' => -1
);
$images = get_children($args);
You can paste this code in your loop and if you provide the appropriate page_id as parent then you'll get all images as an array in the $images and can run a loop.
Read more at Codex.
Update:
To get only the featured image you can use
echo get_the_post_thumbnail('page id here', 'thumbnail');
Read more at Codex.
if ( have_posts() ) : while ( have_posts() ) : the_post();
// stuff before thumbnail
$thumbnail_args = array();
// insert whatever thumbnail args you want
echo get_the_post_thumbnail();
// stuff after thumbnail
endwhile; else:
echo "<h2>Sorry, nothing to see here.</h2>";
endif
Unfortunately, the WP_Post methods are named really poorly. Most methods which interact with the Post need to have some arrangement of '_' and 'post' added to them.