I want to hide <h2>title</h2> if they are not children pages.
<h2> title</h2></br>
<?php
// Globalize the $post variable;
// probably already available in this context, but just in case...
global $post;
wp_list_pages( array(
'title_li' => __(''),
// Only pages that are children of the current page
'child_of' => $post->ID,
// Only show one level of hierarchy
'depth' => 1,
) );
?>
You can use the get_children function to get all child contents of your post. If the return array is empty, there are no child pages defined:
<?php
$children =& get_children( 'post_type=page' );
if (!empty($children)) "<h2> title</h2>";
?>
Documentation:
get_children:
https://codex.wordpress.org/Function_Reference/get_children
Post types: https://codex.wordpress.org/Post_Types
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();
}
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" */
I'm trying to create a sidebar navigation that has the current page and it's children displayed (or if it's the child page, it's siblings and the parent page)
I can only get the children or, when on a child, siblings to display.
How can I get
Parent
Child
Child
To display when on either the parent or child Pages? I don't want the parent page's siblings to display and nothing I try seems to work!
Thanks
<?php
//GET CHILD PAGES IF THERE ARE ANY
$children = get_pages('child_of='.$post->ID);
//GET PARENT PAGE IF THERE IS ONE
$parent = $post->post_parent;
//DO WE HAVE SIBLINGS?
$siblings = get_pages('child_of='.$parent);
if( count($children) != 0) {
$args = array(
'depth' => 1,
'title_li' => '',
'child_of' => $post->ID
);
} elseif($parent != 0) {
$args = array(
'depth' => 1,
'title_li' => '',
'child_of' => $parent
);
}
//Show pages if this page has more than one sibling
// and if it has children
if(count($siblings) > 1 && !is_null($args))
{?>
<ul class="pages-list">
<?php wp_list_pages($args); ?>
</ul>
<?php } ?>
I have Created a Taxonomy Archive template for a CTP.
In this page, before I list all the posts of the current taxonomy - I want to list all the child categories of the current taxonomy.
What I have managed to display is the whole list of child categories of this taxonomy, but I need to filter out the 2nd level children.
for example:
-Taxonomy
--child-taxonomy1
--child-taxonomy2
----child of child-taxonomy2
I want to display only "child-taxonomy1" and "child-taxonomy2"
Here's my code so far: http://www.codeshare.io/MQ4YT
Use this code
<ul>
<?php
global $post;
// grab terms of current post, replace taxonomy name
$terms = get_the_terms($post->ID, 'name_of_custom_taxonomy');
// define arguments of following listing function
$args = array (
'child_of' => $terms[0], // current post's (first) category
'title_li' => '', // disable display of outer list item
'taxonomy' => 'name_of_custom_taxonomy' // replace as well
);
// list child categories
wp_list_categories($args);
?>
</ul>
$taxonomy_name = 'product-category';
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$termchildren = get_terms( $taxonomy_name, array( 'parent' => $term_id, 'hide_empty' => false ) );
echo '<ul>';
foreach ( $termchildren as $child ) {
echo '<li>' . $child->name . '</li>';
}
echo '</ul>';
Works like a charm :)
I want to list all sub-pages only one level though of one particular page. I was reading Function Reference/get pages and thought that $pages = get_pages( array( 'child_of' => $post->ID, 'parent' => $post->ID)) ; will do the trick but it is not working. It lists all pages on the same level like the page I call that code from. If I omit parent option I will get all pages even with sub-pages that I want. But I want only sub-pages.
The whole function is like
function about_menu(){
if (is_page('about')){
$pages = get_pages( array( 'child_of' => $post->ID, 'parent' => $post->ID)) ;
foreach($pages as $page)
{
?>
<h2><?php echo $page->post_title ?></h2>
<?php
}
}
}
below are screen shots from wp admin and results. Mine is second one
Screen shot from WP admin http://img687.imageshack.us/img687/6139/e2568e8ec2684e7aa1bb3d1.png and the result http://img269.imageshack.us/img269/2365/329c5097c78f4d3186a177c.png
Check out wp_list_pages(). I think these settings will give you what you want.
<?php
$args = array(
'child_of' => $post->ID, // current page
'depth' => 1, // get children only, not grandchildren
'echo' => 1, // print immediately when we call wp_list_pages
'sort_column' => 'menu_order', // sort by the menu_order parameter
);
?>
<?php wp_list_pages( $args ); ?>
try adding
global $post;
right before you declare $pages.
$paginas = array();
$paginas_obj = get_pages('sort_column=post_parent,menu_order');
$paginas['false'] = "Seleciona pagina";
foreach ($paginas_obj as $pagina) {
$paginas[$pagina->ID] = $pagina->post_title;
}