【问题标题】:Code to display articles from category ID?从类别 ID 显示文章的代码?
【发布时间】:2012-12-13 13:16:08
【问题描述】:

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

在 Wordpress 中,这样做相当容易:

<?php
   query_posts('cat=1');
   while (have_posts()) : the_post();
   the_title();
   endwhile;
?>

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

【问题讨论】:

    标签: joomla joomla2.5


    【解决方案1】:

    在 joomla 中没有直接的代码可以像 word press 一样获得这个。

    如果您想检查代码,您可以通过以下路径检查实现此目的的代码。

    components/com_content/view/category/view.html.php
    
    and 
    components/com_content/view/category/tmpl/blog.php
    

    根据我的猜测,您的要求是显示同一类别的文章。

    然后在 joomla 中你不需要编辑任何代码。 为此,您可以简单地创建一个菜单。 并且菜单布局类型应该是类别博客并从右侧类别选项中选择您的类别。

    这将获得该类别的完整文章。

    如果你想以你的风格管理它,你可以添加基于 blog.php 文件的风格。

    希望对你有所帮助..

    【讨论】:

      【解决方案2】:
      $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;
      }
      

      【讨论】:

      • 绝妙的答案。只是想要它。
      • 我也可以使用此数据库查询获取自定义字段吗?它们没有出现在我的对象中:/
      【解决方案3】:

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

      $categoryId = $[category id];
      
      require_once("components/com_content/models/category.php");
      $category = new ContentModelCategory();
      $category->hit($categoryId);
      $articles = $category->getItems();
      print_r($articles);
      

      【讨论】:

      • 考虑添加此代码如何工作的摘要以充实您的答案。
      【解决方案4】:

      试试这个:

      $articles = JFactory::getDBO()->setQuery("SELECT * FROM #__content WHERE catid = '21'")->loadObjectList();
      

      当然用你的类别的 id 替换 '21'。

      【讨论】:

        【解决方案5】:

        这在 Joomla 中很容易。您可以在 getItems()

        下的 /components/com_content/models/category.php 中找到代码

        下面是一个例子

        $model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
        $model->setState('params', JFactory::getApplication()->getParams());
        $model->setState('filter.category_id', $category_id);
        
        $articles = $model->getItems();
        

        【讨论】:

          猜你喜欢
          • 2021-12-24
          • 2014-10-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多