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.

Read More

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?

Related posts

1 comment

  1. 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;
    

Comments are closed.