!!Mixing linkblog posts with main posts
! Background
Link(b)logs have been around for a while - amongst the many people who use them see (for example) [http://kottke.org/ "Jason Kottke"]. Many people like to use them as a way of capturing things which they wish to note but which they do not wish to write a full post about.
There are two main forms of displaying linklogs - inline or in a sidebar. Entirely a design preference, with my previous MoveableType blog I used a sidebar. However my three-column site had become very cluttered and I had been thinking about moving back to a two-column layout for some time. I have taken advantage of the shift to WordPress to achieve this.
! Design Goal
The look I wanted to achieve was to group all linklog entries for a given day together and display them after all the normal entries for that day (if any) regardless of the order in which ordinary and linklog posts were made.
! Implementation Approach
# This method uses a specific category in the WordPress blog to collect all linklog entries. This approach is documented in many places, for the clearest (and also the source of the code snippets I have used for selecting the special category and formatting the linkblog entries) see [http://photomatt.net/archives/2004/05/19/asides/ "Photo Matt > On Asides"].
# Within the main display loop of the Wordpress blog an if statement is used to identify if the current post belongs to the special category - if it is it isn't displayed immediately but collected in an array.
# Code is inserted to detect the end of a date block, and if there are any linklog posts for that day they are inserted at that point.
# Finally another post loop is used to post any linklog entries after the last entry on the page.
! Caution
To implement this change requires a fair amount of hacking of the WordPress standard template. Even if you are very sure of what you are doing I strongly recommend you make a backup copy of your current installation before hacking commences.
! Code
!! Set up category
Create a new category in your WordPress blog to be used for all linklog entries. From the category listing screen identify the number of this category and use at the appropriaite place in the code below. (for my example it was category 35)
!! Identify the Loop
Identify the main WordPress [http://wiki.wordpress.org/index.php/TheLoop "Loop"] - it's the part of index.php that starts
<code>
<?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>
</code>
and ends
<code>
<?php endforeach; else: ?>
<p><?php _e(\'Sorry, no posts matched your criteria.\'); ?></p>
<?php endif; ?>
</code>
This is where most of the changes will be made.
! New Code
<code>
<?php

$quicklinks = array(); used to collect linklog posts
$prevdate = \'\';

main loop start

if ($posts) : foreach ($posts as $post) :
start_wp();
$currentdate = mysql2date(\'d-m-Y\', $post->post_date);
note this breaks the date format control

if ($currentdate <> $prevdate ) {
this means we are about to start a new day\'s collection of posts

$temp_post = $post; remember main loop context
if ($quicklinks) {
put quicklinks here
then empty quicklinks ready for next dayset
echo \'<h3 class="storytitle">Quick Links</h3>\';
echo \'<ul class="quicklinks">\';
foreach ($quicklinks as $ql) :
start_wp();
$post = $ql;
need this to use the global $post for calls such as the_content()
start_wp();
echo \'<li id="p\' , the_ID(),\'">\';
echo the_content();
echo \' \';
comments_popup_link(\'(0)\', \'(1)\', \'(%)\');
edit_post_link(\'(e)\');
echo \'</li>\';
$dummy = array_pop($quicklinks); discard - we want to end with an empty array
endforeach;
echo \'</ul>\';
}

$post = $temp_post;
restore context

start_wp(); restore WP state

we are now ready to start outputting the next day\'s posts

echo \'<h2>\', $currentdate, \'</h2>\';

}

$prevdate = $currentdate;

is this post a linklog entry?
if (in_category(35) && !$single) {
$quicklinks[] = $post;
} else { ?>

<div class="post">

<!--
you should reinsert here the HTML from your original template for:
post header
post body
comments links
trackback RDF
-->

<?php include(ABSPATH . \'wp-comments.php\'); ?>

</div>

<?php } ?>

<?php endforeach; else: ?>
<p><?php _e(\'Sorry, no posts matched your criteria.\'); ?></p>

<?php
endif;
end of main loop
?>

<?php
finally we need another loop to catch the case where
the last day on the page has some linklog entries

<?php
if ($quicklinks) : ?>
<h3 class="quicklinkhdr">Quick Links</h3>
<ul class="quicklinks">
<?php foreach ($quicklinks as $post) : safe to use $post here as not in main loop
echo \'<li id="p\', the_ID(), \'">\';
echo the_content();
echo \' \';
comments_popup_link(\'(0)\', \'(1)\', \'(%)\',\'\',\'\');
edit_post_link(\'(e)\');
echo \'</li>\';
endforeach ;
echo \'</ul>\';
endif; ?>
</div> <!-- /content -->
<!-- rest of template beyond here is untouched -->
?>
</code>
! To Do
* Tidy up code, especially to restore functionality of date format control
* Import old linkblog entries from MoveableType
! License
You can do what you like with this provided you accept that any consequences are entirely your problem!
A credit would be nice but not essential.


CategorySiteDesign
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki