Monday, May 12, 2008

Page Pertinent Link Lists

I'm using WordPress as a web site management tool. As such, on my pages I want to have only those links that are pertinent to the page in the link list. This can be done automatically by firs managing the link list categories to have slugs that match those of the page they belong to. Then the following code is place in the right-sidebar.php file:


right-sidebar.php
<?php if (is_home() || is_front_page()) {
echo "<li><h2>"; _e('Recent Posts','andreas09'); echo "</h2>\n<ul>";
wp_get_archives('type=postbypost&limit=10');
echo "</ul></li>\n";
} ?>

This will place the recent posts widget on the home page and post page.



<?php
$cat_found = 0;
// Get all the link categories
$categories = get_categories('type=link');
foreach ($categories as $cat) {
// If the category slug matches the page slug
if ($post->post_name == $cat->category_nicename) {
// no more looking
$cat_found = 1;
// output bookmarks from the category found
wp_list_bookmarks('category='.$cat->cat_ID);
// done
break;
}
}

// If no categories match the page, output default bookmarks
if ($cat_found == 0) {
echo "\n<!-- default -->\n";
wp_list_bookmarks('category=8&orderby=updated&show_updated=1');
wp_list_bookmarks('category=2');
}
?>


Updated to Wordpress 2.6.2 so here is the new wp_widget_links from widgets.php:


function wp_widget_links($args) {
global $post;

extract($args, EXTR_SKIP);

$cat_found = 0;
$categories = get_categories('type=link');
$before_widget = preg_replace('/id="[^"]*"/','id="%id"', $before_widget);

foreach ($categories as $cat) {
echo "<!-- post_name ", $post_ID, " ", $post->post_name, " category_nicename ", $cat->category_nicename, " -->\n";
if ($post->post_name == $cat->category_nicename) {
$cat_found = 1;
wp_list_bookmarks(apply_filters('widget_links_args', array(
'title_before' => $before_title, 'title_after' => $after_title,
'category_before' => $before_widget, 'category_after' => $after_widget,
'show_images' => true, 'class' => 'linkcat widget', 'category' => $cat->cat_ID
)));
break;
}
}

if ($cat_found == 0) {
wp_list_bookmarks(apply_filters('widget_links_args', array(
'title_before' => $before_title, 'title_after' => $after_title,
'category_before' => $before_widget, 'category_after' => $after_widget,
'show_images' => true, 'class' => 'linkcat widget', 'category' => 8
)));
wp_list_bookmarks(apply_filters('widget_links_args', array(
'title_before' => $before_title, 'title_after' => $after_title,
'category_before' => $before_widget, 'category_after' => $after_widget,
'show_images' => true, 'class' => 'linkcat widget', 'category' => 2
)));
}
}

--

No comments:

Post a Comment