将文章标题从joomla获取到插件中


getting article title from joomla into a plugin

所以我有了这个工作的PHP代码来获得Joomla当前显示的文章:

<?php
    $option = JRequest::getCmd('option');
    $view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
    $ids = explode(':',JRequest::getString('id'));
    $article_id = $ids[0];
    $article =& JTable::getInstance("content");
    $article->load($article_id);
    echo $article->get("title");
}
else {
    echo "Error - not an article";
}
?>

我已经成功地在ChronoForms中使用了它来获取我的文章名称,以便通过电子邮件发送给客户,这恰好是一个招标网站,每个文章名称的末尾都有一个ID代码,例如12345。

有没有什么快速而肮脏的方法可以在joomla中的某个地方插入这些代码,这样我就可以在文章中动态修改我的插件,比如:

{gallery}12345{/gallery} 

我试着直接将它添加到模板中,但无论我将它插入到哪里(顶部、底部、中间),都会出现各种问题我还在文章joomla中尝试了一个PHP插件,但该代码停止了页面的显示。

根据评论,这就是我所理解的;如果我弄错了,请纠正我。

你有一些文章包含代码

{gallery}here!{/gallery} 

插件应该用替换

 {gallery}The title of the article{/gallery}

如果是这样的话,这将需要在com_content,view=article上发生,其中Joomla将使插件的onContentBeforeDisplay()的article对象可用。

function onContentBeforeDisplay($context, &$article, &$params, $page=0){
    $view     = JRequest::getCmd('view'); 
    if ($view == 'article') {
        $content = $article->text;  
        $title = $article->title;
        $content = preg_replace('/{gallery}.*{'/gallery}/',"{gallery}$title{/gallery}",$content);
        $article->text = $content;
    }
}

但是,如果您需要另一篇文章的标题,或者您需要com_content view=article之外的标题,那么您必须实例化com_conten并从那里获取它。