如何将自定义字段数据拉入Wordpress短代码

时间:2014-03-21 02:30:49

标签: php wordpress custom-fields shortcode

我正在创建一系列短代码,这些短代码链接到名为heroes的自定义帖子类型上的网页。我想知道如何将我的短代码中的信息链接到自定义帖子类型的帖子,这样我就可以将自定义字段数据输入到短代码中使用。

这是我正在使用的短代码

add_shortcode('illidan', 'illidan');

function illidan($args) {
    $default = array('icon' => 'true');
    $args = wp_parse_args($args, $default);

    $herotip = '';
    if ($args['icon']) {
        $herotip.= '<img src="http://stormable.com/img/heroes/illidan/illidan-ab1.png">';
    }

    $herotip.= '<a href="http://stormable.com/heroes/illidan/">Illidan</a>
    This is where I would like to pull in custom field data from the post illidan
    For example <?php echo get_post_meta($post->ID, "health-lvl1", true); ?>
    ';

    return $herotip;
}

因此,在链接下,我希望能够从自定义帖子类型Illidan下的帖子heroes中提取自定义字段中的数据,但我不确定如何将其链接到该页面。

1 个答案:

答案 0 :(得分:0)

这将从post meta

返回自定义字段值
 // returns the value of health-gain
 $health_gain = get_post_meta( 11, 'health-gain', true); 


 $herotip.= '<a href="http://stormable.com/heroes/illidan/">Illidan</a>
This is where I would like to pull in custom field data from the post illidan
For example '. $health_gain;

查看此链接get_post_meta(),将其实施到您想要的短代码功能中。

我建议你看看这篇文章http://wp.smashingmagazine.com/2012/05/01/wordpress-shortcodes-complete-guide/,告诉你创建短代码的更强大和正确的方法。