• Resolved Mad Max

    (@mad_max)


    Hello, I’m using this plugin shortcode:
    [SimpleYearlyArchive type="yearly"]
    with linked years option turned on in the option panel on one particular category of my posts.
    I’ve also added a rewrite endpoint with
    add_rewrite_endpoint( 'year', EP_CATEGORIES );
    so now I have urls like this on yearly archives:

    /news/bacheca/year/2022/

    where “news” it’s the category base for posts in permaliks options, “bacheca” is a category term and the year value is automatically used in the query var “year”, so I can do a
    get_query_var('year')
    to get the current shown year.

    The question is: how can I add a “current” class to shortcode generated links so I can highlight the link corresponding to the current shown year archive?
    Something similar to the “current-item” class in wordpress menus.

    Thanks, Daniele.

    • This topic was modified 2 years, 5 months ago by Mad Max.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author wpseek

    (@alphawolf)

    Hi @mad_max, unfortunately that’s not possible out-of-the-box but if you’re familiar with javascript you could read the year from the uri and highlight all links grouped under the corresponding year in the archive listing.

    Thread Starter Mad Max

    (@mad_max)

    Hi @alphawolf, thanks for your replay. I’ve modified one line in your plugin to add the class “current” to generated links, based on the query_var “year” value.
    I’m wondering if you could add a filter for the links in an upcoming version of your plugin, so I can modify links the same way, but without touching your source code.

    Thanks, Daniele.

    Plugin Author wpseek

    (@alphawolf)

    Hi @mad_max,

    would you please try replacing the contents of the simple-yearly-archive.php plugin file with the following contents: https://pastebin.com/PZv8sFnq

    You should then be able to use a filter sya_postlink in your functions.php, e.g.:

    function sya_modify_postlinks( $link_html, $post ) {
    	if( $post->post_year == '2012' ) { // or whatever
    		$dom = new DOMDocument();
    		$dom->loadHtml($link_html);
    		$xpath = new DOMXPath($dom);
    		$link = $xpath->query("//a")->item(0);
    		$link_classes = explode(' ', $link->getAttribute('class'));
    		$link_classes[] = 'current-link'; // the class name(s) to add
    		$link->setAttribute('class', implode(' ', $link_classes));
    		return $dom->saveHTML();
    	}
    	return $link_html;
    }
    add_filter( 'sya_postlink', 'sya_modify_postlinks', 10, 3 );
    

    Thank you!

    • This reply was modified 2 years, 5 months ago by wpseek.
    Thread Starter Mad Max

    (@mad_max)

    Hi @alphawolf, and thanks for your time, really appreciate it.

    In the functions.php code you sent me, I’ve replaced the IF test with this:

    if (is_integer( get_query_var('year') ) && get_query_var('year') > 0 && get_query_var('year') == $post->post_year ){

    to get the currently viewed archive year value. Now, with this code and the new version of the plugin file, the class “current-link” is added in to the <ul><li>, on every post link listed under the currently viewed year of posts, as you can see in this screenshot. In this case, all <ul></li> are hidden because I’m not using “Collapsible years” in the plugin options, and each year is linked as I’m using linked years option turned on.

    Indeed, what I actually need to do, is to add the class “current” (or “current-link”) to the Year link (see screenshot), not to the listed posts within.
    You can see the yearly archive pages I’m talking about here.
    If you click on year links, you can see what I got with my change to your plugin.
    Specifically, what I did was change line 285 from this

    $output .= $before . '<a id="year' . $year . '"></a>' . $linkyears_prepend . $year . $linkyears_append;

    to this

    $current = (is_integer( get_query_var('year') ) && get_query_var('year') > 0 && get_query_var('year') == $year )?'current':'';
    $output .= $before . '<a id="year' . $year . '" class="'.$current.'"></a>' . $linkyears_prepend . $year . $linkyears_append;

    So, to sum up it up, I’d need to have a filterable string of classes, added to every <a id="year' . $year . '"></a> links.
    Something like:

    $yearclasses = "";
    $yearclasses = apply_filters("sya_yearlink", $yearclasses, $year);
    $output .= $before . '<a id="year' . $year . '" class="'.$yearclasses.'"></a>' . $linkyears_prepend . $year . $linkyears_append;
    

    Then in my function.php, I can use:

    function sya_modify_year_classes( $yearclasses, $year ) {
    	if (is_integer( get_query_var('year') ) && get_query_var('year') > 0 && get_query_var('year') == $year ){
    		$classes = explode(" ", $yearclasses);
    		$classes[] = "current";
    		$yearclasses = implode( " ", $classes);
    	}
    	return $yearclasses;
    }
    add_filter( 'sya_yearlink', 'sya_modify_year_classes', 10, 3 );

    Sorry for the low level of details in my previoius post. Hope the issue is more clear now.

    Daniele

    • This reply was modified 2 years, 5 months ago by Mad Max.
    • This reply was modified 2 years, 5 months ago by Mad Max.
    Plugin Author wpseek

    (@alphawolf)

    Hi @mad_max, please try this source: https://pastebin.com/eNyix4w7 which adds a new filter sya_yearanchor you can use in your functions.php, e.g.:

    function sya_modify_yearanchors( $link_html, $year ) {
    	if( $year == '2012' ) {
    		$dom = new DOMDocument();
    		$dom->loadHtml($link_html);
    		$xpath = new DOMXPath($dom);
    		$link = $xpath->query("//a")->item(0);
    		$link_classes = explode(' ', $link->getAttribute('class'));
    		$link_classes[] = 'current';
    		$link->setAttribute('class', trim(implode(' ', $link_classes)));
    		return $dom->saveHTML();
    	}
    	return $link_html;
    }
    add_filter( 'sya_yearanchor', 'sya_modify_yearanchors', 10, 3 );

    Again I’m using DOMXPath which offers a lot of variety in customizing the links in addition to just changing/adding CSS class names.

    Lemme know if that’s working for you. ??

    Best,
    Oliver

    Thread Starter Mad Max

    (@mad_max)

    It works great, thanks a lot!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘add class “current” to year link’ is closed to new replies.