WordPress.org

Support

Support » How-To and Troubleshooting » Highlight the year in archive navigation by year

Highlight the year in archive navigation by year

  • hi guys,
    I’d love some help on this I’ve been through the forums and codex at length to try and get a solution!

    I have an archive navigation which is a list of years:

    <?php
    $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts ORDER BY post_date DESC");
    foreach($years as $year) : ?>
    <li><a <?php if (is_page('$year') ) echo('class="onpage" '); ?> href="<?php echo get_year_link($year); ?> "><?php echo $year; ?></a></li>
    <?php endforeach; ?>

    this spits out the url http://www.domain name.com/2012 and uses the archive.php page

    I can not get the archive navigation 2010 to highlight when you are on the 2012 page.

    I’ve tried is_archive but that just highlights all the links!

    Is there a way to do this?
    Thanks so much!

Viewing 13 replies - 1 through 13 (of 13 total)
  • Moderator Andrew Nevins

    @anevins

    Forum moderator

    Can you link us the web page depicting this problem?

    and then if you click through using the year navigation:
    http://www.thecommonguild.org.uk/2011/

    Moderator Andrew Nevins

    @anevins

    Forum moderator

    This echo

    echo('class="onpage" ')

    Need it have parenthesis?
    E.g

    echo 'class="onpage"'

    But clearly the problem lies with this if condition

    is_page('$year')

    apparently not!

    Though that doesn’t seem to be making a difference to the result:

    <?php
    $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts ORDER BY post_date DESC");
    foreach($years as $year) : ?>
    <li><a <?php if (is_page('$year') ) echo'class="onpage" '; ?> href="<?php echo get_year_link($year); ?> "><?php echo $year; ?></a></li>
    <?php endforeach; ?>

    this example still doesn’t highlight the year when you are on that page

    Moderator Andrew Nevins

    @anevins

    Forum moderator

    When you var_dump(is_page('$year')); what is returned?

    Ah well this may be the issue!
    I get:
    bool(false)

    which doesn’t make an awful lot of sense…?

    Moderator Andrew Nevins

    @anevins

    Forum moderator

    which doesn’t make an awful lot of sense…?

    Well in your code, you’re saying; “if ( this_condition() returns true ), echo this string”.

    if (is_page('$year') ) echo'class="onpage" '

    As you can see, this condition returns false.

    bool(false)

    You may need another function to find whether the current page is equal to the $year integer.

    Michael

    @alchymyth

    Forum Moderator

    which doesn’t make an awful lot of sense…?

    it should make more sense after you have read the Codex chapters:

    http://codex.wordpress.org/Function_Reference/is_page
    vs
    http://codex.wordpress.org/Conditional_Tags#A_Date_Page

    Yeah, I see what you’re saying

    I’ve tried:
    is_year
    is_archive
    is_page
    is_date

    what I really need is_url == $year

    I’m used to expression engine where you can just call a segment of the url so I’m struggling a bit with wordpress framework.

    I’m using an archive page which means I thought
    if (is_archive('$year') ) echo'class="onpage" '
    would work (it doesn’t)

    I’m a little lost – surely this is possible?

    is_year and is_archive means all the links display as highlighted it doesn’t seem to be able to differentiate between the years.

    Michael

    @alchymyth

    Forum Moderator

    my mistake – I assumed that is_year() would take a parameter, which (after double checking the Codex) it does not.

    try to check for a year archive and the year in the post date:

    <?php if (is_year() && get_the_date('Y') == $year) echo('class="onpage" '); ?>

    amazing! that is it!

    It’s fully working!

    Thank you very much!

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Highlight the year in archive navigation by year’ is closed to new replies.
Skip to toolbar