如何在短代码中使用短代码(WordPress)

时间:2014-01-30 06:03:38

标签: php wordpress

我有自己的功能来创建短代码。函数将获取类别,并显示帖子内容。但是,如果我在帖子中使用了一些短代码。功能正常,但短信代码将显示为短代码[gallery]

/*------------------------------------------*/
/**[cat] shortcode function - by Yesh**/
/*------------------------------------------*/
function categoryShortcode($atts) {
  //Extract Shotcode from the pages and posts
  extract(shortcode_atts(array('slug' => 'default'), $atts));
  global $post;
  $args = array( 'numberposts' => 5, 'category_name' => $atts['slug'], 'orderby'  => 'post_date', 'order' => 'DESC');
  $posts = get_posts( $args );
  $html="";
  foreach ($posts as $post) {
    $category = get_the_category(); //Get the category from the post
    $cat_img = z_taxonomy_image_url(get_category_by_slug($category[1]->name)->term_id);//Get image url from the child category 
    $html.='<div id="parent-cat-image-'.$category[1]->name.'" style="margin-top:-42px;background-position:right center;height:580px;background-image:url('.$cat_img.');">';
    $html.='<div id="cat-title">';
    $html.='<p id="parent-name">'.get_category_by_slug($category[0]->name)->name.'</p>';
    $html.='<h1 id="cat-name">'.$category[1]->name.'</h1>';
    $html.='<p id="read-more">Read More</p>';
    $html.='</div>';
    $html.='</div>';
    $html.='<div id="pack-post-content-'.$category[1]->name.'" style="min-height:400px;display:none;">';
    $html.='<div id="contents" class="post-cont">';
    $html.= $post->post_content;
    $html.='</div>';
    $html.='</div>';
  }
  return $html;
}
add_shortcode('cat', 'categoryShortcode');

我需要在这个foreach中提取帖子短代码。有什么建议?

2 个答案:

答案 0 :(得分:0)

do_shortcode

您调用该函数并传递要执行的短代码。

示例:

echo do_shortcode('[foo]'); or $var = do_shortcode('[foo]');

以下是do_shortcode documentation的链接。

答案 1 :(得分:0)

替换为$html.= $post->post_content;

使用此$html.= do_shortcode($post->post_content);

它将提取您的帖子,页面内容短代码。

相关问题