News Headlines on Index Page (Technics)
Hi,
Anybody have a quick easy way of displaying a 'latest new' list of titles/links to news articles on the main index page?
News Headlines on Index Page
I love this thing!
Anyway, to save time for those getting started...
set content_function to 1 in Administration → Settings → Advanced settings
Throw the php function below into includes/functions.inc.php and edit as necessary
--------------------------------------
function get_latest_news_headlines($limit) {
$dbr = Database::$content->query("
SELECT page, title, time FROM ".Database::$db_settings['pages_table']." WHERE sections = 'news' AND content_type = '' AND status > 0 AND NOT type = 'news' ORDER BY time DESC LIMIT " . $limit);
$headlines = '';
while ( $row = $dbr->fetch() ) {$headlines .= "<li><a href=\"" . $row['page'] . "\">" . $row['title'] . "</a><span class=\"timestamp\"> " . date("l, M j, Y",$row['time']) . "</span></li>";}
return $headlines;
}
--------------------------------------
and add the following into the index page.
<ul id="headlines">
[function:get_latest_news_headlines(5)]
</ul>
News Headlines on Index Page
Ryan, Great job. After Section="news" what is the purpose of a null Content_type condition?
News Headlines on Index Page
you could pull the content_type condition out of the query, it was left there from testing. '' is the default value for content_type as far as the database is concerned, and phpsqlitecms interprets the '' value as text/html.
News Headlines on Index Page (using simple news)
Here's a function for grabbing the latest simple news headlines as links. Use $limit to determine how many headlines to show at the most.
NOTE: place this function in functions.content.inc.php (new) instead of functions.inc.php as mentioned in my firt post.
function get_simple_news_headlines($limit) { $dbr = Database::$content->query(" SELECT newsitem.id, newsitem.title, newsitem.time, page.page FROM ".Database::$db_settings['news_table']." AS newsitem LEFT JOIN ".Database::$db_settings['pages_table']." AS page ON newsitem.page_id = page.id ORDER BY newsitem.time DESC LIMIT " . $limit ); $headlines = ''; while($row=$dbr->fetch()){ $headlines .= "<li><a href=\"". $row['page'] .",". $row['id'] ."\">". $row['title'] ."</a> <span class=\"timestamp\">".date("M jS, Y, g:i a",$row['time'])."</span></li>"; } return $headlines; }
Call the function from any page content like so ...
<ul>[function:get_simple_news_headlines(5)]</ul>