【问题标题】:Get, modify, then print post title in wordpress在 wordpress 中获取,修改,然后打印帖子标题
【发布时间】:2013-12-31 07:06:48
【问题描述】:

wordpress 中是否有一种本地方式可以将帖子的气味/the_title 的第一个单词的第一个字母大写?

如果没有,我如何在 php 中执行它,而它被包裹在 <a></a> 标记中?

这是完整的代码行。

<a href="<?php the_permalink(); ?>"><?php ucfirst(the_title());?></a>

如你所见,我尝试过ucfirst,但它不起作用。我尝试的另一件事是:

<?php
$temp_title  = the_title();
$final_title = ucfirst($temp_title);
?>
<a href="<?php the_permalink(); ?>"><?php echo $final_title;?></a>

【问题讨论】:

    标签: wordpress ucfirst


    【解决方案1】:

    ucfirst() 返回一个字符串。你需要echoucfirst()返回的值。

    此外,WordPress 函数the_title() 直接打印标题,而不是将其作为字符串返回。请改用get_the_title()

    <a href="<?php the_permalink(); ?>"><?php echo ucfirst(get_the_title());?></a>
    

    【讨论】:

    • 是的,我放在这里的括号是错误的(我只会更正)但它不在我的实际代码中。我的代码与您的选择完全一样,但它不起作用。是的,我使用了您的主要解决方案&lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php echo ucfirst(the_title());?&gt;&lt;/a&gt;,但它也不起作用。无论如何,我感谢您的帮助。
    【解决方案2】:

    我认为这是您的问题:http://core.trac.wordpress.org/browser/tags/3.8/src/wp-includes/post-template.php#L51。如您所见,the_title() 正在使用get_the_title(),然后if($echo) 将其呼应出来。我会尝试get_the_title()

    【讨论】:

      【解决方案3】:

      首先,您的 echo $final_title 中有一个结尾括号没有匹配的括号。其次,您应该尝试将 ucfirst 应用于标题的回声。我删除了 $finaltitle,因为它不再有用。我还没有尝试过代码,但它应该可以工作。请注意,ucfirst() 仅在第一个字母在字母表中时才有效。

      <?php
      $temp_title  = the_title();
      ?>
      <a href="<?php the_permalink(); ?>"><?php echo ucfirst($temp_title);?></a> 
      

      【讨论】:

      • 我之前试过这个。这只是将标题作为一个简单的字符串返回,而不是我需要的 url/链接的形式。奇怪。
      • the_title();应该用 get_the_title(); 改变
      猜你喜欢
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      • 2013-10-10
      • 1970-01-01
      相关资源
      最近更新 更多