I have a function below, list_child_sibling_pages, which returns a string of 'hidden' when the initial 2 if / ifelse statements are false.
I need to put the literal return value 'hidden' in to the array below as shown here
array( 'classname' => 'custom child-parent-menu-container '. $hidden, 'description' => 'Automatically add a child page menu to parent pages. Automatically add a sibling menu to child pages' );
I know I can output it with with
echo list_child_sibling_pages();
But I don't know how I would work that in to the code below. The value is returned fine if I just echo it out on it's own my only issue is echoing it where I need it
Relevant code follows
function tmm_child_parent_menu() {
function list_child_sibling_pages()
{
global $post;// if outside the loop
$post_type = '&post_type='.get_post_type();
$children = get_pages('child_of='.$post->ID.$post_type);
$parent_title = get_the_title($post->post_parent);
$parent_permalink = get_permalink($post->post_parent);
//child pages
if ( $post->post_parent && !is_search() && !is_404() )
{ ?>
<h4><?php echo ''.$parent_title.''; ?></h4>
<ul class="parent-page-children">
<?php wp_list_pages( 'title_li=&child_of='.$post->post_parent.'&sort_column=menu_order'.$post_type ); ?>
</ul>
<?php
}
//parent pages
elseif ( count( $children ) != 0 && !is_search() && !is_404() )
{ ?>
<h4><?php the_title(); ?></h4>
<ul class="parent-page-children">
<?php wp_list_pages( 'title_li=&child_of='.$post->ID.'&sort_column=menu_order'.$post_type ); ?>
</ul>
<?php
}
else
{
return 'hidden';
}
}
$hidden = list_child_sibling_pages();
/* Widget settings. */
$widget_ops = array( 'classname' => 'custom child-parent-menu-container '. $hidden, 'description' => 'Automatically add a child page menu to parent pages. Automatically add a sibling menu to child pages' );
confused but i think you want
$array=array('classname'=>'custom child-parent-menu-container '.list_child_sibling_pages(),
'description'=>'Automatically add a child page menu to parent pages. Automatically add a sibling menu to child pages');
Related
I have a script that is displaying child page list on parent page and a list o child pages without the current child page.
I would like to add the parent page on child pages, but I don't know how to achieve it. Could you please help me out?
This is my code:
if ('page-parent') {
echo '<h3>See also</h3>';
// if we are on a parent page set the $parent variable to current post id
// otherwise set $parent variable to current post parent
$parent = $post->post_parent == 0 ? $post->ID : $post->post_parent;
// if we use current post parent as $parent, exclude the current page
$exclude = $parent == $post->post_parent ? $post->ID : false;
// get all the children
$args = array( 'parent' => $parent, 'sort_column' => 'menu_order' );
if ( $exclude ) $args['exclude'] = $exclude;
$child_pages = get_pages($args);
// show only if there are children
if ( ! empty($child_pages) ) {
global $post;
foreach ( $child_pages as $post ) { setup_postdata( $post );
?>
<div class="child-thumb">
<?php the_title(); ?>
</div>
<?php
}
wp_reset_postdata();
}
}
You need to use 'child_of' argument instead of 'parent'. 'parent' in get_pages means to fetch or not to fetch parental pages.
But your case needs to fetch children of given page.
So you must use
$args = array( 'child_of' => $parent,
'sort_column' => 'menu_order' );
To display one single parent page above children, you can use get_post function:
if ($post->post_parent>0){
$parent_page=get_post($post->post_parent);
echo '<div>'.$parent_page->post_title.'</div>';
}
if ( ! empty($child_pages) ) {
///and so on...
For more information: https://codex.wordpress.org/Function_Reference/get_pages
I am working with a hierarchy of pages and want to list the parent & children as a sidebar index (with current page bolded). It's sort of working, but the PHP script I've come up with is using the main page heading to create the link text. These headings are very long for SEO purposes, so I'd like to replace them with the ACF 'subtitle' field that I'm already using. Here's the project before I begin to explain:
https://newstart.staging.wpengine.com/treatment/mental-health/depression
In This Section shows the script-generated list first with the obnoxiously long titles. The shorter titles (beginning with "Mental Health") were hard coded as placeholders, and that is what I would like the script to end up looking like. For example:
Depression is a Huge Risk Factor for Substance Abuse
(should be...)
Depression
How can I replace these titles with the subtitles? The get_field value in ACF is subtitle
Here is what the PHP looks like now:
<?php
//---- GET THE PAGE'S ANCESTORS
global $post;
$anc = get_post_ancestors( $post->ID );
//---- IF $ANC HAS MORE THAN 1 VALUE
//---- THEN WE HAVE A 3RD LEVEL CHILD PAGE
//---- AND THUS WANT THE PARENT ID
$top_id = $post->ID;
if (count( $anc ) > 1 ){
$top_id = $anc[0];
}
//---- TAKEN FROM WP CODEX
$ancestor_id = $top_id;
//VERY IMPORTANT, THE ORDER OF THIS ARRAY
//ENSURE THE SORTING MATCHES IN WP_LIST_PAGES BELOW
$descendants = get_pages(array(
'child_of' => $ancestor_id,
'sort_order' =>'ASC',
'sort_column' => 'menu_order',
));
$incl = "";
foreach ($descendants as $page) {
if (($page->post_parent == $ancestor_id) ||
($page->post_parent == $post->post_parent) ||
($page->post_parent == $post->ID))
{
$incl .= $page->ID . ",";
}
}
//---- MAKE A FINAL ARRAY FOR USE
//---- AS THE PREV//NEXT NAVIGATION
$pages_array = explode( ',', $incl );
array_pop($pages_array); //pop off last empty value
array_unshift( $pages_array, $ancestor_id); //add the top level page
// echo '<pre>';
// print_r($pages_array);
// echo '</pre>';
?>
<?php
//FIND THE NEXT ITEM
//IN THE PAGES ARRAY
//AFTER THE CURRENT PAGE
$curr_key = array_search($post->ID, $pages_array);
$curr_key++;
?>
<?php if( $curr_key < count($pages_array) ){ ?>
<?php
$id = $pages_array[$curr_key];
$title = get_the_title( $id );
$post = get_post( $id );
?>
<div class="col-md-2 hidden-sm hidden-xs section-index">
<h6>In This Section</h6>
<ul>
<?php
wp_list_pages(array(
"include" => $ancestor_id,
"link_before" => "",
"title_li" => "",
));
wp_list_pages(array(
"child_of" => $ancestor_id,
"include" => $incl,
"link_before" => "",
"title_li" => "",
'sort_order' =>'ASC',
'sort_column' => 'menu_order',
));
?>
</ul>
I come from a design background so I am not good with PHP syntax yet. Most of this is inherited code that I only vaguely understand. Any help would be appreciated!
I was working on a site with categories as primary menu .I have a new post type and added an archive content as menu item with label 'City Guide'.Now when I on City Guide page the menu item is not highlighting.Or with code the class 'current' is not printing in the menu item class.Here is my code:
<?php
$menu_name = PRIMARY_NAVIGATION_NAME;
$menu_items = array();
if( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ){
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menu_items = $menu ? wp_get_nav_menu_items($menu->term_id) : array();
}
?>
<nav class="visible-sm visible-md visible-lg" role="navigation">
<ul id="menu-primary-navigation" class="main-navigation">
<?php if( !empty($menu_items) ): ?>
<?php
$current_category_object = null;
if( !empty($wp_query->query_vars['category_name']) ){
$category_name = $wp_query->query_vars['category_name'];
$current_category_object = get_category_by_slug($wp_query->query_vars['category_name']);
}
?>
<?php foreach( $menu_items as $item): ?>
<?php
$active_class = false;
if ($item->object == 'category' && !empty($current_category_object) ){
$cat_object = get_category($item->object_id);
if ($cat_object->term_id == $current_category_object->term_id || $cat_object->term_id == $current_category_object->parent) {
$active_class = true;
}
}
?>
<li class="<?php echo sanitize_title($item->title); ?><?php echo ' menu-item-object-category'; if ($active_class) echo ' current'?>">
<a href="<?php echo $item->url; ?>" class="main-category"<?php if( $item->target) echo ' target="' . $item->target . '"'; ?>><?php echo $item->title ?></a>
<?php if( $item->object == 'category'): ?>
<?php
$sub_categories = get_terms( array('category'), array('child_of' => $item->object_id, 'hide_empty' => 0) );
if (count($sub_categories) <= 4) {
include(locate_template('partials/sub-menu-two-featured.php'));
} else {
include(locate_template('partials/sub-menu-one-featured.php'));
}
?>
<?php endif; ?>
<?php if ($item->title == 'City Guide'): ?>
<?php include(locate_template('partials/sub-menu-city-guide.php')); ?>
<?php endif; ?>
</li>
<?php wp_reset_query(); ?>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</nav>
How can i implement it into it using a condition to add current for my archive page menu item in the above code .
I tried
<?php if ($item->title == 'City Guide'): ?>
but it was echoing in all instance not when the menu is active.
Please help.
One possible solution is to use get_queried_object() function for this.
Codex says
if you're on a single post, it will return the post object
if you're on a page, it will return the page object
if you're on an archive page, it will return the post type object
if you're on a category archive, it will return the category object
if you're on an author archive, it will return the author object
If you start using $qo = get_queried_object() then no need to use this code
if( !empty( $wp_query->query_vars['category_name'] ) ){
$category_name = $wp_query->query_vars['category_name'];
$current_category_object = get_category_by_slug(
$wp_query->query_vars['category_name']
);
}
because:
If you are in category-archive page, $qo will contain category object, and $qo->term_id and $qo->parent will be available.
If you are in CPT archive page, then $qo will contain CPT object, and properties like CPT name, query_var, label and so on will be available.
So when you have custom queried object and with the help of some conditional tags like is_tax(), is_tag(), is_category(), is_post_type_archive() you will be able to determine exactly where you are.
P.S. You must check the codex first, because there have some notes for precedence when using get_queried_object()
<?php
$active_class = false;
$this_category = get_queried_object();
$this_category->name;
if ($this_category->name == 'city-guide' ){
$active_class = true;
} ?>
<li class="<?php echo sanitize_title($item->title); ?><?php echo ' menu-item-object-category'; if ($active_class ==1 && sanitize_title($item->title) =='city-guide') echo ' current'; ?>">
this worked thanks #pgk for pointing me out
I want to show the parent menu item above the submenu but when there is no submenu the parent menu item must not be showing then.
Now i use this code (page.php):
<div class="left">
<h2>
<?php
$parent_title = get_the_title($post->post_parent);
echo $parent_title;
?>
</h2>
<?php wp_nav_menu( array('container_class' => 'Hoofdmenu','theme_location' => 'Hoofdmenu','sub_menu' => true) ); ?>
<?php get_sidebar(); ?>
</div>
Output:
TITLE PARENT MENU
Subitem
Subitem
Subitem
------------
Widgets and stuff not important.
But with this code the parent menu item (title) is always visible even when there is no submenu...
Looks like what you need is a combination of checking if the page is a parent or not and then also seeing if it has children.
Something like this should work.
<?php
if ( is_page($post->ID) && $post->post_parent ) {
$children = get_pages('child_of='.$post->post_parent);
} else {
$children = get_pages('child_of='.$post->ID);
}
?>
<?php if($children):?>
<h2>
<?php
$parent_title = get_the_title($post->post_parent);
echo $parent_title;
?>
</h2>
<?php endif;?>
I modified a function from a similar question to get the parent menu title:
Getting the current menu item id from specific menu
function wpse16243_wp_nav_menu_objects( $sorted_menu_items )
{
foreach ( $sorted_menu_items as $menu_item ) {
if ( $menu_item->current ) {
$GLOBALS['current_menu_title']=$menu_item->title;
$GLOBALS['current_menu_ID'] =$menu_item->menu_item_parent;
break;
}
}
foreach ( $sorted_menu_items as $menu_item ) {
if ( $menu_item->ID==$GLOBALS['current_menu_ID'] ) {
$GLOBALS['parent_menu_title'] =$menu_item->title;;
break;
}
}
return $sorted_menu_items;
}
Just use the variable $GLOBALS['current_menu_title']
<?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) {
$parent_title = get_the_title($post->post_parent);?>
<li><?php echo $parent_title;?></li>
<?php echo $children; ?>
<?php } ?>
The code above lists the parent and all child pages in a list.
Parent Page
Child Page
Child Page class="active"
Child Page
Child Page
I would like to add a class of "active" to the currently active page. Any help is greatly appreciated. Thanks
To look for a specific page and add an active class to it, you can try using is_page and define the URL/slug of the page.
<a class="<?php if (is_page('name-of-page')) echo 'active'; ?>" href="#">Link</a>
You can easily add active and other classes by checking the $post->post_title against the $item->title
function addLinkClassesWithActive( $atts, $item, $args ) {
global $post;
// check if the item is in the primary menu
if( $args->theme_location == 'main-nav' ) {
// add the desired attributes:
$atts['class'] = $post->post_title === $item->title ? 'mdl-layout__tab is-active' : 'mdl-layout__tab';
}
return $atts;
}
add_filter( 'nav_menu_link_attributes', 'addLinkClassesWithActive', 10, 3 );
I am using this myself and stripping out the wrapping container, the ul, and the li tags so that I have just a link. See example below.
<nav role="navigation" class="mdl-navigation mdl-layout--large-screen-only" itemscope itemtype="http://schema.org/SiteNavigationElement">
<div class="mdl-tabs mdl-js-tabs mdl-js-ripple-effect">
<?php
$primaryMenu = array(
'container' => false,
'theme_location' => 'main-nav',
'items_wrap' => '%3$s',
'echo' => false,
);
echo strip_tags( wp_nav_menu( $primaryMenu ), '<a>' );
?>
</div>
</nav>