如何使ajax短代码在循环外工作

时间:2016-07-22 05:52:34

标签: ajax wordpress shortcode

我在Wordpress帖子中使用了包含属性和内容的短代码。这些短代码是必要的,所以我必须使用它们。我是ajaxing索引和单页。我在收到单个帖子时遇到问题,因为当我输出帖子内容返回到客户端时,短代码无法正常工作。

有关强迫方式的任何建议吗?

function ajaxsingle() {
    global $wpdb;
    $postID = $_POST[ 'postID' ];
    $post = get_post( $postID );
    $postType = get_post_type($postID);
    $slug = $post->post_name; 
    $title = get_the_title($postID);
    $thumbnailUrl = wp_get_attachment_url(get_post_thumbnail_id($postID));
    $excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $postID));
    $sidebar = the_widget( 'post-inner-sidebar' );
    $post_content = $post->post_content;
    $imgBackground = "../wp-content/themes/webdev/img/";
    $imgBackground .= get_post_field( 'post_name', get_post($postID) );
    $imgBackground .= "_preview.jpg";

    if ($postType == "articles" || $postType == "tutorials") {

        $html = '<div id="blogWrap" style="background: linear-gradient(to right, rgba(255,255,255,0) 29%,rgba(255,255,255,0.9) 29%,rgba(255,255,255,0.9) 100%), url('.$imgBackground.'); opacity: 0;"><div id="articleHeader"><div id="headerLogo" style="background: url('.$thumbnailUrl.'), linear-gradient(to right, rgba(248,194,121,1) 0%,rgba(248,194,121,1) 100%)"></div><div id="excerptIntro"><div id="excerptInner"><h1>'.$title.'</h1><div class="leftQuotemark tutorials-leftQuotemark"></div><p>'.$excerpt.'</p><div class="rightQuotemark tutorials-rightQuotemark"></div></div></div></div><div id="blogInnerWrap"><div id="adContentWrap">'.$sidebar.'</div><div id="tutorialContent">'.$post_content.'</div></div></div></div>';

    } else if ($postType == "case-studies"){

        $html = '<div id="blogWrap" style="background: linear-gradient(to right, rgba(255,255,255,0) 29%,rgba(255,255,255,0.9) 29%,rgba(255,255,255,0.9) 100%), url('.$imgBackground.'); opacity: 0;"><div id="blogInnerWrap">'.$post_content.'</div></div></div>';

    } else if ($postType == "software") {

        $html = '<div id="blogWrap" style="background: linear-gradient(to right, rgba(255,255,255,0) 29%,rgba(255,255,255,0.9) 29%,rgba(255,255,255,0.9) 100%), url('.$imgBackground.'); opacity: 0;"><div id="softwareHeader"><div id="headerLogo" style="background-image: url('.$thumbnailUrl.'); ?>")></div><div id="excerptIntro"><h1>'.$title.'</h1><div class="leftQuotemark software-leftQuotemark"></div><p>'.$excerpt.'</p><div class="rightQuotemark software-rightQuotemark"></div></div></div><div id="blogInnerWrap">'.$post_content.'</div></div></div>';

    }

   echo $html;
   wp_die();
}

2 个答案:

答案 0 :(得分:1)

在您的代码中尝试使用正则表达式搜索短代码并为每个短代码调用do_shortcode函数以获取短代码处理的结果并将其插入到$ html变量中。

答案 1 :(得分:0)

使用默认值初始化$html,或确保$postType与某些预定义值匹配:"articles""tutorials""case-studies"或{ {1}}。