• Resolved luispunchy

    (@luispunchy)


    I call wp_get_archives in a sidebar, using the defaults so it displays archives listed by month. I want to highlight the month in the list when you are on that month’s archive page.

    This would be similar to the logic with wp_list_categories when the category list is generated on a Category Archive page, and how the list item for that category is marked with the HTML class current-cat.

    I know the XHTML/CSS involved (i.e., I know how to style the markup to get that affect) – but I CANNOT figure out how to coerce WordPress to generate the necessary class attribute for me to use as a style hook.

    I need something like this:

    <ul id="archives">
      <li class="selected"><a href="#" title="April 2008">April 2008</a></li>
      <li><a href="#" title="March 2008">March 2008</a></li>
      <li><a href="#" title="Feb 2008">Feb 2008</a></li>
      ...
    </ul>

    How do I get wp_get_archives to generate a class attribute for the selected archive month?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter luispunchy

    (@luispunchy)

    sigh… anybody? I’ll keep plugging through the source and post back here if I can figure it out, but I’m thinking *somebody* must have tried this before… and got it working… anybody??

    Yeah, I’d really like an answer to this too. I don’t see why the tag doesn’t have the same actions as wp_list_categories.

    I’m not sure if this will help you or not, but I *think* the code you want to modify would be here:

    /wp-includes/general-template.php

    In WP 2.6.1, there is an area of code around line 350 on that deals with the wp_get_archives function. Perhaps someone with an understanding of this code could post a hack to meet your needs?

    I’m sorry I can’t be of more help.

    I was able to hack this up really quickly… be warned, this is mostly untested, but it should do what you want.

    Find in /wp-includes/general-template.php line 352. It should be the get_archives_link function declaration… replace that function with this modified one:

    /* link navigation hack by Orien https://icecode.com/ */
    function get_archives_link($url, $text, $format = 'html', $before = '', $after = '') {
    	$text = wptexturize($text);
    	$title_text = attribute_escape($text);
    	$url = clean_url($url);
    
    	if ('link' == $format) {
    		$link_html = "\t<link rel='archives' title='$title_text'  href='$url' />\n";
    	} elseif ('option' == $format) {
    		$link_html = "\t<option value='$url'>$before $text $after</option>\n";
    	} elseif ('html' == $format) {
    		$requested = "https://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}";
    		if ($requested == $url){ $class = " class='current-cat'"; } else { $class = null; }
    
    		$link_html = "\t<li$class>$before<a href='$url' title='$title_text'>$text</a>$after</li>\n";
    	} else { // custom
    		$link_html = "\t$before<a href='$url' title='$title_text'>$text</a>$after\n";
    	}
    
    	$link_html = apply_filters( "get_archives_link", $link_html );
    
    	return $link_html;
    }

    For anyone who cares: all this is doing now is testing for the requested uri and checking it against the returned $url from the original function… it currently applies a class of current-cat to the <li>.

    HTH.
    Luke

    As of 2.7 update, the hack I posted is now around line 626 of the same file:

    /**
     * Retrieve archive link content based on predefined or custom code.
     *
     * The format can be one of four styles. The 'link' for head element, 'option'
     * for use in the select element, 'html' for use in list (either ol or ul HTML
     * elements). Custom content is also supported using the before and after
     * parameters.
     *
     * The 'link' format uses the link HTML element with the <em>archives</em>
     * relationship. The before and after parameters are not used. The text
     * parameter is used to describe the link.
     *
     * The 'option' format uses the option HTML element for use in select element.
     * The value is the url parameter and the before and after parameters are used
     * between the text description.
     *
     * The 'html' format, which is the default, uses the li HTML element for use in
     * the list HTML elements. The before parameter is before the link and the after
     * parameter is after the closing link.
     *
     * The custom format uses the before parameter before the link ('a' HTML
     * element) and the after parameter after the closing link tag. If the above
     * three values for the format are not used, then custom format is assumed.
     *
     * @since 1.0.0
     * @author Orien
     * @link https://icecode.com/ link navigation hack by Orien
     *
     * @param string $url URL to archive.
     * @param string $text Archive text description.
     * @param string $format Optional, default is 'html'. Can be 'link', 'option', 'html', or custom.
     * @param string $before Optional.
     * @param string $after Optional.
     * @return string HTML link content for archive.
     */
    function get_archives_link($url, $text, $format = 'html', $before = '', $after = '') {
    	$text = wptexturize($text);
    	$title_text = attribute_escape($text);
    	$url = clean_url($url);
    
    	// modified 2008-12-15 by luke wertz
    	if ('link' == $format) {
    		$link_html = "\t<link rel='archives' title='$title_text' href='$url' />\n";
    	} elseif ('option' == $format) {
    		$link_html = "\t<option value='$url'>$before $text $after</option>\n";
    	} elseif ('html' == $format) {
    		$requested = "https://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}";
    		if ($requested == $url){ $class = " class='current-cat'"; } else { $class = null; }
    
    		$link_html = "\t<li$class>$before<a href='$url' title='$title_text'>$text</a>$after</li>\n";
    	} else { // custom
    		$link_html = "\t$before<a href='$url' title='$title_text'>$text</a>$after\n";
    	}
    
    	$link_html = apply_filters( "get_archives_link", $link_html );
    
    	return $link_html;
    }

    How could we make this a custom function, or otherwise a plugin? This is exactly what I needed, but I hate editing the core files.

    efetch

    (@efetch)

    Luke, you’re my heroe. Your solution is the only I found in the www.

    Thank you very much
    Frank

    borholquib

    (@borholquib)

    Great job with the hack, but I still don’t understand the inconsistency between the various functions (wp_list_* and wp_get_archives).

    I’d love something that doesn’t break with an upgrade.

    I’d also like it if this could be turned into a custom function or plugin – WP is updated much more often than many other CMS’s, so it’d be great not having to change core files.

    If you don’t want to edit the wp core files, just try this :

    function get_archives_link_mod ( $link_html ) {
       preg_match ("/href='(.+?)'/", $link_html, $url);
       $requested = "https://{$_SERVER['SERVER_NAME']} {$_SERVER['REQUEST_URI']}";
          if ($requested == $url[1]) {
             $link_html = str_replace("<li>", "<li class='current'>", $link_html);
          }
          return $link_html;
       }
    add_filter("get_archives_link", "get_archives_link_mod");

    Copy the code in your themes functions.php – this may not be the most elegant solution, but it works (for html lists only) … Have fun!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Modify wp_get_archives | highlight current archive category’ is closed to new replies.