Add class to link in wp_list_pages - php

I'm trying to create a hover-effect on a list-item within the wp_list_pages() function. I'm creating a theme for Wordpress, but can't seem to get the hover effect working. I'm pretty new at this, so bear with me.
My css looks like this:
a.nav:hover { color: yellow; }
So for my html-code I add the "nav"-class to my links, like this:
<ul class="nav">
<li><a class="nav" href="#">HOME</a></li>
</ul>
This works, it changes the color of the link to yellow. So far so good.
But when I try to change this html-code to php-code the class isn't there.
I use the wp_list_pages() function, like this:
<ul class="nav">
<?PHP wp_list_pages('title_li=&depth=1&sort_column=menu_order&exclude='); ?>
</ul>
And the outcome then is:
<ul class="nav">
<li class="page_item page-item-23">HOME</li></ul>
So my question is, how to I add the nav class to this link? Is there an attribute within the function or something? Again, I'm really new to this

from the wordpress docs for wp_list_pages() http://codex.wordpress.org/Function_Reference/wp_list_pages#Markup_and_styling_of_page_items :
ll list items (li) generated by wp_list_pages() are marked with the class page_item. When wp_list_pages() is called while displaying a Page, the list item for that Page is given the additional class current_page_item.
with that, the easiest thing you can do is to just change your hover code to:
li.page_item:hover { color: yellow; }

Have a look at this.
Quote: "Add the following line to your theme's functions.php template file, and it will add a class attribute of "tag" to each link generated by wp_list_pages():
add_filter('wp_list_pages', create_function('$t', 'return str_replace("<a ", "<a class=\"tag\" ", $t);'));

I think that the proper way change the default class of a wp_list_pages function is this:
// add to functions.php
// add classes to wp_list_pages
function wp_list_pages_filter($output) {
$output = str_replace('page_item', 'page_item new-class', $output);
return $output;
}
add_filter('wp_list_pages', 'wp_list_pages_filter');

What if I have a different looks on each pages then I have to call only a specific class. The example below is the best way that worked for me.
page.index:
<aside class="menu-side">
<ul class="side-nav-list">
<li>
<!-- First child ( title of the page) -->
<?php the_title() ?>
</li>
<?php
if($theParent){
$findChildOf = $theParent;
}else{
$findChildOf = get_the_ID();
}
wp_list_pages(array(
'title_li' => NULL,
'child_of' => $findChildOf,
))
?>
</ul>
</aside>
CSS file:
.side-nav-list li a:link,
.side-nav-list li a:visited {
display: block;
padding: 1.6rem 1.2rem;
color: #2d234b;}

Related

How to add <i> item if menu has sub-menu into wp_nav_menu

I have this type of menu, and I want this menu to convert into wp_nav_menu
<li>menu ab</li>
<li >menu bc<i class="hover-ind fa fa-angle-down" aria-hidden="true"></i></li>
<ul class="sub-menu">
<li>sub menu ab</li>
</ul>
As you can see if menu have submenu then I have to add <i></i> code snippets to show down arrow with particular menu.
So I am trying, if menu has sub menu then add <i></i> with anchor link into li.
I am able to add attributes to li if menu has submenu by this code:
add_filter( 'nav_menu_link_attributes', 'wpse154485_add_aria_haspopup_atts', 10, 3 );
function wpse154485_add_aria_haspopup_atts( $atts, $item, $args ) {
if (in_array('menu-item-has-children', $item->classes)) {
$atts['aria-haspopup'] = 'true';
}
return $atts;
}
but I need add a <i></i> code snippet if menu has submenu, Please help if anyone have clue about it.
TIA
you need to modify the Walker_Nav_Menu for adding your HTML markup.
check the GitHub link below for sample code.
https://github.com/wp-bootstrap/wp-bootstrap-navwalker
I have resolved this issue. May be if someone looking for answer in future then my solution can help them. here is I found a blog which is exactly for the solution for drop down indicator.
http://dksolution.in/add-dropdown-arrow-indicators-to-wordpress-menu-items-that-have-submenus/

Bread Crumb - WordPress

I have a site that is in wordpress that has a custom navigation. The site also does not use categories. My question is there away to create bread crumb from the custom navigation. I can dump out the information from the post by passing the post ID to a get_post($id), but I cannot see a relationship to previous links. I also looked in the wordpress database and did not see any relationship between the previous post.
Any help will be greatly appreciated.
First, if you are using wordpress.com they will have breadcrumb plugins. And they also exist for wordpress hosted sites and I'm sure wordpress.com home-made sites.
However, if you want to create the breadcrumb functionality from scratch, here's the link to the breadcrumb code from TheWebTaylor Wordpress site (it's long so I used the link rather than copy & paste): https://www.thewebtaylor.com/articles/wordpress-creating-breadcrumbs-without-a-plugin
To call the crumbs on your page use:
<?php custom_breadcrumbs(); ?>
Disclaimer: I have not tested this code, and please read the disclaimer at the bottom of the linked webpage.
Install the Yoast SEO plugin:
https://yoast.com/wordpress/plugins/seo/
Instructions on how to implement breadcrumbs using Yoast SEO:
https://kb.yoast.com/kb/implement-wordpress-seo-breadcrumbs/
<?php
if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('
<p id="breadcrumbs">','</p>
');
}
?>
Create Breadcrumbs:
We’ve created a custom function called get_breadcrumb() to generate the breadcrumb links. You only need to add the get_breadcrumb() function code in functions.php file of the current theme.
1-Step) Copy Below Code in your theme functions.php file
function get_breadcrumb() {
echo 'Home';
if (is_category() || is_single()) {
echo " » ";
the_category(' • ');
if (is_single()) {
echo " » ";
the_title();
}
} elseif (is_page()) {
echo " » ";
echo the_title();
} elseif (is_search()) {
echo " » Search Results for... ";
echo '"<em>';
echo the_search_query();
echo '</em>"';
}
}
Display Breadcrumbs:
Call the get_breadcrumb() function in single.php file and others files where you want to display the breadcrumbs on your WordPress site.
2- STEP) Paste below code where you want to show breadcrumb for example (header.php)
<div class="breadcrumb"><?php get_breadcrumb(); ?></div>
Styling Breadcrumbs:
This CSS helps to style the breadcrumbs links.
3- STEP) Paste Below CSS
.breadcrumb {
padding: 8px 15px;
margin-bottom: 20px;
list-style: none;
background-color: #f5f5f5;
border-radius: 4px;
}
.breadcrumb a {
color: #428bca;
text-decoration: none;
}

