簡體   English   中英

joomla:在文章中使用API

[英]joomla: use API inside an article

我正在使用Sourcerer插件在我的文章中使用PHP代碼。 我想在我的文章中使用Joomla API /框架來動態設置HTML元標記和其他內容。 我找到了應該允許我這樣做的setHeadData方法,但我根本不知道如何調用它。

[問]有人可以給我一個例子或指向我的教程,這將有助於我開始使用該joomla API /框架嗎?

回答

基於所有指向相同方向的眾多反饋,使用內容插件修改頭部數據比通過文章這樣做更好。 如果像我一樣你想在一篇文章中做到這一點,這就是我所做的:

(1)我使用ezpresso提供的片段在我的文章中設置頭部數據。
(2)我修改了libraries / joomla / document / html / renderer / head.php文件來改變頭數據的設置方式。

例如,您可以在步驟(1)設置標題元標記,然后在步驟(2)中替換以下行:

$strHtml .= $tab.'<title>'.htmlspecialchars($document->getTitle()).'</title>'.$lnEnd;

這一個:

$strHtml .= $tab.'<title>'.htmlspecialchars($document['metaTags']['standard']['title']).'</title>'.$lnEnd;

您可能還需要查看libraries / joomla / document / html / renderer / head.php以在頭部進行更多清理,例如刪除Joomla插入的generator元標記。

以下是您所指的方法的源代碼:

/**
 * Set the html document head data
 *
 * @access  public
 * @param   array   $data   The document head data in array form
 */
function setHeadData($data)
{
    $this->title        = (isset($data['title'])) ? $data['title'] : $this->title;
    $this->description  = (isset($data['description'])) ? $data['description'] : $this->description;
    $this->link         = (isset($data['link'])) ? $data['link'] : $this->link;
    $this->_metaTags    = (isset($data['metaTags'])) ? $data['metaTags'] : $this->_metaTags;
    $this->_links       = (isset($data['links'])) ? $data['links'] : $this->_links;
    $this->_styleSheets = (isset($data['styleSheets'])) ? $data['styleSheets'] : $this->_styleSheets;
    $this->_style       = (isset($data['style'])) ? $data['style'] : $this->_style;
    $this->_scripts     = (isset($data['scripts'])) ? $data['scripts'] : $this->_scripts;
    $this->_script      = (isset($data['script'])) ? $data['script'] : $this->_script;
    $this->_custom      = (isset($data['custom'])) ? $data['custom'] : $this->_custom;
}

它在JDocumentHtml類中實現,該類位於libraries/joomla/document/html/html.php目錄中。

以下是一些如何使用它的示例的鏈接:

我猜你可以像這樣調用setHeadData方法:

$doc =& JFactory::getDocument();
$options = $doc->getHeadData();
$options['metaTags'] = array("tag1", "tag2", "tag3"); // you may change the meta tags here
$doc->setHeadData($options); 

將PHP放在文章中並不是一個很好的方法來完成你想要做的事情。 Joomla框架具有一個操作順序,用於確定何時運行各種功能和插件。 由於操作的順序,在呈現文章后會發生許多功能,可能會否定您在文章中所做的任何更改。 你最好使用擴展來處理標題和元標記,而不是試圖在文章中做到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM