首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用str_replace在Drupal7视图中更改字段的显示

使用str_replace在Drupal7视图中更改字段的显示
EN

Stack Overflow用户
提问于 2017-11-17 01:58:25
回答 3查看 345关注 0票数 0

我正在使用谷歌分析模块来检索过去一天在我的网站上最受欢迎的文章,以填充“趋势”块。

问题是PageTitle字段来自元数据,并且包括管道和站点名称。我想把它从字符串的末尾去掉,但是我不知道如何使用rewrite results在视图中做到这一点。

我创建了一个views-view-field--pageTitle.tpl.php文件,并将其编辑为:

代码语言:javascript
运行
复制
<?php 
$output = str_replace(" | My Site", "", $output);
print $output;
?>

这似乎行不通..。

EN

回答 3

Stack Overflow用户

发布于 2017-11-17 03:52:18

在template.php中使用a预处理器肯定行得通。还有其他选项可以更改页面,但这取决于您使用什么模块和什么设置来生成标题,显然是使用了某个地方定义模式,所以不知道我的建议是使用这样的预处理器

代码语言:javascript
运行
复制
 function THEME_NAME_preprocess_page(&$variables) {
   //you could use arg to identify your page page
  //https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/arg/7.x
    // if the page is the one you are looking then override the title
     drupal_set_title('mytitle');
 }
票数 0
EN

Stack Overflow用户

发布于 2017-11-17 18:15:28

我认为您必须在template.php中修改元数据,因此您可以使用:

代码语言:javascript
运行
复制
THEMENAME_html_head_alter(&$head_elements) {
  // dump content of $head_elements and find your value to modify
}

文档:https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_html_head_alter/7.x

您还可以使用元数据模块来处理meta:https://www.drupal.org/project/metatag

希望它能有所帮助;)

票数 0
EN

Stack Overflow用户

发布于 2017-11-17 02:11:19

如果你确定它总是以My Article Title | My Web Site的形式返回,你可以在|处拆分字符串。

代码语言:javascript
运行
复制
$result = "My Article Title | My Web Site";   //the string that was returned
$splitResult = explode(" | ", $result);       //split the string at " | ", creating an array: ["My Article Title", "My Web Site"]
$articleTitle = $splitResult[0];              //get the first item in the array
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47336298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档