用于显示类别ID的文章的代码?

Rhy*_*Guy 2 joomla joomla2.5

从类别(由ID指定)显示文章的代码是什么?

在Wordpress中,这样做非常简单:

<?php
   query_posts('cat=1');
   while (have_posts()) : the_post();
   the_title();
   endwhile;
?>
Run Code Online (Sandbox Code Playgroud)

我正在为Joomla寻找类似的代码.

bru*_*tas 10

$db = JFactory::getDbo();
$id = 50;//example

$query = $db->getQuery(true);
$query->select('*');
$query->from('#__content');
$query->where('catid="'.$id.'"');

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

foreach($res as $r){
    echo '<h3>'.$r->title.'</h3>';
    echo $r->introtext;
}
Run Code Online (Sandbox Code Playgroud)


小智 5

您可以使用下一个代码模板:

$categoryId = $[category id];

require_once("components/com_content/models/category.php");
$category = new ContentModelCategory();
$category->hit($categoryId);
$articles = $category->getItems();
print_r($articles);
Run Code Online (Sandbox Code Playgroud)

  • 考虑添加有关此代码如何工作的摘要,以充实您的答案。 (5认同)