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

发布于 2024-12-02 01:20:09 字数 38 浏览 0 评论 0原文

我想通过从 joomla 模板传递文章 ID 来获取文章文本。

I want to get article text by passing article ID from the joomla template.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冰火雁神 2024-12-09 01:20:09

简单,为您提供使用 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 ";

编辑:从任何地方获取articleId:

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

Simple, providing you sending an article id with post/get and using variable "id" as its number:

$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 ";

EDIT: to get articleId from anywhere:

$articleId = (JRequest::getVar('option')==='com_content' && JRequest::getVar('view')==='article')? JRequest::getInt('id') : 0;
沐歌 2024-12-09 01:20:09

尝试这个技术:

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

try this technique:

$article = JControllerLegacy::getInstance('Content')->getModel('Article')->getItem($articleId);
echo $article->introtext;
信仰 2024-12-09 01:20:09

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

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

文章 ID 是从组件/插件参数中获取的,例如:

  1. 从自己的组件内部:

    $app = JFactory::getApplication();
    $params = $app->getParams();
    $param = $params->get('terms_article_id');
    
  2. 从其他组件:

    $params = JComponentHelper::getParams('com_mycom');
    $id = $params->get('terms_article_id');
    
  3. 从模板的 php 文件获取模块参数:

    $module = JModuleHelper::getModule('mod_mymodule');
    $params = new JRegistry($module->params); // 或 $mymoduleParams 如果很少使用
    $id = (int) $headLineParams['count'];
    

Get article text by article ID in Joomla 2.5 (3 will work also according docs) plugins:

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

And article id is got from component/plugin parameters for example:

  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'];
    
dawn曙光 2024-12-09 01:20:09

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>";

Joomla has the default script for getting content from sql table.

Here article (#__content)

To Get Article Id:

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

To get Article content:

$table_plan         = & JTable::getInstance('Content', 'JTable');
$table_plan_return  = $table_plan->load(array('id'=>$articleId));
echo "<pre>";print_r($table_plan->introtext);echo "</pre>";
琉璃繁缕 2024-12-09 01:20:09

在 Joomla 4/5 中:

use Joomla\CMS\Factory;

$app = Factory::getApplication();
$factory = $app->bootComponent('com_content')->getMVCFactory();
$model = $factory->createModel('Article');
$article = $model->getItem($id);

echo $article->introtext; // or fulltext

In Joomla 4/5:

use Joomla\CMS\Factory;

$app = Factory::getApplication();
$factory = $app->bootComponent('com_content')->getMVCFactory();
$model = $factory->createModel('Article');
$article = $model->getItem($id);

echo $article->introtext; // or fulltext
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文