简体   繁体   中英

Joomla - get array of article IDs, retrieve text, and link to them

I'm building a Joomla module that can get an array of all articles on the site -- regardless of category -- then identify the category, retrieve intro text and title, and display the titles, categories, and intro text of the most recent two articles in each category in a tiled layout. I have the layout done, but I don't know where to start on the rest. Is it possible?

I'm not averse to getting the articles from category blogs, but I'm not sure if that's possible.

There is the best way to retrieve the articles:

$jcontent=JControllerLegacy::getInstance('Content');
$jarticles=$jcontent->getModel('Articles');
$jarticles->getState();
$jarticles->setState('filter.article_id', $ids);
$jarticles->setState('list.limit', count($ids));
$jarticles->setState('filter.published', 1);
$articles=$jarticles->getItems();

This code was tested and, as for me, this is the best way - it uses Joomla abstractions to retrieve the articles, it uses Joomla caching and is not dependent on database structure.

This is code is not tested, you may need to check and make minor modifications.

Method 1 : In this you need to query the category details again from the category id you get inside the loop.

$db = JFactory::getDbo();

$query = $db->getQuery(true);
$query->select('*');
$query->from('#__content');

$db->setQuery((string)$query);
$res = $db->loadObjectList();

foreach($res as $r){
    //query category details also here
    echo '<h3>'.$r->title.'</h3>';
    echo $r->introtext;
}

Method 2 : In this method you are supposed to get both content and category details in one query. In the select query you need to include the field names which you need.

$db = JFactory::getDbo();
$db->setQuery('SELECT #__content.title as contentTitle, #__categories.title as catTitle FROM #__content, #__categories WHERE #__content.catid = #__categories.id'); 
$details = $db->loadObjectList();
print_r(details);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM