短代码中的Wordpress功能仅在页面上工作一次

时间:2018-04-19 20:34:03

标签: wordpress woocommerce shortcode

我目前在“我的帐户”页面(WooCommerce + Wordpress)上使用短代码来检查用户在显示图像之前是否拥有该课程以及访问内容的链接。如果我只使用1个短代码,它的工作效果非常好。但是,只要我添加第二个具有不同产品创意的短代码,它就会输出一个白页。

//Displays course module image
//[checkpurchase course="" url="" img=""]
function checkpurchase_shortcode( $atts, $content = null ) {

    //set default attributes and values
    $values = shortcode_atts( array(
        'course'    => '0',
        'url'   => '#',
        'img'   => '#',
    ), $atts );     
    $content = "";

    $courseid = esc_attr($values['course']);

    //check if they purchased the course through WooCommerce
    function check_coursepurchase($courseid) {
        global $woocommerce;
        $user_id = get_current_user_id();
        $current_user= wp_get_current_user();
        $customer_email = $current_user->email;

        //if they own the product return true
        if ( wc_customer_bought_product( $customer_email, $user_id, $courseid)) {
                return true;
        } 
        return false;
    }

    //if they own the product them display access
    if( check_coursepurchase($courseid) ){
        //make icon on the my account page for the course
        return '<a href="'. esc_attr($values['url']) .'"  target="_blank"><img src="'. esc_attr($values['img']) .'" border="0" /></a>'; 
    }
}
add_shortcode( 'checkpurchase', 'checkpurchase_shortcode' );

1 个答案:

答案 0 :(得分:0)

现在我停止了嵌套功能。谢谢!

//check if they purchased the course through WooCommerce
function check_coursepurchase($courseid) {
    global $woocommerce;
    $user_id = get_current_user_id();
    $current_user= wp_get_current_user();
    $customer_email = $current_user->email;

    //if they own the product return true
    if ( wc_customer_bought_product( $customer_email, $user_id, $courseid)) {
        return true;
    } 
    return false;
}

//Displays course module image
//[checkpurchase course="" url="" img=""]
function checkpurchase_shortcode( $atts, $content = null ) {

    //set default attributes and values
    $values = shortcode_atts( array(
        'course'    => '0',
        'url'   => '#',
        'img'   => '#',
    ), $atts );     
    $content = "";

    $courseid = esc_attr($values['course']);

    //if they own the product them display access
    if( check_coursepurchase($courseid) ){
        //make icon on the my account page for the course
        $content = '<a href="'. esc_attr($values['url']) .'"  target="_blank"><img src="'. esc_attr($values['img']) .'" border="0" /></a>';
        return do_shortcode($content);
    }
}
add_shortcode( 'checkpurchase', 'checkpurchase_shortcode' );