x
 
Create New Page
 View Previous Changes    ( Last updated by Phoebe )

External Latest Entries

List Latest Entries Externally

I have a home page outside EE but want to list the latest news items with links to the EE part of the site. I have used this snippet of code that can be adapted as required for other sites/uses:

<?php
// hard code paths to config and system folders here, with trailing /
$config_path='../news/';
$system_path='../news/system/';

// get id for weblog (left hand side of Admin,Weblog Management)
$weblog_id=23;

// number of items to display
$num=6;

// template
$before='<ul>';
$each='<li><a href="http://news.energyscenariosireland.com/index.php/esi/more/{url_title}/">{title}</a></li>';
$after='</ul>';

// only show certain categories (ID visible in Admin, Category Management, Add/Edit)
// comment out this line if not required
//$catlist=array(43,10,6); can supply array
$catlist=43; // or just one

DEFINE('EXT','.php');


require(
$config_path.'config.php');
require (
$system_path.'db/db.mysql.php');


$db_config = array(
'hostname' => $conf['db_hostname'],
'username' => $conf['db_username'],
'password' => $conf['db_password'],
'database' => $conf['db_name'],
'prefix' => $conf['db_prefix'],
'conntype' => $conf['db_conntype'],
'debug'=> ($conf['debug'] != 0) ? 1 : 0,
'show_queries'=> ($conf['show_queries'] == 'y') ? TRUE : FALSE,
'enable_cache'=> ($conf['enable_db_caching'] == 'y') ? TRUE : FALSE
);

$DB = new DB($db_config);
$DB->db_connect();
$DB->enable_cache = FALSE;


// get latest news items for specified weblog

$sql='SELECT title,url_title
FROM exp_weblog_titles t'
;

if (isset(
$catlist)) {

$sql
.=', exp_category_posts c
WHERE weblog_id='
.$weblog_id.' AND
t.entry_id=c.entry_id AND '
;

if (
is_array($catlist))
$sql.='cat_id IN ('.implode(",",$catlist).') ';
else
$sql.='cat_id ='.$catlist;

} // category specified

$sql.=' ORDER BY entry_date DESC LIMIT '.$num;

$query = $DB->query($sql);


if (
$query->num_rows == 0) return false;

// built HTML

$latest=$before;
foreach (
$query->result as $row) {
$latest
.= str_replace(array('{title}',
'{url_title}'),
array(
$row['title'],
$row['url_title']),
$each);
}
$latest
.=$after;

echo
$latest;

$DB->db_close();
?>

Category:Tricks

Categories: