在Joomla中如何通过文章ID获取文章文本?

8
我希望通过在 Joomla 模板中传递文章 ID 来获取文章文本。
4个回答

17

简单来说,只要你在post/get中附带文章ID并使用"id"作为其编号即可:

$articleId = JRequest::getInt('id');
$db =& JFactory::getDBO();

$sql = "SELECT fulltext FROM #__content WHERE id = ".intval($articleId);
$db->setQuery($sql);
$fullArticle = $db->loadResult();

if(!strlen(trim($fullArticle))) $fullArticle = "Article is empty ";

编辑:从任何地方获取文章ID的方法:

$articleId = (JRequest::getVar('option')==='com_content' && JRequest::getVar('view')==='article')? JRequest::getInt('id') : 0;

谢谢。有没有办法通过传递文章ID从Joomla模板获取文章内容? - Aryan G
articleId = JRequest::getInt('id'); 文章编号 = JRequest::getInt('id'); - WooDzu
1
如果您想使用固定的文章ID,请使用模板参数:http://docs.joomla.org/Template_parameters - WooDzu
你能给我提示如何获取文本直到切割点吗? - Eugene Shmorgun

7

在Joomla 2.5中(根据文档,3也可以),插件中通过文章ID获取文章文本:

$article =& JTable::getInstance("content");
$article->load($id);
$content = '<h3>'. $article->get("title").'</h3>';
$content .= $article->get("introtext"); // introtext and/or fulltext

文章ID可以从组件/插件参数中获取,例如:
  1. From inside own component:

    $app = JFactory::getApplication();
    $params = $app->getParams();
    $param = $params->get('terms_article_id');
    
  2. From other component:

    $params = JComponentHelper::getParams('com_mycom');
    $id = $params->get('terms_article_id');
    
  3. Get Module Parameter from template's php file:

    $module = JModuleHelper::getModule('mod_mymodule');
    $params = new JRegistry($module->params); // or $mymoduleParams if few used
    $id = (int) $headLineParams['count'];
    

7

尝试这个技巧:

$article = JControllerLegacy::getInstance('Content')->getModel('Article')->getItem($articleId);
echo $article->introtext;

1
只要您从另一篇文章访问文章内容,它就可以正常工作。但是一旦您进入模板代码,它就停止工作了。 - Samsky

4

Joomla有从SQL表中获取内容的默认脚本。

这里是文章 (#__content)

获取文章ID:

$articleId = (JRequest::getVar('option')==='com_content' && JRequest::getVar('view')==='article')? JRequest::getInt('id') : 0;

获取文章内容的方法如下:
$table_plan         = & JTable::getInstance('Content', 'JTable');
$table_plan_return  = $table_plan->load(array('id'=>$articleId));
echo "<pre>";print_r($table_plan->introtext);echo "</pre>";

1
这个解决方案对我很有效,即使是通过访问模板PHP中的文章上下文。 - Samsky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接