Tuesday, October 18th, 2005


Another Blogsome user wanted a Random Post to be displayed on the home page. I haven’t quite figured out how to get the actual post displayed, but a link to a random post should be pretty easy.

Luckily, WP-MU uses post ids. If we can get the latest post id, then we should be able to get a random number between 1 and this. We can then check to see if the post exists (it may have been deleted: WP does not reuse post ids), and if it doesn’t, get another random number, and keep doing this until we find a post id that is still valid.

It’s then a simple step to create the permalink.

The first problem arises when we try to get the latest post. We could use {the_author_posts}, but this will give us the number of posts, which means if the user has deleted n posts, the last n posts will not be in the selection of random posts. This is not necessarily such a bad thing, as it will mean that older posts are likely to be used. The next problem of {the_author_posts} is that it must be used within The Loop. If we are planning to put the link before we have been through ‘The Loop’, then we should be alright. The third issue of {the_author_posts} is that with a multi-author blog, the random number will be much lower than the total number of posts, meaning even older posts are only likely to be selected from.
(more…)

View Comments (5)   RSS Feed for Comments on this Post

A user on the Blogsome forums wanted to be able to reduce the number of items that appeared in a Smarty generated list. Since the original code was using {foreach}, and I know {section} has a max=n attribute, I thought I’d try that.

The problem was that the Array in question: {popularposts} generates $pposts; is not an array that can be accessed by indices. It’s key instead is the number of hits on the pages, so that’s not much good to us.

However, it’s possible to use a simple {if} clause, and the {counter} function, to effectively limit the number of iterations.

{counter assign=idx print=0}
{foreach from=$array key=key item=item}
    {counter}
    {if $idx < = n}
        Data to be repeated goes in here.
    {/if}
{/foreach}

Replacing n with a number will cause n-1 iterations to be displayed. I said effectively above, as the other iterations will actually occur, but no data will actually be printed on the screen, as there is no {else} clause.

View Comments (4)   RSS Feed for Comments on this Post

After hacking through the source, I finally figured out how to make {relatedstories} work.

The trick is that it stores the date in a variable called $relatedstories.

{relatedstories}
{if $relatedstories}
  <h2>Related Stories</h2>
  <ul>
  {foreach from=$relatedstories key=key item=story}
    <li> <a href="{get_permalink id=$story->ID}" >
        {$story->post_title}</a>
    </li>
  {/foreach}
  </ul>
{/if}

I’ve got it set up in my sidebar at the moment. It’s smart enough to figure out when it’s not a single post page, and won’t show then, so you don’t need any fancy extra Smarty Tags to only show in single post view.

Oh, and it also sets the variable $relatedstoriesWords, so it’s possible to do something like:

<h2 title="{$relatedstoriesWords}">Related Stories</h2>

and when you hover over the title, it tells you the words it’s looking for.

I’m still working on how to truncate the title. {$story->post_title|truncate} fails dismally. Quite annoying.

View Comment (1)   RSS Feed for Comments on this Post

Blogsome has implemented RSS feeds in the forums:

RSS: click if your RSS syndicator can handle feed: URIs.

RSS: an http: link to the feed.

View Comment (1)   RSS Feed for Comments on this Post