0

exist some chance to highlight archive link in classic wp archive widget on single.php template?

I have the same template for archive.php and single.php where I call

<?php wp_custom_archive(); ?>

It's customizated wp archive with highlighting current links (placed in functions.php):

function wp_custom_archive($args = '') {
global $wpdb, $wp_locale;

$defaults = array(
    'limit' => '',
    'format' => 'html', 'before' => '',
    'after' => '', 'show_post_count' => false,
    'echo' => 1
);

$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );

if ( '' != $limit ) {
    $limit = absint($limit);
    $limit = ' LIMIT '.$limit;
}

$archive_date_format_over_ride = 0;
$archive_day_date_format = 'Y/m/d';
$archive_week_start_date_format = 'Y/m/d';
$archive_week_end_date_format   = 'Y/m/d';

if ( !$archive_date_format_over_ride ) {
    $archive_day_date_format = get_option('date_format');
    $archive_week_start_date_format = get_option('date_format');
    $archive_week_end_date_format = get_option('date_format');
}

$where = apply_filters('customarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
$join = apply_filters('customarchives_join', "", $r);

$output = '<ul>';

    $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";
    $key = md5($query);
    $cache = wp_cache_get( 'wp_custom_archive' , 'general');
    if ( !isset( $cache[ $key ] ) ) {
        $arcresults = $wpdb->get_results($query);
        $cache[ $key ] = $arcresults;
        wp_cache_set( 'wp_custom_archive', $cache, 'general' );
    } else {
        $arcresults = $cache[ $key ];
    }
    if ( $arcresults ) {
        $afterafter = $after;
        foreach ( (array) $arcresults as $arcresult ) {
            $url = get_month_link( $arcresult->year, $arcresult->month );
            $year_url = get_year_link($arcresult->year);
            $text = sprintf(__('%s'), $wp_locale->get_month($arcresult->month));

            $year_text = '<span class="year">'.sprintf('%d', $arcresult->year).'</span>';
            if ( $show_post_count )
                $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
            $year_output = get_archives_link($year_url, $year_text, $format, $before, $after);              
            $output .= ( $arcresult->year != $temp_year ) ? $year_output : '';
            $output .= get_archives_link($url, $text, $format, $before, $after);

            $temp_year = $arcresult->year;
        }
    }

$output .= '</ul>';


if ( $echo )
    echo $output;
else
    return $output;

It works, but not for single.php template. On single.php is nothing highlighted. Have someone an idea what I missed?

1 Answer 1

1

As your links are generated by get_archives_link(), this is the place where you have to look. Doing so, by inspecting the source , you will notice that get_archives_link() - itself - doesn't give you the possibility to add a CSS class to make the highlighting happen.

There is of course the filter hook get_archives_link - or see the source - which could be used to do this. But as you can see, the only information available is the complete link html output - $link_html - which pretty much forces you to do some RegEx to extract the URL and then check on that and if it fits add a class to the output. This is a way to do it and you will find information on here or out there how to do it.

But, if we are honest, it is not great, because who actually wants to do RegEx's all the time - it is just not really desirable. So personally it would make my_own_archives_link() function, there you can implement the possibility to add classes for highlighting according to your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.