If page has children show sub nav - php

I'm creating a wordpress theme and I'm looking to show the sub nav only if the current page has children, I'm using the code below which works great, but doesn't have an if statement, which means it shows an empty menu on some pages.
<?php global $id;
wp_list_pages("title_li=&child_of=$id&show_date=modified&date_format=$date_format");
?>

Try below :-
$kids = get_pages("child_of=$id");
if (count($kids) > 0) { wp_list_pages(...); }

Related

Wishlist item counter in top bar menu of website (YITH Wishlist/WooCommerce)

I want to add an item counter to the wishlist page link in the top bar menu of my site. I tried adding and using a shortcode, but the counter still does not show up in the menu link - instead of the menu link being Wishlist X (where X is the number of items in the wishlist), it's only displayed as Wishlist. Could someone please help me add an item counter to the link? Thanks in advance! 🙂
The code I’m currently using in my functions.php of my child theme:
function tnc_wishlist_counter_cart(){
global $woocommerce;
$wl_items = YITH_WCWL()->get_products();
$count = 0;
foreach ($wl_items as $key => $item) {
$count = $count + 1;
}
return $count;
}
add_shortcode( 'tnc-wishlist-counter', 'tnc_wishlist_counter_cart' );
In the Navigation Label of the top menu item, I have added this:
Wishlist <?php echo do_shortcode('[tnc-wishlist-counter]'); ?>
So with the help of YITH Themes Support, I managed to find the answer! :) Sharing the link in case anyone would need this too

Display categories list in Wordpress without subcategories

I am attempting to show categories in a list, but without displaying subcatgories. My current code is:
<?php if (is_category()) {
$this_category = get_category($cat);
if (get_category_children($this_category->cat_ID) != "") {
echo "<ul>";
wp_list_categories('orderby=id&show_count=0&title_li=
&use_desc_for_title=1&child_of='.$this_category->cat_ID);
echo "</ul>";
}
}?>
Which displays the categories nicely
but when I added a sub-category it looked like this:
Any ideas? Thanks!
Use get_categories() instead.
https://developer.wordpress.org/reference/functions/get_categories/#Get_only_top_level_categories
It has "parent" parameter which you can set to 0 and get desired result.
looks like a css problem to me : you probably want to hide the subcategory item until it's clicked or hovered

How to display sub categories using Collapsing Categories plugin

My site is using the Collapsing Categories plugin (Robert Felty). As a sidebar widget, it is working the way we want it. But now I've written a page, and I'm inserting the php code in the page, and I don't know how to make it display the same Blog sub categories that it displays on any blog post page.
The sub-categories of the blog category are:
- from-the-experts
- grilling-lifestyle
- grilling-tips-and-techniques
But when I add the php code to the page (using the page editor), it outputs all the top level categories, instead:
â–ºBlog (118)
â–ºLamb (1)
â–ºNews (1)
â–ºRecipes (59)
â–ºUncategorized (4)
This is the php code, taken from the readme.txt page:
[php]
echo "<ul class='collapsCatList'>\n";
if (function_exists('collapsCat')) {
collapsCat();
} else {
wp_get_categories('your_options_here');
}
echo "</ul>\n";
[/php]
What I want to know is what I have to put in that code to display only the few sub categories I want displayed.
Answered by plugin author here
$my_options=array(
'inExclude' => 'include',
'inExcludeCats' => 'blog',
'showTopLevel' => false
);
echo "<ul class='collapsCatList'>\n";
if (function_exists('collapsCat')) {
collapsCat($my_options);
} else {
wp_get_categories('your_options_here');
}
echo "</ul>\n";

Wordpress Sub Menu / Show Sister Pages

I am currently using this snippet to show children of the current page.
<?php wp_list_pages('title_li=&child_of='.$post->ID.''); ?>
when I click one of those children pages the sub nav disappears because there are no child pages of the current page, so what I need it to do is continue to show the sister pages of the current page.
What is the best way to do this?
You could request the parent ID instead of the post ID if there is no childs on the current page :
$page_query = new WP_Query();
$all_pages = $page_query->query(array('post_type' => 'page'));
$childs = get_page_children($post-ID, $all_pages);
if(!empty($childs)) {
// display the page childs
wp_list_pages($post->ID);
} else {
// display the page siblings
wp_list_pages(wp_get_post_parent_id($post->ID));
}
get_page_children is used to check if there is any child for the current page - the WP_Query part is needed for get_page_children which required an array of all the pages to look in.

Add submenu to Wordpress theme

I want to add a submenu of a wordpress menu into my theme. I want to use the wp_nav_menu function of Wordpress 3.0. And in other words, I want to see the submenu not the subpages which means that wp_list_pages is not the right function because I want the submenu and not the subpages.
Let's assume the menu structure looks like that:
Home
Entry1
Entry3
Entry4
Entry2
Entry5
Entry6
I want that if someone clicks on Entry1 (and makes it the parent) the Theme just shows the submenu of this entry. In the case of Entry1 it's:
Entry3
Entry4
I know that there is a code like that:
<?php
$children = ($post->post_parent) ? wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0') : wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if($children) { echo('<ul>'.$children.'</ul>'); }
?>
However, the point is that I'm talking about the menu structure and not the page structure. Oh, and the depth parameter does not work because it means to here and not from here.
I think there could be a solution with a custom walker but I don't know how to implement that.
Function reference for wp_nav_menu
http://codex.wordpress.org/Template_Tags/wp_nav_menu
I'm looking for a solution for this problem for so long so please help me. Thanks a lot.
In order to get this to work I had to hide the .sub-menu as soon as the page loaded. Then, show only the relevant sub-menu by targeting ".current_page_item .sub-menu"
$(document).ready(function() {
$(".sub-menu").hide(); // hide the submenu on page load
$(".current_page_item .sub-menu").show();
)};
This should help: From http://www.svennerberg.com/2009/02/creating-a-submenu-in-wordpress/
<?php
$has_subpages = false;
// Check to see if the current page has any subpages
$children = wp_list_pages('&child_of='.$post->ID.'&echo=0');
if($children) {
$has_subpages = true;
}
// Reseting $children
$children = "";
// Fetching the right thing depending on if we're on a subpage or on a parent page (that has subpages)
if(is_page() && $post->post_parent) {
// This is a subpage
$children = wp_list_pages("title_li=&include=".$post->post_parent ."&echo=0");
$children .= wp_list_pages("title_li=&child_of=".$post->post_parent ."&echo=0");
} else if($has_subpages) {
// This is a parent page that have subpages
$children = wp_list_pages("title_li=&include=".$post->ID ."&echo=0");
$children .= wp_list_pages("title_li=&child_of=".$post->ID ."&echo=0");
}
?>
<?php // Check to see if we have anything to output ?>
<?php if ($children) { ?>
<ul class="submenu">
<?php echo $children; ?>
</ul>
<?php } ?>
One solution is to put another wp_nav_menu function on page and to modify css to hide inactive menu items.

Categories