WordPress.org

Ready to get started?Download WordPress

Forums

Highlight the year in archive navigation by year (14 posts)

  1. pcmdv
    Member
    Posted 2 years ago #

    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!

  2. Andrew
    Forum Moderator
    Posted 2 years ago #

    Can you link us the web page depicting this problem?

  3. pcmdv
    Member
    Posted 2 years ago #

  4. pcmdv
    Member
    Posted 2 years ago #

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

  5. Andrew
    Forum Moderator
    Posted 2 years ago #

    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')
  6. pcmdv
    Member
    Posted 2 years ago #

    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

  7. Andrew
    Forum Moderator
    Posted 2 years ago #

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

  8. pcmdv
    Member
    Posted 2 years ago #

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

    which doesn't make an awful lot of sense...?

  9. Andrew
    Forum Moderator
    Posted 2 years ago #

    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.

  10. alchymyth
    Forum Moderator
    Posted 2 years ago #

    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

  11. pcmdv
    Member
    Posted 2 years ago #

    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?

  12. pcmdv
    Member
    Posted 2 years ago #

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

  13. alchymyth
    Forum Moderator
    Posted 2 years ago #

    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" '); ?>
  14. pcmdv
    Member
    Posted 2 years ago #

    amazing! that is it!

    It's fully working!

    Thank you very much!

Topic Closed

This topic has been closed to new replies.

About this Topic