MaryOne MaryOne - 16 days ago 8
PHP Question

Edit get_post_meta() output in WordPress

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

get_post_meta
.

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();


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

Answer

Replace your echo command with:

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

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

Comments