Reputation: 3
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.
Upvotes: 0
Views: 612
Reputation: 2168
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.
Upvotes: 1
Reputation: 1926
Just add the following code:
global $wp_query;
$postid = $wp_query->post->ID;
$key = 'My song title mp3';
$key = substr($key, 0, -4);
echo get_post_meta( $postid, $key, true );
wp_reset_query();
Upvotes: 1