Wordpress - Add "active" class if current page is active - php

<?php
if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) {
$parent_title = get_the_title($post->post_parent);?>
<li><?php echo $parent_title;?></li>
<?php echo $children; ?>
<?php } ?>
The code above lists the parent and all child pages in a list.
Parent Page
Child Page
Child Page class="active"
Child Page
Child Page
I would like to add a class of "active" to the currently active page. Any help is greatly appreciated. Thanks

To look for a specific page and add an active class to it, you can try using is_page and define the URL/slug of the page.
<a class="<?php if (is_page('name-of-page')) echo 'active'; ?>" href="#">Link</a>

You can easily add active and other classes by checking the $post->post_title against the $item->title
function addLinkClassesWithActive( $atts, $item, $args ) {
global $post;
// check if the item is in the primary menu
if( $args->theme_location == 'main-nav' ) {
// add the desired attributes:
$atts['class'] = $post->post_title === $item->title ? 'mdl-layout__tab is-active' : 'mdl-layout__tab';
}
return $atts;
}
add_filter( 'nav_menu_link_attributes', 'addLinkClassesWithActive', 10, 3 );
I am using this myself and stripping out the wrapping container, the ul, and the li tags so that I have just a link. See example below.
<nav role="navigation" class="mdl-navigation mdl-layout--large-screen-only" itemscope itemtype="http://schema.org/SiteNavigationElement">
<div class="mdl-tabs mdl-js-tabs mdl-js-ripple-effect">
<?php
$primaryMenu = array(
'container' => false,
'theme_location' => 'main-nav',
'items_wrap' => '%3$s',
'echo' => false,
);
echo strip_tags( wp_nav_menu( $primaryMenu ), '<a>' );
?>
</div>
</nav>

Related

WordPress wp_list_pages - Change children of parent from URLS into #anchor links

I'm setting up a side navigation menu using wp_list_pages and I would like to convert the children links from URLs into anchor links i.e #link rather than the current https://example.com/link being generated by wp_list_pages .
I was attempting to use the following code:
<?php
$my_pages = wp_list_pages('echo=0&title_li=&child_of=5&depth=1');
$pieces = explode('"', $my_pages);
$i = 5;
$j = 3;
$limit = count($pieces);
for (;$i<$limit;) {
$tmp1 = '#'.$pieces[$j];
$pieces[$i] = $tmp1;
$i = $i+6;
$j = $j+6;
}
$tmp2 = implode('"',$pieces);
echo $tmp2;
?>
But it seems to be very old and I can't wrap my head around how to properly implement it into my current structure. Maybe this code is useless for what I'm trying to do but I couldn't find anything that would work.
This is what I have currently:
<div class="hero-container">
<?php
global $children;
global $post;
if ( $post->post_parent ) {
$children = wp_list_pages( array(
'title_li' => '',
'child_of' => $post->post_parent,
'echo' => 0
) );
} else {
$children = wp_list_pages( array(
'title_li' => '',
'child_of' => $post->ID,
'echo' => 0
) );
}
if ( $children ) : ?>
<?php echo '<div class="hero-side-menu">', '<h1>', get_the_title(), '</h1>', '<ul>', $children, '</ul>', '</div>' ?>
<?php endif; ?>
</div>
Any suggestions would be very much appreciated as I've been trying to figure this out for a few days and have gotten nowhere... also would appreciate an explanation as I'm trying to learn where I went wrong!
I need to modify the children of the parent pages to have #anchor links rather than URLS as I've condensed the pages into their parents but still wish to have them as options within the side menu.
Clarification: I have a page with children pages that I referred to as parents however, they are indeed children. I would like to keep the URLS for the children pages and then make the children of the children #links.
This depends a bit on what you want the anchor text to be. Whether it's an ID or a title, etc. But you can modify that to your need.
You may want to try get_pages() instead, so you have a little more control over the output. Something like the following:
<div class="hero-container">
<?php
global $post;
$current_page_title = $post->post_title; // current page title
$child_pages =
get_pages(
array(
'child_of' => $post->ID
),
);
if ($child_pages) : ?>
<div class="hero-side-menu">
<h1><?php echo $current_page_title; ?></h1>
<ul>
<?php foreach ($child_pages as $page) : ?>
<li><?php echo $page->post_title; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
</div>

