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']
Related
I am having some trouble with my WordPress navigation functionality. I have the following function that pulls menu items from the admin:
function cr_get_menu_items($menu_location)
{
$locations = get_nav_menu_locations();
$menu = get_term($locations[$menu_location], 'nav_menu');
return wp_get_nav_menu_items($menu->term_id);
}
In my navigation template, I use this function to pull in only parent items like this:
<?php $nav = cr_get_menu_items('navigation_menu') ?>
<?php foreach ($nav as $link):
if ($link->menu_item_parent == 0) : ?>
<a class="main-nav" href="<?= $link->url ?>"><?= $link->title ?></a>
<?php endif; endforeach; ?>
I tried to make a sub navigation that shows children items like this:
<?php $nav = cr_get_menu_items('navigation_menu') ?>
<?php foreach ($nav as $link):
if ($link->menu_item_parent !== 0) : ?>
<?= $link->title ?>
<?php endif; endforeach; ?>
This pulls in ALL children menu items. The way the navigation I am building should work is: you click on a parent menu item and the sub navigation displays all child menu items for that parent. The hide/display functionality is all JS.
Is there a way to alter the function I have to pull in only children for a specific parent menu item? Any help/guidance is appreciated.
Is there a way to alter the function I have to pull in only children
for a specific parent menu item?
For that purpose, yes, there is.
Try the following function (replace the existing cr_get_menu_items() function):
function cr_get_menu_items($menu_location, $parent = -1)
{
$locations = get_nav_menu_locations();
$menu = get_term($locations[$menu_location], 'nav_menu');
$items = wp_get_nav_menu_items($menu->term_id);
if ( is_numeric( $parent ) && $parent >= 0 ) {
$_id = (int) $parent;
foreach ( $items as $i => $item ) {
if ( $_id !== (int) $item->menu_item_parent ) {
unset( $items[ $i ] );
}
}
}
return $items;
}
Usage examples:
$nav = cr_get_menu_items( 'navigation_menu' ); // Get all menu items.
$nav = cr_get_menu_items( 'navigation_menu', 0 ); // Get menu items whose parent ID is 0
UPDATE
After I re-read your question, this is the function that you likely need:
// $items is the menu items array that you retrieved using `cr_get_menu_items()`,
// or other functions which return valid `nav_menu` items.
function cr_get_submenu_items( array $items, $parent ) {
$parent = (int) $parent;
$list = [];
foreach ( $items as $item ) {
if ( $parent === (int) $item->menu_item_parent ) {
$list[] = $item;
}
}
return $list;
}
UPDATE #2
Here's how you would/could use cr_get_menu_items() along with cr_get_submenu_items():
<?php $nav = cr_get_menu_items('navigation_menu') ?>
<!-- Display parent items. -->
<?php $nav = cr_get_menu_items('navigation_menu') ?>
<?php foreach ($nav as $link):
if ($link->menu_item_parent == 0) : ?>
<a class="main-nav" href="<?= $link->url ?>"><?= $link->title ?></a>
<?php endif; endforeach; ?>
<!-- Display children items. (in its own wrapper `div`/`ul`/etc.) -->
<?php $_ids = []; ?>
<?php foreach ($nav as $link):
$parent = (int) $link->menu_item_parent;
if ( 0 !== $parent && ! in_array( $parent, $_ids ) ) : ?>
<!-- This `div` is just an example wrapper. -->
<div class="menu-<?= $parent ?>-subnav">
<?php foreach ( cr_get_submenu_items( $nav, $parent ) as $clink ): ?>
<?= $clink->title ?>
<?php endforeach; ?>
<?php $_ids[] = $link->menu_item_parent; ?>
</div>
<?php endif; endforeach; ?>
I´m displaying a menu with this code:
<!-- Subcategory menu from current category -->
<ul class="sub-menu">
<?php
if (is_home()) {
wp_list_categories('orderby=id&title_li=&depth=1');
}
else{
$category = get_the_category();
$cat_term_id = $category[0]->term_id;
$cat_category_parent = $category[0]->category_parent;
$listcat = wp_list_categories('echo=0&child_of='.$cat_category_parent.'&title_li=&orderby=order&order=ASC');
$listcat = str_replace("cat-item-".$cat_term_id, "cat-item-".$cat_term_id." current-cat", $listcat);
if ( in_category( $cat_term_id ) || post_is_in_descendant_category( $cat_term_id )) {
echo $listcat;
}
}
?>
and each li of that menu display post titles using this:
<!-- Post list from current category -->
<div class="menu_list">
<ul id="submenu_productos" class="clearfix">
<?php
$IDOutsideLoop = $post->ID;
while( have_posts() ) {
the_post();
foreach( ( get_the_category() ) as $category )
$my_query = new WP_Query('category_name=' . $category->category_nicename . '&orderby=date&order=DESC&showposts=100');
if( $my_query ) {
while ( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<!-- this line to hightlight current post in the category page -->
<li<?php if ( $post->ID == $wp_query->post->ID ) { echo ' class="test"'; } else {} ?>>
<?php the_title(); ?>
</li>
<?php
}
}
}
?>
</ul>
</div>
The menu works well and it shows like this:
The problem is that when I have one post that belongs to that category/subcategory it is also displayed on the list... I want to hide it when it´s only one item:
Is there any way to hide it when the subcategory has only one post?
PD: PLEASE avoid telling me to put this on wordpress.stackexchange.com, I always post there with no answer and here people always help me...
You can count the number of item in your sub menu using :
$count = $my_query->post_count;
I'll suggest to add it in your if condition:
if( $my_query && $my_query->post_count > 1)
That should work
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');
wordpress have function the_category(''); for show all category assigned to te current post , but i need to get current category only child and not show parent .
for example my post have category category parent --> category child and the_category; print :your post cat is : ( category parent , category child )
i neeed parint , your post cat is : (category child) and not show parent .
Use get_the_category function , witch will return all categories asigned to a post ( this means all parents and childs allso ) so you can loop thru them and see witch one is parent and witch one is child and print the one you're trying to get . I suggest you build a function in you're theme functions file.
Update
For example let's say you whant to display the child category name in you're single.php theme file so you would do this:
<?php $child_category = post_child_category(get_the_ID()); ?>
<?php if ( $child_category ) echo $child_category->cat_name; ?>
In order for that to work you need to define post_child_category function in you're theme functions file ( if you look in you're theme directory you'll see a functions.php file , if not then you can create it now ) , so you would add the following :
if ( ! function_exists( 'post_child_category' ) )
{
function post_child_category( $id = null )
{
if ( $id = null )
return false;
$categories = get_the_category( $id );
if ( count($categories) > 0 )
{
return $categories[count($categories)-1];
} else {
return false;
}
}
}
Update
If you whant to display the category link you would do this :
<?php $child_category = post_child_category(get_the_ID()); ?>
<?php if ( $child_category ) : ?>
<a href="<?php echo get_category_link($child_category->cat_ID); ?>" title="<?php echo $child_category->cat_name;?>">
<?php echo $child_category->cat_name;?>
</a>
<?php endif;?>
<ul>
<?php
$blogCategoryID = "5"; // current category ID
$childCatID = $wpdb->get_col("SELECT term_id FROM $wpdb->term_taxonomy WHERE parent=$blogCategoryID");
if ($childCatID){
foreach ($childCatID as $kid)
{
$childCatName = $wpdb->get_row("SELECT name, term_id FROM $wpdb->terms WHERE term_id=$kid");
?>
<li><?php echo $childCatName->name; ?></li>
<?php
}
}
?>
</ul>
take a look at :
http://codex.wordpress.org/Function_Reference/wp_list_categories
this worked for me
$catID=$wp_query->query_vars['cat'];
$args = array('parent' => $catID);
$categories = get_categories( $args );
foreach($categories as $category) {
echo '<li><a href="' . get_category_link( $category->term_id ) . '" ' . '>' . $category->name.'</a> </li> ';
}
I'm drawing out a listing of menu items inside a wordpress template (header.php) and need to assign a special className to the last menu item in the list. I'm building the list with this code...
$myposts = get_posts();
foreach($myposts as $post) :?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
I'd like to add this to the last menu item...
<li class="last">...
You could try this (assuming get_posts() returns an array...)
<?php
$myposts = get_posts();
$last_key = end(array_keys($array));
foreach($myposts as $key => $post) :
?>
<li <?php if ($key==$last_key) echo 'class="last"'; ?>><?php the_title(); ?></li>
<?php endforeach; ?>
You can add following code to functions.php file of your theme
add_filter( 'wp_nav_menu_objects', 'first_last_class_to_top_level_menu' );
function first_last_class_to_top_level_menu( $objects ) {
// add 'first' class to first element of menu items array
$objects[1]->classes[] = 'first';
// find array index of the last element of top level menu
foreach($objects as $i=>$item) if($item->menu_item_parent==0) $last_top_level_menu_index = $i;
// add 'last' class to the last element of top level menu
$objects[$last_top_level_menu_index]->classes[] = 'last';
return $objects;
}