简体   繁体   English

摘录中的PHP wordpress shortcode

[英]php wordpress shortcode in excerpt

I am looking for away to use shortcodes in post excerpts that will be called via: 我正在寻找在帖子摘录中使用短代码的方式,该摘录将通过以下方式调用:

 $post_object->post_excerpt;

I have the function set up to pull the excerpts but I am struggling to pull the shortcode. 我已经设置了提取摘录的功能,但是我在努力提取简码。 at the moment it just returns a string EG: "[short]foo[/short]". 目前,它仅返回字符串EG:“ [short] foo [/ short]”。 Does anyone know what I need to add to functions.php to allow shortcode usage in this way? 有谁知道我需要添加到functions.php以允许以这种方式使用简码吗? WP 3.6 WP 3.6

EDIT: Also the call to the excerpt is nested inside an already existing shortcode function, which works fine. 编辑:摘录的调用也嵌套在一个已经存在的shortcode函数中,可以正常工作。

Edit 2: 编辑2:

To clarify, I am using 2 function. 为了澄清,我正在使用2函数。 One to create a format for the page "box": 一个为页面“框”创建格式的方法:

//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");

And one to create an icon "char" (this is the function I want to use on the excerpt in the short code above): 然后创建一个图标“ 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"); 

I may be way off base here, is it even possible to use shortcodes in this fashion? 我在这里可能不合时宜,甚至可能以这种方式使用简码吗? and if so how do I stop the excerpt from ignoring the shortcode format? 如果是的话,如何停止摘录中忽略短代码格式?

For anyone who is looking. 对于正在寻找的任何人。

I made a school boy error here. 我在这里犯了一个男生错误。 I had the filter: 我有过滤器:

add_filter( 'post_excerpt', 'do_shortcode');

But forgot to use: 但是忘了使用:

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

Now works fine :-) 现在工作正常:-)

In order to resolve shortcodes, you will need to run your content (the excerpt in your case) through the do_shortcode() API function. 为了解析简码,您将需要通过do_shortcode() API函数来运行内容(本例中的摘录do_shortcode()

So I'd imagine it as something like $xrpt=do_shortcode($xrpt); 所以我想像它像$xrpt=do_shortcode($xrpt);

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

Add these 4 lines to your functions.php file for complete and comprehensive results: 将以下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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM