I am working on a site that calls the categories of a wordpress page and displays them in the right-side navigation using a php call. I am new to php and web programming in general. Is there a way I could split the categories into two sections using a particular php call or perhaps an if-loop.
Essentially, I want to display particular categories under custom sub-headings to better organize the site. Any help, I'm currently using the following script to display my categories:
<ul><?php wp_list_categories('show_count=1&title_li='); ?></ul>
Here is my site for reference: http://www.merrimentdesign.com
Try using your code above twice. Each time, you can use the other function arguments to limit the output to certain categories. See http://codex.wordpress.org/Template_Tags/wp_list_categories for the various ways to customize the output of the function.
For example you could use:
<ul><?php wp_list_categories('show_count=1&title_li=&child_of=100'); ?></ul>
// where 100 is the parent id of all of the categories you want to print.
<ul><?php wp_list_categories('show_count=1&title_li=&exclude_tree=100'); ?></ul>
// and then show everything, but children of 100
Or simply use the first string multiple times specifying different parent ids each time.
By far and away your best option is to use the new menu functionality within WordPress. It's dead straight forward to set up in your theme:
add_theme_support( 'menus' );
add_action( 'init', 'register_my_menus' );
function register_my_menus() {
register_nav_menus(
array(
'public-menu' => __( 'Public Menu' ),
'sidebar-public-menu' => __( 'Sidebar Public Menu' ),
'sidebar-members-menu' => __( 'Sidebar Members Menu' ),
'sidebar-staff-menu' => __( 'Sidebar Staff Menu' ),
'footer-menu' => __( 'Footer Menu' )
)
);
}
place that in your functions.php file (and obviously change it for your requirements).
Then in your template file - probably sidebar.php you'll want something like:
<?php wp_nav_menu( array( 'theme_location' => 'sidebar-staff-menu', 'container' => false ) ); ?>
And then go to the back end of WordPress (your wp-admin) and then go to Appearance > Menus and voila you're able to drag and drop your categories to your heart's content!
Helpful link: http://justintadlock.com/archives/2010/06/01/goodbye-headaches-hello-menus
Read that, Justin Tadlock is awesome.
Good luck.
Related
i'm using 3 widget areas in my child theme's footer.
i created a secondary custom theme successfully, and am hoping i can simply change those 3 widget area's out in the second theme.
"Footer - Column 1" would change to "Alt Footer - Column 1"
"Footer - Column 2" would change to "Alt Footer - Column 2"
"Footer - Column 3" would change to "Alt Footer - Column 3"
(and yes, i've already created in WP admin > Appearnace > Widgets, those 3 new custom widget areas, & put temp text widgets in them for now).
i'm using this in "functions.php" to change the "Menu"...
add_filter( 'wp_nav_menu_args', 'respect_menu_swap' );
function respect_menu_swap( $args = '' )
{
if(is_page_template('template-respect.php') && $args['menu_id'] == 'avia-menu')
{
$args['menu'] = '16';
}
return $args;
}
...am hoping for something similar to change each of those "widget areas".
i've searched, read, searched, & read a LOT trying to figure this out & just can't seem to grasp what should go in place of the menu terms in that code bit. i really struggle with PHP, so would greatly appreciate specific explanation & code:-)
and, i'm saying "Widget Area" instead of "Widgets" because i realized that "Widgets" are INSIDE the "Widget Areas". i'd like to swap the whole area instead of just the widget so my people can add/remove various widgets in those 3 "areas" in the WP > Appearance > Widgets admin page as needed. it’s my understanding that if i just use the “Widget ID” then when someone changes which widget(s) are in one of those widget areas, it won’t update on the sites front-end without me changing those ID’s first. i’d like to avoid having to do that if possible.
(BTW: i'm using the Enfold WP theme, if that matters)
WOW!
well, it would seems as tho i figured it out!
after MORE searching, i came across https://learn.wordpress.org/lesson-plan/widget-areas
apparently i had to "register" a "sidebar" - which thru me, as i was wanting to modify in the footer. after looking that up, the term "sidebar" is used anywhere the theme allows the user to add widgets in. and since the Enfold theme footer allows for widgets, i had to "register_sidebar" and THEN modify a line in the template.
i also didn't understand why i had to "register" the "widget area's" at all, thinking that just by creating the custom widgets in WP admin > Appearance > Widgets, that should do it, but no. ya gotta create them there and ALSO register them in the child functions too.
so after reading that wordpress page (linked above), i started hunting around in the main Enfold theme files, trying to find something similar. since apparently, some things are named differently than in other themes, which is why my copy/pasting of examples i found elsewhere didn't work.
i found it in... themes > enfold > includes > admin > register-widget-area.php
so i combined what that wordpress link offered with the footer widget registration and came up with this code that i put in my child functions.php...
add_action( 'widgets_init', 'MYWIDGETAREA' );
function MYWIDGETAREA()
{
register_sidebar( array(
'name' => 'Respect Footer - Column ' . $i,
'id' => 'espect_foot_widget_' . $i,
'before_widget' => '<section id="%1$s" class="widget clearfix %2$s">',
'after_widget' => '<span class="seperator extralight-border"></span></section>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>'
));
}
in the array, i only changed the "name" & "id", leaving the rest so the structure matches the original theme.
then i went to my template-custom.php and found this line...
if( ! ( function_exists( 'dynamic_sidebar' ) && dynamic_sidebar( 'Footer - Column ' . $i ) ) )
...and replaced it with...
if( ! ( function_exists( 'dynamic_sidebar' ) && dynamic_sidebar( 'Respect Footer - Column ' . $i ) ) )
AMAZING!!!
well hopefully all this may benefit someone else in a similar position some day.
If you just want a different widget area for each page template, you can just register a widget area for each one, since the page template would contain it.
But, if you're using the same footer file on multiple page templates and want to load different widget areas dynamically, you would first have to register widget areas with the page template slugs included in the ID, for example:
function custom_register_widget_area() {
register_sidebar( array(
'name' => 'Template Respect Footer',
'id' => 'template-respect-footer',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'custom_register_widget_area' );
Adding another register_sidebar() for each template.
Then in the footer file (using template-respect.php from your example) you can get the basename of the template to build the widget area ID:
$template = basename( get_page_template_slug( get_the_id() ), '.php' );
if ( $template && is_active_sidebar( $template . '-footer' ) ) {
dynamic_sidebar( $template . '-footer' );
} elseif ( is_active_sidebar( 'default-footer' ) ) {
dynamic_sidebar( 'default-footer' );
}
This would load whichever widget area matches the page template slug, or the default if the default page.php is being used.
I am looking to add tow menus in wordpress theme using hooks. One menu should show on when members logged in my system and second menu will show when members are logout from system.
I am using below code but it's showing only for logged out users.
function custom_new_menu() {
register_nav_menus(array('custom-menu' => __( 'Custom Menu' ),'extra-menu' => __( 'Extra Menu')));
}
add_action( 'init', 'custom_new_menu' );
You can utilize the is_user_logged_in() function to check if the current user is logged into Wordpress.
function custom_new_menu() {
if (is_user_logged_in()) {
register_nav_menu(
// Loads the additional nav for logged in users
'extra-menu' => __( 'Extra Menu')
);
} else {
register_nav_menu(
// Loads the default nav for visitors
'custom-menu' => __( 'Custom Menu' ),
);
}
}
add_action( 'init', 'custom_new_menu' );
Note that the code above switched from the register_nav_menus() which registers multiple menus to register_nav_menu() which registers a single menu. The syntax for these is slightly different as we no longer need the inner array.
My parent theme (html5blank) has 3 menus set up my default in it's functions.php. I want to amend their names in my child theme so they make more sense for my client.
I've tried copying the code into my child theme functions.php and renaming the register_html5_menu function but it won't work as there's no reference to the menus in the templates - presumably because this function is related to the CMS?
Here's the code from the parent theme:
function register_html5_menu()
{
register_nav_menus(array( // Using array to specify more menus if needed
'header-menu' => __('Header Menu', 'html5blank'), // Main Navigation
'sidebar-menu' => __('Sidebar Menu', 'html5blank'), // Sidebar Navigation
'extra-menu' => __('Extra Menu', 'html5blank') // Extra Navigation if needed (duplicate as many as you need!)
));
}
Can anyone think of away to amend the names/possibly add new menus via the child theme functions.php?
Also, what is the second 'html5blank' name for? "Header Menu" is the only thing that shows up in the CMS and I see these 'second comments' throughout the functions file. Just wondering what they do, if anything?
The register_html5_menu() function needs to be called the snippet you provide only defines the function.
Can you try adding
add_action( 'after_setup_theme', 'register_html5_menu' );
to your functions.php file after the function.
On you question about the second 'html5blank' I think is more a question about the translation text function __()
More info here:
https://developer.wordpress.org/reference/functions/__/
Try this
add_action( 'after_setup_theme', 'register_my_menu' );
function register_my_menu() {
register_nav_menu( 'header-menu', __( 'Header Menu' ) );
register_nav_menu( 'sidebar-menu', __( 'Sidebar Menu' ) );
register_nav_menu( 'extra-menu', __( 'Extra Menu' ) );
}
I was able to register a new menu location (it shows in the admin Appearance->Menus section. But when I call wp_nav_menu with that location, it prints nothing. If I set location =>'' then it prints it!
functions.php
//This works
add_action( 'init', 'register_my_menus' );
function register_my_menus()
{
register_nav_menus( array( 'my_special_slug' => __( 'My Menu' )));
}
header.php
wp_nav_menu(
array(
'items_wrap'=> '%3$s',
'walker' => new MyWalker(),
'container'=>false,
'menu_class' => '',
'theme_location'=>'my_special_slug', //This does NOT work
'fallback_cb'=>false
)
);
What am I doing wrong?
register_nav_menu takes a comma separated array.
function register_my_menus()
{
register_nav_menu(array( 'my_special_slug', 'My Menu');
}
It appears (please correct me if this is incorrect) that without the user going into the admin interface and turning the menu on, it cannot be found. They need to go to `Appearance->Menus->Locations" and select the menu to show for that location.
I have been unable to find a programmatic way of doing this.
So what i mean by the above is. I've so far made many plugins but i keep getting this issue which i don't know how to describe and that is why i cant search it on google.
What happen is in many plugin a main menu is required and under it several sub-menus that leads to different pages related to the plugin. you can click on the main menu lets say "car" and that will take you to a add car page. and there will also be a sub-menu called add car.
like: posts - when you click on posts for example it takes you to all post, but there is also a sub-menu that is called all post.
What i am having problem with is the main menu is named one thing and when hover it the first sub-menu is also called the same.
My question is:
How can i make it so that either when creating a main menu it will not automatically add a sub menu or if it does. the name will be different then the main menu.
I hope the explanation is clear - i really tried.
Here is how i define my menus:
function my_plugin_menu() {
add_menu_page( 'add_deal', 'Add Deal', 'manage_options', 'add_deal', 'menuload_add_deal', '', '28');
add_submenu_page( 'add_deal', 'deals', 'Deals', 'manage_options', 'deals', 'menuload_deals');
add_submenu_page( 'add_deal', 'orders', 'Order', 'manage_options', 'orders', 'menuload_orders');
add_submenu_page( 'add_deal', 'gc_options', 'Options', 'manage_options', 'gc_options', 'menuload_gc_options');
}
add_action( 'admin_menu', 'my_plugin_menu' );
The above now display for me:
Add Deal - //opens add deal page
Deals
Orders
Options
I would like to change it to:
Shopping Cart - //opens add deal page
add deal - //opens add deal page
deals
orders
options
Ones again i hope that makes sense.. not trying to waste anyone's time but that's why its so hard for me to find info on googble about this issue. (i don't know how to describe the problem well).
I've found a solution incase anyone interested:
function my_plugin_menu() {
add_menu_page( 'add_product', 'Bass Nation', 'manage_options', 'bass-nation-real-shopping-cart', 'menu_load_add_product', '', '28');
add_submenu_page( 'bass-nation-real-shopping-cart', 'add_product', 'Add Product', 'manage_options', 'bass-nation-real-shopping-cart', 'menu_load_add_product');
add_submenu_page( 'bass-nation-real-shopping-cart', 'products', 'Products', 'manage_options', 'products', 'menu_load_products');
add_submenu_page( 'bass-nation-real-shopping-cart', 'orders', 'Orders', 'manage_options', 'orders', 'menu_load_orders');
add_submenu_page( 'bass-nation-real-shopping-cart', 'bnrsc_options', 'Options', 'manage_options', 'bnrsc_options', 'menu_load_options');
}
add_action( 'admin_menu', 'my_plugin_menu' );
You just name the first sub menu's slugen same as the main menu.
Hope this helps someone out there.