• Hi all,

    My thmeme gives me dynamic highligt of the menu-item. Great!

    However; I need a function to return some “current” for a menu-item in case a single post, the archive or the category is viewed.

    How do I do that?

    From the header:

    <?php wp_nav_menu(array(
    				    'container'       => '',
    					'theme_location'  => 'header-menu')
    					);
    				?>

    From the functions.php

    /**
     * Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
     */
    function responsive_page_menu_args( $args ) {
    	$args['show_home'] = true;
    	return $args;
    }
    add_filter( 'wp_page_menu_args', 'responsive_page_menu_args' );
    
    /**
     * Remove div from wp_page_menu() and replace with ul.
     */
    function responsive_wp_page_menu ($page_markup) {
        preg_match('/^<div class=\"([a-z0-9-_]+)\">/i', $page_markup, $matches);
            $divclass = $matches[1];
            $replace = array('<div class="'.$divclass.'">', '</div>');
            $new_markup = str_replace($replace, '', $page_markup);
            $new_markup = preg_replace('/^<ul>/i', '<ul class="'.$divclass.'">', $new_markup);
            return $new_markup; }
    
    add_filter('wp_page_menu', 'responsive_wp_page_menu');

    Can I just add something like this:

    function test
            {
                    if (is_single())
                    {
                    echo " id=\"current-test\"";
                    }
                    }

    Do I just add it in the functions? And where do I call it?

    I could hardcode the menu, but I really dont want to ??

    So – I just need the existing functions, that returns current_page_item, to return current_page_item for single, archive and category.

    Thanks.

    rasmus

Viewing 5 replies - 1 through 5 (of 5 total)
  • I have the same problem and I don′t know how can I fix it. Did you find any solution?

    Thanks a lot!

    I tried to use it

    function add_parent_url_menu_class( $classes = array(), $item = false ) {
    	// Get current URL
    	$current_url = current_url();
    	// Get homepage URL
    	$homepage_url = trailingslashit( get_bloginfo( 'url' ) );
    	// Exclude 404 and homepage
    	if( is_404() or $item->url == $homepage_url ) return $classes;
    	if ( strstr( $current_url, $item->url) ) {
    		// Add the 'parent_url' class
    		$classes[] = 'current-menu-item current_page_item';
    	}
    
    	return $classes;
    }
    
    function current_url() {
    	// Protocol
    	$url = ( 'on' == $_SERVER['HTTPS'] ) ? 'https://' : 'https://';
    	$url .= $_SERVER['SERVER_NAME'];
    	// Port
    	$url .= ( '80' == $_SERVER['SERVER_PORT'] ) ? '' : ':' . $_SERVER['SERVER_PORT'];
    	$url .= $_SERVER['REQUEST_URI'];
    
    	return trailingslashit( $url );
    }

    from this site – https://bloke.org/php/highlighting-current-page-in-wordpress-menus/

    but to me, I didn′t have any results

    Moderator keesiemeijer

    (@keesiemeijer)

    What is it exactly you want to do? By default the “current-menu-item” class is given to a single Post “menu item” if that single Post is visited.

    Ok, but it′s not happening, only in page, not in post or archive (categories) page. And I′m using the same css for both.

    Moderator keesiemeijer

    (@keesiemeijer)

    Can we have a link to your site to see what is going on. These should all get the same “current-menu-item” class when visited.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘current_page_item –> on single post, category and archive’ is closed to new replies.