How do I add another footer menu? - php

I am using the customizr theme on wordpress and I want to edit my footer menu. On the help page I found this php code. I put it in functions.php and it does work but I want to add another menu.
add_filter('tc_credits_display', 'my_custom_credits');
function my_custom_credits(){
$credits = '';
$newline_credits = '';
return '
<div class="span4 credits">
<p> · © '.esc_attr( date( 'Y' ) ).' '.esc_attr(get_bloginfo()).' · '.($credits ? $credits : 'Designed by Themes & Co').' ·'.($newline_credits ? '<br />· '.$newline_credits.' ·' : '').'</p> </div>';
}
The 1st one is copyright, 2nd one is themes & co link, I want to add another one for terms of service.
'Terms of Service').'
How do I do it?

to add another menu you need to do two things first declare the menu in the themes function file like so:
function register_my_menu() {
register_nav_menu('header-menu',__( 'Header Menu' ));
}
add_action( 'init', 'register_my_menu' );
Where register_my_menu can be whatever you want to call the function, header-menu needs to be unique and match the following command.
Then:
<?php wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); ?>
to echo out the menu in the theme files...
please read this: http://codex.wordpress.org/Navigation_Menus

Related

Swap Wordpress "Widget Area" based on Page Template

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.

Add a custom class to woocommerce recent product reviews div

I am trying to add a class to <div class="star-rating"> located in content-widget-reviews.php ( in woocommerce template )
displayed here:
<?php echo wc_get_rating_html( intval( get_comment_meta( $comment->comment_ID, 'rating', true ) ) ); ?>
I want to make my template work with Bootstrap 4 ( see caps below )
float right needs to be removed vs.
wanted result
I tried this solution below, but it adds the classes on the whole template ( I just want to add this class to the widget area)
// Widget review Stars
function myfuntion_wc_get_rating_html($html) {
// Replace link classes
$html = str_replace('<div class="star-rating"', '<div class="star-rating float-none text-primary"', $html);
return $html;
}
add_filter("woocommerce_product_get_rating_html", "myfuntion_wc_get_rating_html")
;
Is there a way to add my custom class directly on this code ?
<?php echo wc_get_rating_html( intval( get_comment_meta( $comment->comment_ID, 'rating', true ) ) ); ?>
Thanks
P.S. I'd like to avoid the use of Jquery and CSS
Did you try replacing WooCommerce template in your theme like described here https://docs.woocommerce.com/document/template-structure/ ?
Edit files in an upgrade-safe way using overrides. Copy the template
into a directory within your theme named /woocommerce keeping the same
file structure but removing the /templates/ subdirectory.

wordpress: set class of navigation element to content of element

i'm new to wordpress theming and I currently try to implement a navigation menu using icons. The framework i use requires me to set specific class names to achieve the icon being displayed - so what i basically want is:
<ul>
<li class="blog">blog</li>
....
So the class should be equal to the content
I found the following in the wordpress docs
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function special_nav_class($classes, $item){
if(is_single() && $item->title == "Blog"){
$classes[] = "special-class";
}
return $classes;
}
This is how I currently display my menu
wp_nav_menu( array( 'theme_location' => 'primary', 'menu_id' => 'primary-menu' ) );
I'm kind of confused by the example given from the wordpress doc's since I cannot figure out the use of al the parameters or even where to place it.
I assume that you are registered your primary menu in functions.php.
After that you must put this code sample again in your functions.php.
What the code does is that it would launch the function special_nav_class for each menu item from primary-menu. As a parameter this function will receive the current menu item ( as post object ) and current classes applied for it as an array.
So, if you have menu with 5 elements, this function will run 5 times and each time it will receive current menu item and its classes array.
Basically something like this can do the job, if menu items are named properly:
function special_nav_class($classes, $item){
$classes[] = strtolower($item->title);
return $classes;
}

how to get a wordpress blog page to show differnt nav bar from maine page

Hi im a web dev an im trying to get word press to use a 2nd navigation for my blog section. i have the main site all set up but what i want is when i hit the blog in my top nav to show a differnt navigation for the blog section. i have tried using the following code in the header.php
<?php
if (is_single('blog')){
wp_nav_menu(array('menu'=>'secondary' ));
}
?>
but this code does not seem to work even though the theme supports 2 navigations.
<nav class="Seo-nav">
<div class="Seo-nav-inner">
<?php
echo theme_get_menu(array(
'source' => theme_get_option('theme_menu_source'),
'depth' => theme_get_option('theme_menu_depth'),
'menu' => 'primary-menu',
'class' => 'Seo-hmenu'
)
);
get_sidebar('nav');
?>
the above code is the code i use to call my navigation. is there any way to get a specific page or pages to show the one menu? any help on this would be great. never had to make certain pages have different navigation before so this is a new one on me.
Like was said in comments earlier, you'll need Conditional Tags. Probably for you specifically, the is_page() conditional.
<?php
if (is_page('blog')) {
// Echo this menu on the blog slug page
wp_nav_menu(array(
'theme_location' => '[name_of_menu]'
));
} else {
// Otherwise, echo this one
wp_nav_menu(array(
'theme_location' => '[name_of_main_menu]'
));
}
See the codex for the wp_nav_menu attributes. Can print out the menu other ways, that's just the one I recommend.
Depending how you blog is set up, is_page('blog') may need to be is_home(), 'blog' == get_post_type(), or some other variation.
I think you can go here two ways.
One is to only show children of current page on the second like
function show_different_nav(){
global $post;
$sec_nav=wp_list_pages("child_of=".$post->ID);
return $sec_nav;
}
now call show_different_nav() in your template.
The other way would be to write an own filter. I modified the http://wordpress.org/plugins/get-different-menus/ in this case.
add_filter( 'wp_nav_menu_items', 'get_different_custom_menu_item_according_to_page_slug', 10, 2 );
function get_different_custom_menu_item_according_to_page_slug ( $items, $args ) {
$current_id=(int)$GLOBALS['post']->ID;
$current_slug=$GLOBALS['post']->slug;
$the_nmo = wp_get_nav_menu_object($current_slug);
if ($the_nmo==""){}else{
$menu_id=$the_nmo->term_id;
$items_2 = wp_get_nav_menu_items($menu_id);
$items="";
if ($args->theme_location == 'secondary') {
foreach ($items_2 as $item){
$link_page_id = (int)($item->object_id);
if ($current_id == $link_page_id){$cur=" current-menu-item current_page_item";}else{$cur="";
}
$items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page'.$cur.'">'.$item->title.'</li>';
}
}
}
return $items;
}
In my opinion you need a menu for this called exactly the same as the page slug.

Wordpress Navigation Help

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.

Categories