摘录中的PHP WordPress简码


php wordpress shortcode in excerpt

我正在寻找在帖子摘录中使用短代码,这些短代码将通过以下方式调用:

 $post_object->post_excerpt;

我设置了提取摘录的功能,但我正在努力提取短代码。 目前它只返回一个字符串 EG:"[short]foo[/short]"。有谁知道我需要向函数添加什么.php才能允许以这种方式使用短代码?WP 3.6

编辑:对摘录的调用也嵌套在已经存在的简码函数中,该函数工作正常。

编辑 2:

澄清一下,我正在使用 2 个函数。一个为页面"框"创建格式:

//create box shortcode
function box( $atts, $content = null) {
    $p_id = $atts['post'];
    $p = get_post($p_id);
    $output = '<div class="box"><a href="'.get_permalink($p_id).'"><div class="box_inner"><h1>'.$p->post_title.'</h1><p>'.$p->post_excerpt.'</p></div></a></div>';
    return $output;
}
add_shortcode("box", "box");

还有一个用于创建一个图标"char"(这是我想要在上面简短代码中的摘录中使用的函数):

//big text for icons shortcode
function icon( $atts, $content = null) {
    return '<p style="text-align: center; font-size: 100px;>'.$content.'</p>';
}
add_shortcode("icon", "icon");

我可能在这里离谱很远,甚至有可能以这种方式使用短代码吗? 如果是这样,如何阻止摘录忽略简码格式?

对于任何正在寻找的人。

我在这里犯了一个男生错误。我有过滤器:

add_filter( 'post_excerpt', 'do_shortcode');

但忘记使用:

apply_filters('post_excerpt', $p->post_excerpt);

现在工作正常:-)

为了解析短代码,您需要通过do_shortcode() API函数运行您的内容(在您的例子中是摘录)。

所以我把它想象成$xrpt=do_shortcode($xrpt);

U 可能还需要使用 add_action() 来注册到您的短代码的钩子。

将以下 4 行添加到您的functions.php文件中,以获得完整而全面的结果:

add_filter('the_excerpt', 'shortcode_unautop');
add_filter('the_excerpt', 'do_shortcode');
add_filter('get_the_excerpt', 'shortcode_unautop');
add_filter('get_the_excerpt', 'do_shortcode');