Change menu (nav) items to uppercase in wordpress

I am currently going through Wordpress tutorials.
The pages that I have created here are all in Capatalized case (e.g. About us). During run-time I want them to automatically convert to upper-case (i.e. ABOUT US) instead of changing the page titles through the dashboard.
I am aware about the php's strtoupper() method however not sure where to apply this so that it dynamically changes the page titles to upper-case in the nav menu.
CODE in header.php:
<!-- Nav Menu -->
<nav class="site-nav clearfix">
<?php
$args = array(
'theme_location' => 'primary'
);
wp_nav_menu( $args ); enter code here
?>
</nav>
This code in the stylesheet should work:
nav.site-nav li {
text-transform: uppercase;
}

WordPress: Add empty list-item to navigation menu

<ul id="menu-primary">
<li></li>
<li></li>
<li></li><li class="stretcher"></li> /* add adjacent to the last menu item */
</ul>
I need to add <li class="stretcher"></li> adjacent to the last menu item exactly as shown, to the menu with id="menu-primary".
(The reason is to remove the whitespace generated in some browsers. Similar to the first answer in this question: Fluid width with equally spaced DIVs)
I'd add it with a filter:
add_filter('wp_nav_menu_items', 'add_stretcher', 10, 2);
function add_stretcher($items, $args) {
if ($args->theme_location == 'primary') {
$items .= '<li class="stretcher"></li>';
}
return $items;
}
You can add it using jQuery with following code
<script>
jQuery(document).ready(function(){
jQuery("#menu-primary li").last().before("<li class="stretcher"></li>");
});
</script>

Need to add selector to child UL in WordPress menu

In order to make use of a particular jQuery menu in WordPress, I need the child UL (dropdown part of the menu) to have a selector added to it (third line, below):
<ul class="dropdown">
    <li>the first list item
        <ul class="sub_nav">
            <li>child list item</li>
        </ul>
    </li>
</ul>
Note: I left out the link code for greater clarity.
I'm not a coder. My PHP and javascript skills are of the copy-paste-tinker variety. Other forums have yielded lots of vague suggestions, but no solutions. I'm open to other solutions, but I'd like to solve it in one of two places:
Modify <#?php wp_list_pages('title_li=&depth=2'); ?> in the theme header.php file
[had to add # to code bit to make it show up]
Add a function in the theme functions.php file
Mike Little of zed1.com furnished a solution on a LinkedIn forum:
In the theme functions file:
class My_Walker_Page extends Walker_Page {
function start_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
if (0 == $depth)
$output .= "\n$indent<ul class=\"sub_menu\">\n";
else
$output .= "\n$indent<ul>\n";
}
}
In the header file where menu is to appear:
<ul class="dropdown"><?php $walker = new My_Walker_Page;
wp_list_pages(array('title_li'=>'', 'depth' => 2, 'walker' => $walker)); ?></ul>
Mucho gracias to Mike Little, who was a founding member of the WordPress team.
To add a class to the <ul> you can use jQuery, but i think you can do it without adding a class to the second ul.
To add class jQuery('ul.dropdown li ul').addClass('sub_nav');
now you can use jQuery('ul.sub_nav') for whatever you need for, but its same as jQuery('ul.dropdown li ul') so it won't make any difference either you add class or not.

Categories