在 WordPress 中编辑 get_post_meta() 输出 [英] Edit get_post_meta() output in WordPress

查看:24
本文介绍了在 WordPress 中编辑 get_post_meta() 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WordPress 站点中,我想删除 get_post_meta 的渲染输出中的最后 4 个字符(包含空格).

In a WordPress site, I want to remove the last 4 charaters (included space) in the rendered output of get_post_meta.

这是 PHP 代码,我在其中输出名为 key 的帖子的自定义字段:

Here is the PHP code, where I output the custom field named key of a post:

global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'key', true);
wp_reset_query();

示例:如果在特定帖子中,keyMy song title mp3,则输出将是 My song title 因为 mp3已修剪.

Example: If in a specific post, key is My song title mp3, the output will be My song title because mp3 has been trimmed.

推荐答案

将您的 echo 命令替换为:

Replace your echo command with:

$string = get_post_meta($postid, 'key', true);
echo substr($string, 0, -4);

将帖子元保存为 $string 然后使用 substr() 删除最后 4 个字符.

which saves the post meta as $string then uses substr() to remove the last 4 characters.

这篇关于在 WordPress 中编辑 get_post_meta() 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