Hello I have a script that displays all categories in wordpress menu but I want to add subcategories to this menu too. How I can check if categories have subcategories then display them in submenu of category.
$items .= '<ul class="sub-menu">';
$categories = get_categories();
foreach ($categories as $category) {
$option = '<li><a href="'.get_category_link( $category->term_id ).'">';
$option .= $category->cat_name;
$option .= '</a></li>';
$items .= $option;
}
$items .= '</ul></li>';
get_categories() only fetches categories and subcategories those are assigned to any post(s) unless you pass array("hide_empty"=>0) as parameter to fetch inactive categories/subcategories too
try below
$items .= '<ul class="sub-menu">';
$categories = get_categories(array("hide_empty"=>0,'parent'=> '0'));
foreach ($categories as $category) {
$childrens = get_categories(array('child_of'=>$category->term_id,"hide_empty"=>0));
$subitems ='';
if(count($childrens)>0){
$subitems .= '<ul class="sub-menu">';
foreach($childrens as $children){
$opt = '<li><a href="'.get_category_link($children->term_id ).'">';
$opt .= $children->cat_name;
$opt .= '</a></li>';
$subitems .= $opt;
}
$subitems.= '</ul></li>';
}
$option = '<li><a href="'.get_category_link( $category->term_id ).'">';
$option .= $category->cat_name;
$option .= '</a>'.$subitems.'</li>';
$items .= $option;
}
$items .= '</ul></li>';
I know its a dirty way. Don't use "hide_empty"=>0 unless you really need it
Related
I do not know php, I am googling for codes, and I came up on something which I need to do in Wordpress site.
This code shows nothing, but neither brokes site, I think its not correct so can someone help me.
here is the code:
$output.= '<div class="column1">'
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories) {
$output .= '<div class="above-entry-meta"><span class="cat-links">';
foreach($categories as $category) {
$output .= ''.$category->cat_name.''.$separator;
}
}
$output .='</span></div>';
$output.= '</div> <!-- end column1 -->';
I want to output current post categories via shortcode, and this is one part of the same.
I need just help on how it goes from this example, I ll be able to continue codding by following example.
try this code
$output = '';
$output .= '<div class="column1">';
$args = array( 'orderby' => 'count', 'order' => 'DESC', 'number' => 5 );
$categories = get_the_category($args);
$separator = ' ';
if($categories) {
$output .= '<div class="above-entry-meta"><span class="cat-links">';
foreach($categories as $category) {
$output .= ''.$category->cat_name.''.$separator;
}
}
$output .='</span></div>';
$output.= '</div> <!-- end column1 -->';
i'd like to create a shortcode for wordpress to display the category name and i found this code :
function categories_list_func( $atts ){
$categories = get_the_category();
if($categories) {
foreach($categories as $category) {
$output .= '<li class="cat-' . $category->cat_ID . '">'.$category->cat_name.'</li>';
}
$second_output = trim($output);
}
$return_string = '<h4>'.__( "Categories :", "my_site").'</h4><div class="overflow"><ul class="post-categories">' . $second_output . '</ul></div>';
return $return_string;
} // END Categories
add_shortcode( 'categories-list', 'categories_list_func' );
But i know nothing in PHP and i'd like to remove everything (the link and "Catégorie :" in h4) except the category name.
I managed to do this with CSS but i'd like a clean php code if possible.
Could you help me please ?
Thanks.
Nicolas.
this is the code you want with link to categories :
<?php
function categories_list_func( $atts ){
$categories = get_the_category();
if($categories) {
foreach($categories as $category) {
$output .= '<li class="cat-' . $category->cat_ID . '">'.$category->cat_name.'</li>';
}
$second_output = trim($output);
}
$return_string = '<ul>' . $second_output . '</ul>';
return $return_string;
} // END Categories
add_shortcode( 'categories-list', 'categories_list_func' );
?>
and this is only the names without any listing and any links
function categories_list_func( $atts ){
$categories = get_the_category();
if($categories) {
foreach($categories as $category) {
$output .= '<li>'.$category->cat_name.'</li>';
}
$second_output = trim($output);
}
$return_string = '<ul>' . $second_output . '</ul>';
return $return_string;
} // END Categories
add_shortcode( 'categories-list', 'categories_list_func' );
or if you want it in another way dont hesitate to tell me :)
I am trying to output Wordpress' navigation menu items along with their CSS Classes.
I was able to get the menu items, but the classes are more complicated. the css classes come in an array but it's already inside a foreach() loop.
$menu_items = wp_get_nav_menu_items($menu->term_id);
$menu_list = '<ul id="menu-' . $menu_name . '">';
foreach ( (array) $menu_items as $key => $menu_item ) {
$title = $menu_item->title;
$url = $menu_item->url;
$classes = $menu_item->classes;
$menu_list .= '<li>';
$menu_list .= '<i class=""></i>';
$menu_list .= '' . $title . '';
$menu_list .= '</li>';
}
$menu_list .= '</ul>';
As you can see, inside the foreach() loop I am getting the $title and $url which are both strings. But then when I try to get the css classes associated with the menu item through $menu_item->classes it returns an array and I am not sure how to do this.
Because each menu item has 3 css classses fa fa-user fa-3x I can return the values using this
$classes[0] . $classes[1] . $classes[2]
But suppose I don't want the third class and I leave it out empty, I get a missing offset error.
I also tried doing a foreach within the original foreach
$menu_list .= '<li>';
foreach($classes as $class) {
$menu_list .= '<i class="'. $class .'"></i>';
}
$menu_list .= '' . $title . '';
$menu_list .= '</li>';
But this returns 3 separate <i> tags for each menu item, which is not what I want. The output looks like this
<i class="fa"></i>
<i class="fa-user"></i>
<i class="fa-3x"></i>
Simply use a nested Loop in place like so:
<?php
$menu_items = wp_get_nav_menu_items($menu->term_id);
$menu_list = '<ul id="menu-' . $menu_name . '">';
foreach ( (array) $menu_items as $key => $menu_item ) {
$title = $menu_item->title;
$url = $menu_item->url;
$classes = $menu_item->classes;
$comboClass = "";
// USING NESTED LOOP CONSTRUCT
foreach($classes as $class){
$comboClass .= $class . " "; // SEPARATE CLASS-NAME WITH A SPACE
}
$menu_list .= '<li>';
$menu_list .= '<i class="' .$comboClass . '"></i>';
$menu_list .= '' . $title . '';
$menu_list .= '</li>';
}
$menu_list .= '</ul>';
You can nest loops...
foreach(...) {
... html stuff ..
echo '<div class="';
foreach($classes as $class) {
echo $class, ' ';
}
echo '">...</div>';
}
I am using this code to post a list of all children terms for my taxonomy called 'location'
<?php
$termID = 10;
$taxonomyName = "location";
$termchildren = get_term_children( $termID, $taxonomyName );
echo '<ul>';
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>
Currently the code is using term id 10 and getting the children for that and displaying in a list.
However I want to add multiple term id's and display a list of all the children of the id's I add.
My id's are 4,6,8,10,14,18,22
Looking for some help to figure out how to do this?
Cheers
I figured out the answer to my own question. using get_terms will get all the terms retured, so there is no need to specify individual id's of my terms.
My code is
<?php
$args = array( 'taxonomy' => 'location' );
$terms = get_terms('location', $args);
$count = count($terms); $i=0;
if ($count > 0) {
$cape_list = '<p class="location-archive">';
foreach ($terms as $term) {
$i++;
$term_list .= '<li><span class="feature">' . $term->name . '</li>';
if ($count != $i) $term_list .= ' · '; else $term_list .= '</p>';
}
echo '<ul class="list">';
echo $term_list;
echo '</ul>';
}
?>
I am using the get_categories() function to manually create myself a nav menu. I have a custom taxonomy I'm using called Category and I'm trying to return the link for it for my tags in the menu using the get_category_link() function.
foreach ($categories as $category) {
if ($category->parent == 0) { //Check to see it is a parent
$output .= '<li>';
$output .= '' . $category->name . ''; //display parent taxonomy category
}
}
But it always returns <a href="">. I can echo out the $category->cat_ID successfully so I know it is passing the ID into the function but I don't know why it's returning blank.
Am I missing something? Is it because these are custom taxonomies? They have slugs.
You need something like this for custom taxonomies:
$tax = 'cars';
$cats = get_terms( $tax, '' );
if ($cats) {
foreach($cats as $cat) {
$output .= "<li>";
$output .= '<a href="' . esc_attr(get_term_link($cat, $tax)) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>' . $cat->name.'</a>';
$output .= "</li>";
}
}
Although you can easily add to the top of the script to get an array of all taxonomies to feed in if you wanted.