在functions.php中执行do_shortcode

问题描述 投票:0回答:1

我试图在functions.php中运行do_shortcode,但没有成功

我正在使用 Types 插件 http://wp-types.com/ 来创建自定义帖子类型和自定义字段。

我正在尝试做的是在管理中添加一个自定义列,用于查看显示自定义字段缩略图的所有自定义帖子。

这是我到目前为止得到的,但似乎短代码在functions.php中不起作用

// add a column for custom post type (products)
add_filter('manage_product_posts_columns', 'add_thumbnail_column');
add_action('manage_product_posts_custom_column', 'add_thumbnail_content', 10, 2);

function add_thumbnail_column($defaults)
{
    $newSlice = array('thumbnail' => 'Image preview');
    $counter = 2;
    $array_head = array_slice($defaults,0,$counter);
    $array_tail = array_slice($defaults,$counter);
    $defaults = array_merge($array_head, $newSlice);
    $defaults = array_merge($defaults, $array_tail);
    return $defaults;  
}

function add_thumbnail_content($column_name, $post_ID)
{
    // this one works when putting into post content
    echo do_shortcode('[types field="square-picture" id="' . $post_ID . '" size="thumbnail"]' . '[/types]');
}

有人可以帮忙吗?

php wordpress
1个回答
0
投票

在 WordPress 的功能注释中,它说

“如果没有定义短代码标签,那么内容将为 未经任何过滤就返回。如果插件是这样,这可能会导致问题 已禁用,因为其短代码仍会显示在帖子或内容中。”

仅当您位于前端时,类型可能有条件地声明其短代码。可能发生的情况是,在管理中,未定义短代码,您只是得到错误的返回。在前端,定义了短代码,您就可以得到您想要的结果。

© www.soinside.com 2019 - 2024. All rights reserved.