摘录中的php wordpress短代码

时间:2013-10-14 14:28:54

标签: php wordpress

我正在寻找将在后摘录中使用短代码的方法:

 $post_object->post_excerpt;

我已设置功能以提取摘录但我正在努力拉动短代码。此刻它只返回一个字符串EG:“[short] foo [/ short]”。 有谁知道我需要添加到functions.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"); 

我可能会离开这里,是否有可能以这种方式使用短代码?如果是这样,我如何阻止摘录忽略短代码格式?

4 个答案:

答案 0 :(得分:1)

对于正在寻找的人。

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

add_filter( 'post_excerpt', 'do_shortcode');

但忘了使用:

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

现在工作正常: - )

答案 1 :(得分:0)

要解决短代码,您需要通过do_shortcode() API函数运行您的内容(在您的案例中摘录)。

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

将这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');