List a child's sibling pages in Wordpress - php

I'm build a website for a hotel and in my sidebar file I have a PHP script to print out the list of child pages. The structure is as follows:
Restaurant (Breakfast, Lunch, Dinner)
Rooms (Standard, Double, Luxury)
So, essentially I want to display the child (ones in the brackets) pages in the sidebar when a user is on the parent page (Restaurant or Rooms) or a child page.
However, my script doesn't appear to work and doesn't display the list of pages.
//SET UP FAMILY
$children = get_pages('child_of='.$post->ID);
$parent = $post->post_parent;
$siblings = get_pages('child_of='.$parent);
if(count($children) != 0 ) {
$args = array(
'depth' => 1,
'title_li' => null,
'sort_column' => 'menu_order',
'child_of' => $post->ID
);
}
else if($parent != 0) {
$args = array(
'depth' => 1,
'title_li' => null,
'sort_column' => 'menu_order',
'exclude' => $post->ID,
'child_of' => $parent
);
}
if(count($siblings) > 1 && !is_null($args)) {
echo "<ul class='pages'>";
echo "<h2>Browse</h2>";
wp_list_pages($args);
echo "</ul>";
}
I don't mind if the current page is shown in the menu either.

Related

Wordpress Display Current page and parent/children

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

wordpress get_categories count also custom post type

on my home page i want to present how many posts are for each category. so i use get_categories->count for that. but i have a news custom post type. each news item is associated with a category. so how can i exclude the custom post type from the count?
here is the code i have:
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'exclude' => array(1,8),
'include' => '',
'number' => '',
'taxonomy' => array('category'),
'pad_counts' => true
);
$categories = get_categories( $args );
then i use within the loop: echo $category->count
If I understood your question correctly, this should help you out. The following code will list all of your post categories with their respective post counts, omitting the News CPT posts from the count total.
<?php $cats = get_categories(); // Get categories ?>
<?php if ($cats) : ?>
<ul>
<?php // Loop through categories to print name and count excluding CPTs ?>
<?php foreach ($cats as $cat) {
// Create a query for the category to determine the number of posts
$category_id= $cat->term_id;
$cat_query = new WP_Query( array( 'post_type' => 'post',
'posts_per_page' => -1,
'cat' => $category_id
) );
$count = $cat_query->found_posts;
// Print each category with it's count as a list item
echo "<li>" . $cat->name . " (" . $count . ")</li>";
} ?>
<?php wp_reset_query(); // Restore global post data ?>
</ul>
<?php endif; ?>

Wordpress navigation for grandchildren

I'm currently using this code (per the codex) to show children on parent pages, and children of the parent pages on their children:
<?php if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>
<ul>
<?php echo $children; ?>
</ul>
<?php } ?>
I would like to add that if on a secondary child page (child's child) to then show it's parent and parent's siblings.
Thank you for your help! :D
<?php
if($post->post_parent)
{
//get the parent post
$parent = get_post($post->post_parent);
//check to see if we have a grandparent
if($parent->post_parent)
{
$page_list = wp_list_pages( array( 'child_of' => $parent->post_parent, 'echo' => false, 'depth' => 1 ) );
}
else
{
$page_list = wp_list_pages( array( 'child_of' => $post->post_parent, 'echo' => false, 'depth' => 1 ) );
}
}
else
$page_list = wp_list_pages( array( 'child_of' => $post->ID, 'echo' => false, 'depth' => 1 ) );
if ($page_list) {
?>
<ul>
<?php echo $page_list; ?>
</ul>
<?php } ?>
This will check if the post has a parent and then if that post has a parent. The $page_list should be a list of pages of the parent and its siblings. the 'depth' => 1 tells WordPress to only get one level of pages. This will stop it from getting the children of those pages

Get an array of child posts IDs, wordpress 3.0, php

Ok here's one for ya...
On a custom template I'm using this code to retrieve & display a list of child pages/posts
$args = array(
'depth' => 1,
'show_date' => '',
'date_format' => get_option('date_format'),
'child_of' => $post->ID,
'exclude' => '',
'include' => '',
'title_li' => '',
'echo' => 1,
'authors' => '',
'sort_column' => 'menu_order, post_title',
'link_before' => '',
'link_after' => '',
'walker' => '' );
wp_list_pages( $args );
This works great, I'm also wondering how I can access/create an array of child post ID's. My goal is to access some custom fields meta data through the get_post_meta() function of each child post using it's ID.
Thanks guys.
I guess I wasn't very clear with this one as it's the first time I've never recieved an answer from SO.
I managed to find the information I needed and will place it here for anyone else browsing with the same request.
ok - To get all child IDs..
$pages = get_pages('child_of=X');
foreach($pages as $child) {
// Now you have an object full of Children ID's that you can use for whatever
// E.G
echo $child->ID . "<br />";
}
If you want to build an array of post ids for later use you can do this:
$pageids = array();
$pages = get_pages('child_of=X');
foreach($pages as $page){
$pageids[] = $page->ID;
}
And you have a clean array of just page ids.
$children = get_posts('post_parent=SLUG_OF_PARENT_POST&post_status=publish');
foreach($children as $child)
{
echo '<br/>ID:'.$child->ID;
}
you can use other attributes (i.e. $child->post_content)...
if you need to define post_type, then add this argument too : &post_type=POST_TYPE_NAME
Another way to do this:
$my_page_id = 12345;
$child_query_args = array(
'post_parent' => $my_page_id,
'post_type' => 'page',
'posts_per_page' => -1,
'fields' => 'ids',
);
$child_query = new WP_Query($child_query_args);
if ( $child_query && $child_query->have_posts() && $child_query->posts ) {
// (Since fields=ids, $child_query->posts is just an array of IDs)
$child_ids = $child_query->posts;
foreach ( $child_ids as $child_id ) {
$whatever = get_post_meta( $child_id, 'whatever', true );
echo esc_html($whatever);
}
}

retrieve only sub-pages

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

Categories