wp_get_archives link to that particular archive page

Setup an Archive list on the right of my page and the design has a “view” button after each item.

I’m trying to have that view button link to the archive month page.

Read More

Looking at doing something with the after and adding a href=”” to the view button, However not sure what to reference to get that to happen.

My current code is as follows :

                <?php
                // Get Archives'
                $args = array (
                    'type'  => 'monthly',
                    'order' => 'DESC',
                    'after' => '<div class="pull-right"><a class="view" href="#">View</a></div>',
                    'limit' => '6'
                );
                $archives = wp_get_archives( $args );
                ?>

As you can see the ‘after’ parameter in the array is where I am trying to add the href.

Hope that makes sense.

Thank You!

Related posts

1 comment

  1. Few things about wp_get_archives:

    • It doesn’t return anything except if you force the echo parameter to 0 – otherwise calling the function will result of the archive links to be printed.

    • The after parameter is dependant of the format parameter – it is only used when using the “html” (default – list mode) or “custom” format. What it does is to display the value you pass as parameter after the link. So you don’t need to reference the link inside. Use it in combination with the before parameter to achieve what you want to do here.

    • You don’t really need to set the type as monthly, as it is the default value for this parameter. Same for the order parameter, which default is allready DESC.

    So a valid call would be:

    wp_get_archives(array(
        'format' => 'custom',
        'before' => '<div class="pull-right">',
        'after'  => '</div>',
        'limit'  => 6
    ));
    

    You probably notice that it doesn’t output exactly what you are trying to do, as it miss the class view on your links. You need to add a filter on get_archives_link in order to achieve this (this go in your theme functions.php):

    add_filter('get_archives_link', 'get_archive_links_css_class' );
    function get_archive_links_css_class($link) {
        return str_replace('href', 'class="view" href', $link);
    }
    

    This will add the class before the href attribute.

Comments are closed.