【问题标题】:Wordpress: Conditionally show stripped tag or post title. "Array" gets printedWordpress:有条件地显示剥离的标签或帖子标题。 “数组”被打印出来
【发布时间】:2014-01-02 21:22:10
【问题描述】:

我想要实现的目标: 显示帖子标签(不带链接)或标题。 如果标签存在,则打印,如果不存在,则使用标题。

问题:我已按照Codex 中的建议使用get_the_tags() 来获取不带链接的标签,并且可以正常工作,但它也会将单词“Array”打印为前缀。

<?php 
 if( has_tag() )
 { 
 echo $posttags = get_the_tags();
 if ($posttags) {
   foreach($posttags as $tag) {
    echo $tag->name . ' '; 
   }
 } 
 }
 else { echo the_title(); };
?>

我错过了什么?

【问题讨论】:

  • 我会将您的问题归类为Stack Exchange Wordpress。在那里发布您的问题,它会得到更快的响应。
  • @dansdixun 请检查我的回答

标签: arrays wordpress tags conditional title


【解决方案1】:

您正在回显 $posttags 这是一个数组。如果您回显 array,它将回显array作为输出

<?php 
 if( has_tag() )
 { 
 This is printing Array as prefix ----> echo $posttags = get_the_tags();
 if ($posttags) {
   foreach($posttags as $tag) {
    echo $tag->name . ' '; 
   }
 } 
 }
 else { echo the_title(); };
?>

请删除那个 echo ,这样你的新代码就会是

<?php 
 if( has_tag() )
 { 
 $posttags = get_the_tags();
 if ($posttags) {
   foreach($posttags as $tag) {
    echo $tag->name . ' '; 
   }
 } 
 }
 else { echo the_title(); };
?>

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2013-12-19
    • 2013-11-01
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 2015-11-07
    • 2023-03-21
    相关资源
    最近更新 更多