• Resolved raw-m

    (@raw-m)


    Hi everyone

    I’m trying to display the list of years with a custom background image specific to each year. I need a custom hook in the html for the css to get hold of.

    I’ve customised wp_get_archives to strip out the default <li> and include a new one by changing the “format” to “custom” and using the before/after to include an <li> with a class (see below). Does anyone know if I can insert the permalinks (or any other attribute/php for that matter) into my class tag?

    wp_get_archives(array('format'=>'custom','type'=>'yearly','before'=>'<li class="PERMALINK HERE!">','after'=>'</li>
    '));
Viewing 6 replies - 1 through 6 (of 6 total)
  • If you can hand-code a couple of items to look the way you want, and paste the code here, I will try to generate that code from wp_get_archives() by exploding the string returned and preg_replacing selected parts.

    Thread Starter raw-m

    (@raw-m)

    Hi vtxyzzy, many thanks for your offer

    This is basically what I’m after (I’m not on my dev computer but the hrefs should be the same as is outputed by using wp_get_archives):

    <li class="2010"><a href="2010">2010</a></li>
    <li class="2009"><a href="2009">2009</a></li>
    <li class="2008"><a href="2008">2008</a></li>...

    I’ve tried to recreate it using get_posts but I can’t find details on how to return a list the same as “type=yearly”.

    See if the code below will do what you want:

    $linkstring = wp_get_archives(array('echo' => 0,'format'=>'custom','type'=>'yearly','before'=>'|'));
    $linkarray = explode('|',$linkstring);
    if ( get_option('permalink_structure') != '' ) {
      $pattern = '#/(\d\d\d\d)/?[^>]+?>#';
    } else {
      $pattern = '/m=(\d\d\d\d)[^>]+?>/';
    }
    $output = "<ul>\n";
    foreach ($linkarray as $onelink) {
      if (preg_match($pattern,$onelink,$matches)) {
         $output .= "<li class='{$matches[1]}' >$onelink</li>\n";
      }
    }
    $output .= "</ul>\n";
    echo $output;
    Thread Starter raw-m

    (@raw-m)

    vtxyzzy, you’re a star!! Massive thanks 🙂

    Glad it worked! Now, please use the dropdown at top right to mark this topic ‘Resolved’ so that anyone researching a similar problem can see that there is a solution.

    Thread Starter raw-m

    (@raw-m)

    It is done! Thanks again.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Inserting permalink into wp_get_archives’ is closed to new replies.