MaryOne
MaryOne

Reputation: 3

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.

Upvotes: 0

Views: 612

Answers (2)

mike510a
mike510a

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

Stanimir Stoyanov
Stanimir Stoyanov

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

Related Questions