0

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.

MaryOne
  • 3
  • 1

2 Answers2

1

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();
Stanimir Stoyanov
  • 1,876
  • 1
  • 15
  • 20
1

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.

mike510a
  • 2,102
  • 1
  • 11
  • 27