How to remove specific children pages when using echo $children; in Wordpress

UPDATE: Fixed it!!
<?php
$children = wp_list_pages("title_li=&child_of=".$root_parent_id."&sort_column=menu_order&echo=0&exclude=912");
if ($children) {
?>
We are using the following php code in the page.php file to dynamically build our sidebar nav section; however, I need to exclude certain child pages from appearing. Any suggestions would be very helpful!
<?php
$children = wp_list_pages("title_li=&child_of=".$root_parent_id."&sort_column=menu_order&echo=0");
if ($children) {
?>
<div class="submenu">
<h2><?php echo get_the_title( $root_parent_id ); ?></h2>
<ul>
<?php echo $children; ?>
</ul>
</div>
Replace your first line with the following code. Update 12 with the IDs of your child pages that you want to exclude.
Note: I moved the parameters into $args as its easier to read and maintain,
$args = array(
'title_li' => '',
'child_of' => $root_parent_id,
'sort_column' => 'menu_order',
'echo' => 0,
'exclude' => '12'
);
$children = wp_list_pages($args);
Here is more info on wp_list_page().

Add Bootstrap class/style to Wordpress wp_list_pages menu items

I have some code that generates a sidebar menu on my Wordpress site as below...
<ul>
<?php
if($post->post_parent)
$children = wp_list_pages("title_li=&sort_column=menu_order&child_of=".$post->post_parent."&echo=1");
else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=1");
if ($children) { ?>
<?php echo $children; ?>
</div>
<?php } ?>
</ul>
This outputs code in the following way...
<ul>
<li class="page_item page-item-94 current_page_item">Menu Item</li>
</ul>
The problem is, I need it to use my Bootstrap 3 styles and output the list as below...
<div class="list-group submenu">
Menu Item
</div>
Basically I need to add the class of list-group-item to the a tag in the output list items. Is that at all possible?
You can do this through jquery
$(document).ready(function(){
$("ul").find("li").each(function(){
$(this).addClass("YourClass");
})
})
Your can your Walker Class to simply extend the output of wp_list_pages modify your $output put the Class in functions.php
class NS_CS_Walker extends Walker_page {
function start_el( &$output, $page, $depth, $args, $current_page ) {
if ( $depth ) {
$indent = str_repeat( "\t", $depth );
} else {
$indent = '';
}
extract( $args, EXTR_SKIP );
$output .= $indent . '<li><div>' . get_post_meta( $post_id, $key, $single ) . '</div></li>';
// modify your output here
} // End start_el
} // End Walker Class
Then your rendering code should be
$args = array(
'walker' => new NS_CS_Walker()
// your extra $args would be here
);
wp_list_pages( $args );

Wordpress nav menu -- adding conditional span

Does anyone have a working example of a Wordpress menu outputted manually? The one on the Codex didn't seem to work, though my menu_name slug of "Footer" can't be that complicated..? I need to add offscreen spans within external menu links for accessibility purposes. I already have CSS classes; what I need is specific markup added via the functions.php file.
The basic code I'm looking for (see list items w/ 'external' class. Hrefs changed to # for code brevity, they link offsite in real life):
<ul id="menu-footer" class="menu">
<li class="menu-item">
About Us
</li>
<li class="external menu-item">
<a title="Link opens in a new window." target="_blank" href="#">Terms & Conditions <span class="offscreen">Opens in a new window</span></a>
</li>
<li class="menu-item">
Comments Policy
</li>
<li class="external menu-item">
<a title="Link opens in a new window." target="_blank" href="#">Privacy Statement<span class="offscreen">Opens in a new window</span></a>
</li>
<li class="menu-item">
Contact Us
</li>
</ul>
A custom "walker" class?
http://www.kriesi.at/archives/improve-your-wordpress-navigation-menu-output
Option 1:
Assuming you've created your menu by using Appearance -> Menus , you can try the following.
Navigate to Appearance -> Menus
Click the "Screen Options" (top right) of the screen, make sure "CSS Classes" is selected.
Once you've done the above steps, each of the menu items will now have a "CSS Classses" textbox showing where you can on an individual basis assign CSS Classes (have a look at the screen grab).
Option 2:
You could also try http://codex.wordpress.org/Function_Reference/wp_nav_menu#Adding_Conditional_Classes_to_Menu_Items
<?php
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function special_nav_class($classes, $item){
if(is_single() && $item->title == "Blog"){ //Notice you can change the conditional from is_single() and $item->title
$classes[] = "special-class";
}
return $classes;
}
?>
Option 3:
Provided the above 2 options don't work you have the 3rd option of using jQuery.
HTH
Answer: the nav menu has to be registered & assigned to the theme for the Codex example to work. DUH!
in functions.php:
// default register any navigation menus
register_nav_menus(
array(
'footer' => 'Footer Menu',
'links' => 'Links Menu'
)
);
Assign menu(s) to your theme:
Back in functions.php:
function accessibleAnchors($menu_name, $link_after = NULL){
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
$menu_list = '<ul id="menu-' . $menu_name . '" class="menu">';
foreach ( (array) $menu_items as $key => $menu_item ) {
// uncomment next line to see all array values
// print_r($menu_item);
$linkText = $menu_item->title;
$url = $menu_item->url;
$target = $menu_item->target;
$titleAttr = $menu_item->attr_title;
// hard-coding the first & only CSS class here, you could loop through others
$cssClass = $menu_item->classes[0];
if($target == '_blank'):
$titleAttr = " title=\"$titleAttr\"";
$span = "<span class=\"offscreen\"> Opens in a new window</span>";
else:
$span = "";
endif;
$menu_list .= '<li class="menu-item '.$cssClass.'"><a href="' . $url . '"'.$titleAttr. '>' . $linkText . $span . $link_after .'</a></li>';
}
$menu_list .= '</ul>';
}
return $menu_list;
}
Now call the function in your template:
<?php echo accessibleAnchors('footer'); ?>
Boom.
You'll have to start in wp-includes/nav-menu-template.php to Copy and paste the function to your theme's functions.php and change the name or use a filter in functions.php to change selected markup of the hook. And possibly take a look in wp-includes/nav-menu.php as well.
Sorry I can't provide more detail.

Getting the literal value of a variable without echo

I have a function below, list_child_sibling_pages, which returns a string of 'hidden' when the initial 2 if / ifelse statements are false.
I need to put the literal return value 'hidden' in to the array below as shown here
array( 'classname' => 'custom child-parent-menu-container '. $hidden, 'description' => 'Automatically add a child page menu to parent pages. Automatically add a sibling menu to child pages' );
I know I can output it with with
echo list_child_sibling_pages();
But I don't know how I would work that in to the code below. The value is returned fine if I just echo it out on it's own my only issue is echoing it where I need it
Relevant code follows
function tmm_child_parent_menu() {
function list_child_sibling_pages()
{
global $post;// if outside the loop
$post_type = '&post_type='.get_post_type(); 
$children = get_pages('child_of='.$post->ID.$post_type);
$parent_title = get_the_title($post->post_parent);
$parent_permalink = get_permalink($post->post_parent);
 
//child pages
if ( $post->post_parent && !is_search() && !is_404() ) 
{ ?> 
<h4><?php echo ''.$parent_title.''; ?></h4>
<ul class="parent-page-children">
<?php wp_list_pages( 'title_li=&child_of='.$post->post_parent.'&sort_column=menu_order'.$post_type ); ?>
</ul>
<?php 
} 
//parent pages
elseif ( count( $children ) != 0 && !is_search() && !is_404() )
{ ?>
<h4><?php the_title(); ?></h4>
<ul class="parent-page-children">
<?php wp_list_pages( 'title_li=&child_of='.$post->ID.'&sort_column=menu_order'.$post_type ); ?>
</ul>
<?php 
} 
else 
{
return 'hidden';
}
}
$hidden = list_child_sibling_pages();
/* Widget settings. */
$widget_ops = array( 'classname' => 'custom child-parent-menu-container '. $hidden, 'description' => 'Automatically add a child page menu to parent pages. Automatically add a sibling menu to child pages' );
confused but i think you want
$array=array('classname'=>'custom child-parent-menu-container '.list_child_sibling_pages(),
'description'=>'Automatically add a child page menu to parent pages. Automatically add a sibling menu to child pages');

Categories