在Wordpress函数中获取标题而不是ID


Get Title instead of ID in Wordpress Function

我在wordpress站点中设置了这个函数:

    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;`

返回$val给出post ID编号,但我希望它是post title

:$val = get_post_meta($post_id, $key, true);这里应该有get_post_title或者get_title

提示吗?

您可以使用get_the_title,它根据帖子ID号返回帖子的标题。

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