Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Change the class of a blogroll link if older than 30 days WordPress

  • SOLVED

Hi,

I have this code:

<ul><?php wp_list_bookmarks('title_li=&categorize=0&category=89&orderby=name'); ?></ul>

It outputs the bookmarks/blogroll links from the link category 89, orders the list alphabetically and removes the heading. Easy :)

It contains many bookmarks but I'd like to change the CSS class of links that are older than 30 days (or any other number of days). The reason I want to do this is I want to automate the process of adding a 'new' badge next to the most recent links.

Here's some examples so you can have a better idea of what I'm trying to do:

If bookmark is added today = CSS class of 'new'
If bookmark was added last year = CSS class of 'old'
If bookmark was added 29 days ago = CSS class of 'new'
If bookmark was added 30 days ago = CSS class of 'new'
If bookmark was added 31 days ago = CSS class of 'old'

So, is it possible?

Thanks a lot!



<strong>*UPDATE: </strong> Thanks a lot guys! I just tried both solutions.

For some reason Japh's solution made my H2 headings (which I'm adding manually) turn into links and also did something weird with some of my jQuery ([[LINK href="http://jquery.kuzemchak.net/collapsorz.php"]]collapsorz plugin[[/LINK]] acting weird)... I'm not sure what happened, but it does looks like a very promising solution since it doesn't need editing of a core file. Is there maybe a typo in the code provided that may cause the headings to turn into links?

As for Utkarsh's solution, well, even though it means editing a core file, it worked great! Actually I don't mind editing a core file. Of course I'd prefer not to, but it's still a viable solution. I should also mention that Utkarsh's solution uses the Link Updated plugin: [[LINK href="http://wordpress.org/extend/plugins/link-updated/"]]http://wordpress.org/extend/plugins/link-updated/[[/LINK]]

I think both your solutions are pretty good nonetheless. Thanks a lot! :)

<strong>UPDATE #2:</strong> I just upped the prize amount a bit and will award both of you half of the amount as soon as my Paypal payment shows up here.

Thanks a lot for the help, Japh and Utkarsh! :)

Answers (2)

2010-01-08

Utkarsh Kukreti answers:

You will have to edit the file wp-includes/bookmark-template.php
Around line 99
from
$output .= '<a href="' . $the_link . '"' . $rel . $title . $target . '>';
to
$output .= '<a href="' . $the_link . '"' . $rel . $title . $target . custom_link_class($bookmark) . '>';


Then add the following code to theme functions.php
function custom_link_class($bookmark)
{
$days = 30;
$time = $days * 86400;
if(strtotime($bookmark->link_updated) + $time > time())
{
return ' class="new"';
}
return '';
}



Edit: looks like Japh missed a </a> in that
$str .= '<li><a' . ($datediff <= $days_old ? ' class="new"' : '') . ' href="' . $b->link_url . '" title="' . $b->link_description . '">' . $b->link_name . '</a></li>';

Excellent solution anyways!

2010-01-08

Japh answers:

Place the following in your functions.php file for the active theme:

function edit_link_update($link_ID) {
global $wpdb;
$sql = "update wp_links set link_updated = NOW() where link_id = " . $link_ID . ";";
$wpdb->query($sql);
}
add_action('edit_link', 'edit_link_update');

function get_bookmarks_fresh($args, $days_old = 30) {
$bookmarks = get_bookmarks($args);
$str = '';
foreach ($bookmarks as $b) {
$datediff = ((((date('U') - date('U', strtotime($b->link_updated))) / 60) / 60) / 24);
$str .= '<li><a' . ($datediff <= $days_old ? ' class="new"' : '') . ' href="' . $b->link_url . '" title="' . $b->link_description . '">' . $b->link_name . '</a></li>';
}
echo $str;
}


Then where you would normally put "wp_list_bookmarks", put "get_bookmarks_fresh" instead, like:

get_bookmarks_fresh('orderby=name');


I have found that "link_updated" doesn't seem to be set correctly in a default WP install, which is why the 'edit_link_update' function is there.

Also, I should mention that editing core file like "/wp-includes/bookmark-template.php" is not such a great plan, as an upgrade may destroy your changes.

<strong>Edit:</strong> Fixed the typo causing link troubles! Sorry about that :)