【问题标题】:Wordpress, use tag slug to find tag name and store it in a variableWordpress,使用标签 slug 查找标签名称并将其存储在变量中
【发布时间】:2014-07-18 21:32:18
【问题描述】:

通过这样做:

if (has_tag( "my-tag-slug", $post )) { $postsTag = "my-tag-slug"; $tagImageFormat = ".jpg"; }

我明白了:

- a variable named $postsTag containing the string "my-tag-slug"

- a variable named $tagImageFormat containing the string ".jpg"

具有此 slug 的标签的名称是“我的标签 slug”。我怎样才能得到以下信息:

- a variable named $postsTagName containing the string "My tag slug"

通过使用我从函数中获取的 slug?

【问题讨论】:

    标签: php wordpress tags slug


    【解决方案1】:

    是的,您可以使用从函数中获取的 slug,因为 您不能直接使用 Wordpress 内置函数“取消 slug”字符串,实际上您所看到的 slug 是post_name,存储在数据库中(Wordpress 在内部使用 sanitize_title() function 清理所有已保存帖子/页面/附件等的标题)。

    但是,您可以使用简单的 PHP 字符串操作并根据需要构造 $postsTagName 变量,例如:

    <?php    
    $postsTagName = str_replace('-', ' ', ucfirst(strtolower('my-tag-slug')));
    //Now your variable will contain My tag slug
    ?>
    

    PHP FIDDLE 在这里。

    注意:上面的代码只是replace字符串中的破折号-capitalize第一个字符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      • 1970-01-01
      • 2016-03-27
      相关资源
      最近更新 更多