How to make function to get page ancestors in wordpress - wordpress

How I can get child pages for each page, if a page dont have any child then there must be shown right menu.
For Example:
Parent Page
child page 1
child page 2
if parent don't have any child then display any menu
Right now I am using following code in:
FUNCTION.PHP
function get_top_ancestor_id() {
global $post;
if ($post->post_parent) {
$ancestors = array_reverse(get_post_ancestors($post->ID));
return $ancestors[0];
}
return $post-> ID;
}
HEADER.PHP
<?php
$args = array(
'child_of' => get_top_ancestor_id(),
'title_li' => ''
);
?>
<?php wp_list_pages($args); ?>

This is the default menu calling in wordpress
<?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu', 'menu_id' => 'primary-menu' ) ); ?>

Directly use this function
<?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu', 'menu_id' => 'primary-menu' ) ); ?>

Ahmad, if you just want to list the children of the page that you're currently on, you can use the function wp_get_post_parent_id (http://codex.wordpress.org/Function_Reference/wp_get_post_parent_id)
So, your code might be simplified to
<?php
global $post;
//If the page has children - print the list
if(wp_get_post_parent_id( $post->ID ))
{
$args = array(
'child_of' => wp_get_post_parent_id( $post->ID ),
'title_li' => ''
);
wp_list_pages($args);
}
else{
$args = array(
'child_of' => $post->ID,
'title_li' => ''
);
wp_list_pages($args);
}
?>

Related

WordPress navigation menu is not loading

I am working on my custom theme in wordpress, I have a problem in the navigation in my site, when I open the site, the main navigation is not loading.
Below are the codes.
Any assistance is highly appreciated.
Thanks.
Function.php file
function register_my_menus() {
register_nav_menus( array(
'menu-1' => esc_html__( 'Primary', 'nethub' ),
) );
}
add_action( 'init', 'register_my_menus' );
Then in header.php the code is
<?php
wp_nav_menu(
array(
'theme_location' => 'menu-1',
'menu' => 'primary-menu',
'container' => false,
'items_wrap' => '<ul class="rd-navbar-nav">%3$s</ul>',
)
);
?>
</div>
Have you created a menu in the menus edit screen and set it to the "Primary" location? Also think you can remove the 'menu' => 'primary-menu', from your header.php:
// header.php
<?php
wp_nav_menu(array(
'theme_location' => 'menu-1',
'container' => false,
'items_wrap' => '<ul class="rd-navbar-nav">%3$s</ul>',
));
?>
</div>

Render child page on parent page in Wordpress

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

how to add custom wordpress Menu using just A tag output

I want to create menu like below code but wordpress generate other code.
help me to generate code like below HTML code
Thanks in advance.
help me to generate below code using wordpress
You can do this by using menu walker class like this:
wp_nav_menu( array(
'container' => false,
'menu' => 'main-menu',
'menu_class' => 'nav navbar_menu',
'walker' => new Custom_Walker_Nav_Menu
)
);
add_theme_support('menu');
function register_newtheme_menu(){
register_nav_menus(
array(
'main-menu'=>_('Main Menu')
)
);
}
add_action('init','register_newtheme_menu');
function add_menuclass($ulclass) {
return preg_replace('/<a /', '<a class="navigation-link w-nav-link" ', $ulclass);
}
add_filter('wp_nav_menu','add_menuclass');
?>
<nav class="menu-hamburger w-nav-menu" role="navigation">
<?php
$defaults = array(
'container' => 'false',
'echo' => false,
'fallback_cb' => false,
//'items_wrap' => '<a id="%1$s" class="%2$s">%3$s</a>',
'depth' => 0
);
//echo wp_nav_menu( $defaults );
echo strip_tags(wp_nav_menu( $defaults ),'<a>');
?>
</nav>

get_pages( array( 'child_of' => $post->ID ) does not show all children

I am pretty new to wordpress, and wondering if someone could shed some light on this code.
I am trying to list all sub pages on their parent page, here is the code with some html stripped out:
<?php
$mypages = get_pages( array( 'child_of' => $post->ID ) );
foreach( $mypages as $page ) {
$content = $page->post_content;
if ( ! $content ) // Check for empty page
continue;
$content = apply_filters( 'the_content', $content );
?>
<p style="color: white; text-transform: uppercase;"><?php echo $page->post_title; ?></p>
<?php
}
?>
The code works, and correct sub pages are displayed - but not all of them. The 7 oldest posts are showing, but none of the newest pages that I created this week. I have checked and double checked that all new and old pages are the same in every way - same template, same parent page, same creator, same order number, and all published. Anyone have an idea of what I could be doing wrong?
Try below code:
$args = array('child_of' => $post->ID,'sort_order' => 'desc',
'sort_column' => 'ID',
);
I think you need additional argument as well to get result you are looking for.
$args = array(
'child_of' => $post->ID,
'parent ' => $post->ID,
'hierarchical' => 0,
'sort_column' => 'menu_order',
'sort_order' => 'asc'
);
$mypages = get_pages( $args );
You can check wordpress Doc for its argument and return output.
NOTE If you want to sort pages by date then you can change 'sort_column' => 'menu_order', with 'sort_column' => 'post_date',.
There is other method as well to achieve the same and I prefer below way.
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$mypages = new WP_Query( $args );
if ( $mypages->have_posts() ) : ?>
<?php while ( $mypages->have_posts() ) : $mypages->the_post(); ?>
<p style="color: white; text-transform: uppercase;"><?php the_title(); ?></p>
<?php endwhile; ?>
<?php endif; wp_reset_query(); ?>
You can also use wp_list_pages to render direct HTML.
Try using parent instead of child_of and setting hierarchical to false. According to the docs it appears the default hierarchical value of true can can affect the results.
<?php
$mypages = get_pages( array( 'parent' => $post->ID, 'hierarchical' => 0 ) );
?>
Reference: https://codex.wordpress.org/Function_Reference/get_pages
I have fixed the issue, but I actually have no idea why this works. Any comments about why this did the trick would be awesome.
From my original code, I removed the "if ( ! $content )" section, and they all show up - even though all pages have the same amount of content.
So, in the end, my code reads:
<?php
$mypages = get_pages( array( 'child_of' => $post->ID ) );
foreach( $mypages as $page ) {
$content = $page->post_content;
$content = apply_filters( 'the_content', $content );
?>
<p style="color: white; text-transform: uppercase;"><?php echo $page->post_title; ?></p>
<?php
}
?>
I never had to change the get_pages function parameters at all.

Wordpress: Query child pages with specific templates for each page

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.

Resources