user2849533
user2849533

Reputation: 39

Get Title instead of ID in Wordpress Function

I have this function in a wordpress site:

    function cf7_get_custom_field($atts){
        extract(shortcode_atts(array(
            'key' => '',
            'post_id' => -1,
            'obfuscate' => 'off'
        ), $atts));

        if($post_id < 0){
            global $post;
            if(isset($post)) $post_id = $post->ID;
        }

        if($post_id < 0 || empty($key)) return '';

        $val = get_post_meta($post_id, $key, true);

        if($obfuscate == 'on'){
    $val = cf7dtx_obfuscate($val);
        }

        return $val;`

The return $val gives the post ID number, but instead I would like it to be the post title.

The line: $val = get_post_meta($post_id, $key, true); Should have get_post_title or get_title in there somewhere, I suppose.

Any tips?

Upvotes: 3

Views: 307

Answers (1)

m4n0
m4n0

Reputation: 32255

You can use get_the_title which returns the title of the post based on the post ID number.

$val = get_post_meta($post_id, $key, true);
$post_title = get_the_title( $val );

return $post_title;

Upvotes: 1

Related Questions