Dynamic current page highlight issue in wordpress - wordpress

I'm trying get the code below to always highlight the current page of a theme that I'm working on but it does nothing. The HTML part is as shown below the script. Any suggestions or solution for the fix.
<script type="text/javascript">
var url = window.location.href;
url = url.substr(url.lastIndexOf("/") + 1);
$("#navbar").find("a[href='" + url + "']").addClass("current");
</script>
<div id="navbar">
<ul>
<li>home</li>
<li>services</li>
<li>procedure</li>
<li>about</li>
<li>contact</li>
</ul>
</div>

You need to look into WP Nav Menu.
I'm guessing this is a theme you're developing? If so, Register a Nav Menu in your functions.php file. What this will do is allow you to define your menu in the Wordpress dashboard (Appearance->Menus), and customize it however you like.
The reason why I'm suggesting this is that not only will it allow you to have total control over your Menu in a very intuitive way, but it also defines all the classes you could possibly need for your stylesheet automatically (including current items, and ancestry).
That is the cleanest option you have at your disposal. You can also look into WP List Pages for a similar (albeit, more limited) solution.
Otherwise, you'll have to do something dirty like this:
<?php
$id = get_the_ID();
$class = ' class="current"';
?>
<ul>
<li<?php echo is_home() ? $class : ''; ?>>home</li>
<li<?php echo $id==$serives_ID ? $class : ''; ?>>services</li>
<li<?php echo $id==$procedure_ID ? $class : ''; ?>>procedure</li>
<li<?php echo $id==$about_ID ? $class : ''; ?>>about</li>
<li<?php echo $id==$contact_ID ? $class : ''; ?>>contact</li>
</ul>
Replace $services_ID, $procedure_ID, etc. with their respective Page IDs, and if the current ID matches any one of them, that item will have a class of "current" applied to it, which you can then target in your stylesheet.
The main reason why I suggest a server-side solution over using any kind of jQuery is that this is not a task that is suited specifically for jQuery itself.
While the jQuery library is a powerful tool, Wordpress has all of the built-in functionality you need to detect everything you want to detect in this instance. Let the server do as much work as possible before letting the client's browser take over. Otherwise, you'll be looking at some serious load times.
UPDATE:
Based on your last comment, if you're going to go with the messy way and you don't want to be bothered with finding IDs of your pages, you can also do this:
<?php
$slugs = array('services', 'procedure', 'about-us', 'contact-us');
?>
<ul>
<li<?php echo is_home() ? ' class="current"' : ''; ?>>home</li>
<?php
foreach($slugs as $slug)
{
$page = get_page_by_path($slug);
$class = is_page($slug) ? ' class="current"' : '';
echo '<li'.$class.'>'.str_replace('-us','',$slug).'</li>';
}
?>
</ul>
UPDATE BASED ON LAST COMMENT:
Now that you're using wp_nav_menu, you can target the current menu item with CSS. Here's an example that will turn the background of the current item to red:
<style type="text/css">
.current-menu-item{background:#F00;}
</style>
Apply these rules either in your header, or preferably in your main style.css file. Good luck!

Your substr is using lastindex of / +1. The links shown in the html have / on the end. I'd imagine the two are mutually exclusive as s string ending in / would always return an empty string with that substr.

Related

How to style links generated by the get_category_parents wordpress's function?

I am using a code to generate categories hierarchy using get_category_parents() in wordpress. and it works fine, but i want to change the link's color by filtering this function by adding an inline style to the anchor link.
So How to do that? Or just can't filter any function while this function does not contain a hook , and i mean by this the apply_filter().
As you can see on the developer documentation : https://developer.wordpress.org/reference/functions/get_category_parents/#source
There is actually no filter on the output. So the only way, the best one though, is to use CSS for this matter :
// We fetch them :
$cats = get_category_parents($category_id);
if(!is_wp_error($cats)) :
// Let's wrap everything :
echo '<div class="parent-categories">';
echo $cats
echo '</div>';
endif;
And then in your css :
.parent-categories a:first-child{color: red;}
.parent-categories a:last-child{color: blue;}
.parent-categories a:nth-child(2){color: pink;}
// and so on
See : http://www.w3schools.com/cssref/sel_nth-child.asp

How to remove header from particular single page

I'm a newbie at WordPress and php.
I want to remove the header from one or two pages only.
I saw this http://wordpress.org/support/topic/how-to-remove-footer-from-individual-pages
so I did this in my header.php file found under my theme folder
<?php if( !is_page('18') ) :?> <!-- this is what I added -->
<header class="banner">
...rest of html...
</header>
<?php endif;?> <!-- this is what I added -->
Just for the sake of seeing if changes to this file were taking effect I also did <?php if( false ) :?> which was supposed to remove it from all pages but this didn't work either.
Though I don't know the difference, I saw some different syntax and so also tried <?php if(false) { ?> and <?php } ?>
I am wondering if I have to do something else to have the changes take effect.
In case it matters, I am using the roots starter theme http://roots.io/starter-theme/
and WordPress 3.9.1
You should be editing the file /roots/templates/header-top-navbar.php. This get decided at base.php:
if (current_theme_supports('bootstrap-top-navbar')) {
get_template_part('templates/header-top-navbar');
} else {
get_template_part('templates/header');
}
Just in case, add your code to both. I tested your code with header-top-navbar.php and works ok.
you are giving wrong condition it should be if(is_page('18'))
{// do nothing } else {...rest of html...}

Drupal 7 - how to add unique class name to each page?

I'm second day into Drupal and pretty confused... I need to add an unique class name to the body tag to identify each page as there are many unique styling (to repeating elements) on each page across the site.
I've found a couple of code snippets online but they haven't worked. For example I've added this into the template.php file, but it doesn't work:
function testtheme_preprocess_html(&$vars) { // my theme is called "testtheme"
$path = drupal_get_path_alias($_GET['q']);
$aliases = explode('/', $path);
foreach($aliases as $alias) {
$vars['classes_array'][] = drupal_clean_css_identifier($alias);
}
}
It's suppose to add a class to the body tag but nothing is showing up for me. Am I missing something or can someone provide another solution?
The answer of Rainfall is true but :
There are already unique class by page in drupal in body element page-NAMEPAGE
Sorry if I didn't understand you right, but you can add code to the body tag in your html.tpl.php file to make it look this way:
<body class="<?php print $classes; ?>" <?php print $attributes;?>>
And that's all. After that your body tag will automatic have css classes according to the page . Also you'll have info about node type, info about if user logged in, node id.
Let me know if it works or if I made some mistake.
Thanks and good luck!
You can modify https://www.drupal.org/project/node_class module:
1) comment function node_class_preprocess_node in node_class.module
2) insert
$node_classes = node_class($node);
if (!empty($node_classes)) $node_classes .= ' ';
$content_column_class = str_replace(' class="','', $content_column_class);
$content_column_class = ' class="'. $node_classes . $content_column_class;
in page.tpl.php
before
<section<?php print $content_column_class; ?> id="content_col">

