retrieve only sub-pages - php

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;
}

Related

List all pages from a category in Wordpress

I'm in the middle of a small crisis here. I need your help! I've done my research for hours but this is my last resort. Cutting to the story, I'm trying to list all the 'pages' from an active category in Wordpress.
Here are the steps I've done so far. I was able to enable categories for my pages by adding the following code to my functions.php file:
function jh_cats() {
register_taxonomy_for_object_type('post_tag', 'page');
register_taxonomy_for_object_type('category', 'page');
}
add_action( 'init', 'jh_cats' );
Next, I needed to make it so that the website has categories & sub-categories. The sub-categories will only show on the category page of the parent. I've added the following to my category.php file to get the list of all my sub-categories.
<?php
$categories = get_categories( array(
'orderby' => 'name',
'parent' => $cat,
'hide_empty' => FALSE
) );
if ($categories)
{
foreach ( $categories as $category )
{?>
<li><?php echo $category->name ?></li>
<?php
}
} ?>
Now, when you visit a specific sub-category (considering nothing will be assigned to the main one), I need to be able to list all the pages that is assigned that sub-category.
What can I do next? I have this in my index.php but this just lists all the pages. Regardless if you're in a specific category or not.
<ul>
<?php
global $post;
$myposts = get_posts( array(
'post_type' => 'page',
'posts_per_page' => 5
) );
if ( $myposts ) {
foreach ( $myposts as $post ) :
setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
<?php the_content(); ?>
</li>
<?php
endforeach;
wp_reset_postdata();
}
?>
</ul>
Hope you can help! Thanks. :)
Credits to replies/code from others over here to help me solve some of the earlier issues.
On the page you wish to filter posts, you can try using $cat = get_the_category($post->id); to return array. Then use a foreach or just get the first item:
$cat_id = $cat[0]->cat_ID;
Then update the WP query to show all posts using that category.
By ID:
$query = new WP_Query( array(
'post_type' => 'page',
'cat' => $cat_id
) );
By name:
$cat_name = get_cat_name($cat_id);
$query = new WP_Query( array( 'category_name' => $cat_name ) );
Source: https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
Have you tried something like this, for your index.php code:
$myposts = get_posts( array(
'post_type' => 'page',
'taxonomy' => 'category'
) );
Also, I would try and avoid "get_categories", it's best to try and use the "get_terms" function whenever you can.

Display child post on parent post page in wordpress

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" */

Show title only if content

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

How to display random child pages of a parent page in Wordpress?

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!

Wordpress display all pages on one page

I need to display all the pages on the one page.
At the moment I am using this to bring in each page:
<?php
$page_id = 5; //id example
$page_data = get_page( $page_id );
$content = apply_filters('the_content', $page_data->post_content);
$title = $page_data->post_title;
echo $content;
?>
But if a new page is created it wont display it as wont have this code and its ID in place..
Is there a way to bring all the pages in automatically?
Any help will be appreciated, thanks
You can get all the pages of your blog by using get_pages();, do this in a loop like so:
$pages = get_pages();
foreach ($pages as $page_data) {
$content = apply_filters('the_content', $page_data->post_content);
$title = $page_data->post_title;
echo $content;
}
The accepted answer's returned array doesn't contain "post customs" (which I needed in my case). And also you may need to retrieve pages with specific attributes. Use args for more customized results:
<?php $args = array(
'sort_order' => 'asc',
'sort_column' => 'post_title',
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'child_of' => 0,
'parent' => -1,
'exclude_tree' => '',
'number' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages($args);
?>
And use them like this:
<?php
foreach ($pages as $page) {
$title = $page->post_title;
echo $title. "<br />";
} ?>
Read more here
Also, if you are using the Twenty Ten theme (I don't know how will it work in others) you can easily get all the pages formatted:
echo "<h1 class=\"entry-title\">".$title."</h1>"
."<div class=\"entry-content\">".$content."</div>";
Well you can get the contents of each page by this code as well.
$content = apply_filters( 'the_content', $content );
Still there is huge problem if you are using custom templates for different pages you can get the content but not the template files on your one page

Categories