dongqiang8683 2014-05-29 11:30
浏览 56

Wordpress,使用标签slug查找标签名称并将其存储在变量中

By doing this:

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

I get:

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

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

The name of the tag that has this slug is "My tag slug". How can I get the following:

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

By using the slug I grabbed from the function?

  • 写回答

1条回答 默认 最新

  • douduoquan2824 2014-05-29 12:24
    关注

    Yes, you can use the slug, grabbed from the function, because directly you cannot "un-slug" the string with Wordpress build-in functions, actually what you see as a slug, is the post_name, stored inside the database (Wordpress internally sanitize the titles of all saved posts / pages / attachments / etc with sanitize_title() function).

    However, you can use simple PHP string string manipulation and construct your $postsTagName variable as you want, for example:

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

    The PHP FIDDLE is here.

    Notes: The above code simply replace the dashes - from the string and capitalize the first character.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向