How do I display titles and links to recently created articles in the Articles block?
You want a list of articles, sorted by date, with the most recent at the top? What exactly do you want to display? Just the title and link? Or do you want the date as well? Intro? Author? Number of views? Number of comments? Rating?
Just the title and a link would be good. A date would be nice too I guess.
I've tried looking at the block's settings but I can't get it to display just what I need.
In a php block:
global $db_prefix, $scripturl, $user_info;
$query = db_query(
"SELECT subject, id, date
FROM {$db_prefix}tp_articles
WHERE approved = 1
ORDER BY date DESC
LIMIT 10", __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($query))
{
echo '<a href="' . $scripturl . '?page=' . $row['id'] . '">' . $row['subject'] . '</a> ' . timeformat($row['date'], $user_info['time_format']) . '<br />';
}
Perfect - thanks!
I saw that you wanted to modify this to use the short title.
global $db_prefix, $scripturl, $user_info;
$query = db_query(
"SELECT subject, shortname, date
FROM {$db_prefix}tp_articles
WHERE approved = 1
ORDER BY date DESC
LIMIT 10", __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($query))
{
echo '<a href="' . $scripturl . '?page=' . $row['shortname'] . '">' . $row['subject'] . '</a> ' . timeformat($row['date'], $user_info['time_format']) . '<br />';
}
That should work.
Yes, it works fabulously - thanks again ;)
Actually, JP, that latest version won't link properly if the 'shortname' has no value. I added the 'if' clause to swap to the article's id number if 'shortname' was empty.
global $db_prefix, $scripturl, $user_info;
$query = db_query(
"SELECT subject, id, date, shortname
FROM {$db_prefix}tp_articles
WHERE approved = 1
ORDER BY date DESC
LIMIT 10", __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($query))
{
if(empty($row['shortname'])) $row['shortname'] = $row['id'];
echo '<a href="' . $scripturl . '?page=' . $row['shortname'] . '">' . $row['subject'] . '</a> ' . timeformat($row['date'], $user_info['time_format']) . '<br />';
}
Good idea. Thanks!! :)