Body class trace for Joomla template like WordPress

How can I trace all the relevant tags of a page in the form of classes to the <body> tag? I'm referring to how Wordpress puts the following in a page's body class attribute:
home page page-id-12766 page-template page-template-page-home-php cufon2-disabled safari theme-*
This could be extremely helpful in Joomla template development.
Thanks in advance.
I don't know whether there is any out of the box solutions exists for this. As the main structure of the site is mainly based on the Menu structure, the below snippet may be useful. This is used in the templates/mytemplate/index.php file.
<?php
$menu = &JSite::getMenu();
$active = $menu->getActive();
$pageclass= '' ;
//$currentpage = ''; //In case if wanted to get the alias of the current page in a later stage.
if( count($active->tree) > 0 ) {
foreach ($active->tree as $key => $value) {
$pageclass .= 'level-'.$key . ' pageid-'.$value. ' page-'.$menu->getItem($value)->alias ;
//$currentpage = $menu->getItem($value)->alias;
}
}
?>
<body class="<?php echo $pageclass; ?>">
This will output something like:
<body class="level-0 pageid-101 page-home">
You may improve this using various values available in the $active variable.
Here's some information on the Body Class Function
How Wordpress determines which classes to apply using it is found in post-template.php
The relevant function you're looking for is get_body_class within this template. It essentially runs a series of checks to determine if the current page meets the criteria for given classes. It also relies heavily on Wordpress specific function calls that helps make that determination.

Drupal theme preprocess function - primary links

I recently wrote a theme function to add a class to my primary links that works great. I then wrote some css classes to style these links with custom background images. Worked out great. Now comes the problem, the link text for the primary links still is displayed. Normally this isn't a problem as I would just wrap the in a with a custom "hide" class. For example:
<span class="hide">Link Text</span>
So my question is how can I loop through the primary links and wrap the text w/ a <span> like my example? Here's my theme function that I used to add my classes.
function zkc_preprocess_page(&$vars, $hook) {
// Make a shortcut for the primary links variables
$primary_links = $vars['primary_links'];
// Loop thru the menu, adding a new class for CSS selectors
$i = 1;
foreach ($primary_links as $link => $attributes){
// Append the new class to existing classes for each menu item
$class = $attributes['attributes']['class'] . " item-$i";
// Add revised classes back to the primary links temp variable
$primary_links[$link]['$attributes']['class'] = $class;
$i++;
} // end the foreach loop
// reset the variable to contain the new markup
$vars['primary_links'] = $primary_links;
}
Is jQuery an option?
Try something like this:
$(document).ready(function(){
$('#primary li a')
.wrapInner('<span class="hide">' + '</span>');
});
EDIT:
Or if you want to go Drupal, put this guy in your foreach loop:
$link['title'] = '<span class="hide">' . check_plain($link['title']) . '</span>';
If all you want is to hide the link text, why don't you just use something like text-indent: -9999px;?
The correct methods for altering the output of the menu links can be done at the theming layer. You were on the right path with the preprocessing hook use, but there is a little more to it.
Refer to this for more information:
http://drupal.org/node/352924#comment-1189890
http://api.drupal.org/api/function/theme_links/6
Typo?
$primary_links[$link]['$attributes']['class'] = $class;
Should read;
$primary_links[$link]['attributes']['class'] = $class;

Resources