WP客户评论在另一个页面上呼叫短代码

时间:2018-06-02 作者:urfusion

我已经创建了一些主题页,我想打电话给WP Customer Reviews 短代码

 echo do_shortcode(\'[WPCR_SHOW POSTID="\' . $post->ID . \'" NUM="1" HIDEREVIEWS="0" HIDERESPONSE="1" HIDECUSTOM="1" SHOWFORM="0"]\');
但它回来了

[WPCR\\u SHOW POSTID=“3498”NUM=“1”HIDEREVIEWS=“0”HIDERESPONSE=“1”HIDECUSTOM=“1”SHOWFORM=“0”]

这是我的完整源代码。文件位于wp-content\\themes\\couponxl - child\\category_details.php

<?php
/* Template Name: Category Details */
get_header();
$search_sidebar_location = couponxl_get_option(\'search_sidebar_location\');

if ($_GET[\'paged\']) {
    $cur_page = $_GET[\'paged\'];
    $offset = $_GET[\'paged\'] * 4;
} else {
    $cur_page = 1;
    $offset = 1;
}

get_template_part(\'includes/title\');
?>
<section>
    <div class="container">

        <?php
        $content = get_the_content();
        if (!empty($content)):
            ?>
            <div class="white-block">
                <div class="white-block-content">
                    <div class="page-content clearfix">
                        <?php echo apply_filters(\'the_content\', $content) ?>
                    </div>
                </div>
            </div>
            <?php
        endif;
        ?>

        <div class="row">

            <?php if ($search_sidebar_location == \'left\'): ?>
                <?php get_sidebar(\'coupon\') ?>
            <?php endif; ?>

            <div class="col-md-<?php echo is_active_sidebar(\'sidebar-coupon\') ? \'9\' : \'12\' ?>">
                <?php echo do_shortcode("[wcps id=\'3414\']"); ?>
                <div class="row masonry masonry-item">
                    <?php
                    $args = array(\'posts_per_page\' => 5, \'taxonomy\' => \'product_cat\', \'terms\' => $_GET[\'cat\'],
                        \'paged\' => $cur_page, \'post_type\' => \'product\', \'tax_query\' => array(
                            array(
                                \'taxonomy\' => \'product_cat\',
                                \'field\' => \'slug\',
                                \'terms\' => $_GET[\'cat\'], // Where term_id of Term 1 is "1".
                            )
                    ));
                    $postslist = new WP_Query($args);
                    $coupons = $postslist;
                    $page_links_total = $coupons->max_num_pages;
                    $pagination_args = array(
                        \'prev_next\' => true,
                        \'total\' => $page_links_total,
                        \'current\' => $cur_page,
                        \'prev_next\' => true,
                        \'type\' => \'array\',
                    );

                    $pagination_args[\'format\'] = \'?paged=%#%\';

                    $page_links = paginate_links($pagination_args);
                    $pagination = couponxl_format_pagination($page_links);

                    if (!empty($postslist->posts)) {
                        foreach ($postslist->posts as $post) {
                            ?>
                            <div class="white-block offer-box coupon-box <?php echo esc_attr($offer_view) ?> <?php echo $col == \'12\' ? \'clearfix\' : \'\' ?>">
                                <div class="col-md-6 product-list">
                                    <div class="white-block-media <?php echo $col == \'12\' ? \'col-sm-4\' : \'\' ?>">
                                        <div class="embed-responsive embed-responsive-16by9 ">
                                            <?php
                                            $store_id = get_post_meta(get_the_ID(), \'location\', true);
                                            couponxl_store_logo($store_id);
                                            ?>
                                        </div>
                                        <?php if (couponxl_is_plugin_active(\'couponxl-cpt/couponxl-cpt.php\')) : ?>
                                            <?php couponxl_cpt_share(); ?>
                                        <?php endif; ?> 
                                    </div>
                                    <?php
                                    echo \'<label class="flag-new">\' . $label = get_post_meta($post->ID, \'label\', true) . \'</label>\';
                                    echo \'<label class="flag-discount" style="color: #f32398;">-\' . $discount = get_post_meta($post->ID, \'discount\', true) . \'%</label>\';
                                    echo \'<label class="wishlist-button">\' . do_shortcode(\'[woosw id = \' . $post->ID . \']\') . \'</label>\';
                                    ?>
                                    <div class="white-block-content <?php echo $col == \'12\' ? \'col-sm-8\' : \'\' ?>">
                                        <?php
                                        echo do_shortcode(\'[WPCR_SHOW POSTID="\'.$post->ID.\'" NUM="1" HIDEREVIEWS="0" HIDERESPONSE="1" HIDECUSTOM="1" SHOWFORM="0"]\');
                                        if (get_post_meta($post->ID, \'expires_in\', true)) {
                                            $earlier = new DateTime(date("Y-m-d"));
                                            $later = new DateTime(date(\'Y-m-d\', strtotime(get_post_meta($post->ID, \'expires_in\', true))));
                                            $diff = $later->diff($earlier)->format("%a");
                                            if (!empty($diff)) {
                                                echo \'<span class="expire-date">Expires in: <span class="expire-text">\' . $diff . \' days</span></span>\';
                                            } else {
                                                echo \'<span class="expire-date"> </span>\';
                                            }
                                        }
                                        ?>
                                        <h3>
                                            <a href="<?php the_permalink() ?>">
                                                <?php the_title(); ?>
                                            </a>
                                        </h3>

                                        <ul class="list-unstyled list-inline bottom-meta">

                                            <li>
                                                <i class="fa fa-dot-circle-o icon-margin"></i>
                                                <?php echo couponxl_taxonomy(\'product_cat\', 1) ?>
                                            </li>
                                            <li>
                                                <i class="fa fa-map-marker icon-margin"></i> 
                                                <?php echo couponxl_taxonomy(\'location\', 1) ?>
                                            </li>               

                                        </ul>
                                    </div>
                                    <div class="white-block-footer  <?php echo $col == \'12\' ? \'col-sm-12\' : \'\' ?>">
                                        <div class="white-block-content">
                                            <?php
                                            $price = get_post_meta(get_the_ID(), \'_regular_price\', true);
                                            $sale = get_post_meta(get_the_ID(), \'_sale_price\', true);
                                            ?>

                                            <?php if ($sale) : ?>
                                                <span class="price-style">
                                                    <span class="woocommerce-Price-amount amount">
                                                        <i class="">RM</i><?php echo $sale; ?>
                                                        <i class="fa">RM</i>
                                                        <del><?php echo $price; ?></del> 
                                                    </span>    
                                                </span>    
                                            <?php elseif ($price) : ?>
                                                <span class="price-style">
                                                    <span class="woocommerce-Price-amount amount">
                                                        <i class="fa">RM</i> <?php
                                                        echo $price;
                                                        ?>    
                                                    </span>    
                                                </span>    
                                            <?php endif; ?>
                                            <span class="span-class1">
                                                <a class="btn single_add_to_cart_button alt" href="<?= site_url() . \'/cart/?add-to-cart=\' . $post->ID ?>">Add to cart</a>
                                            </span>
                                        </div>
                                    </div>
                                </div>

                                <?php
                            }
                        }else {
                            ?>
                            <div class="white-block-content">
                                <p class="nothing-found">Currently there is no products for this category.</p>
                            </div>
                        <?php }
                        ?>
                    </div>
                </div>
            </div>
            <?php //endif;              ?>
        </div>
        <?php if (!empty($pagination)): ?>
            <div class="col-sm-4 masonry-item paginate">
                <ul class="pagination">
                    <?php echo $pagination; ?>
                </ul>
            </div>
        <?php endif; ?>
    </div>

</section>

<?php get_footer(); ?>
我已经为该产品启用了WP客户评论。

enter image description here

有人能帮我吗?

2 个回复
最合适的回答,由SO网友:urfusion 整理而成

我想出了在你想要的任何页面上使用该插件的方法。

在主题/函数中创建函数。php

function get_review_rating($id = null) {
    $id = $id[\'id\'];
require_once( ABSPATH . \'wp-content/plugins/wp-customer-reviews/wp-customer-reviews-3.php\' );
    $WPCustomerReviews31 = new WPCustomerReviews3();
    $WPCustomerReviews31->start();
        $WPCustomerReviews31->init();
    echo do_shortcode(\'[WPCR_SHOW POSTID=\' . $id . \' NUM="1" HIDEREVIEWS="0" HIDERESPONSE="1" HIDECUSTOM="1" SHOWFORM="0"]\');
//    die;
}

add_shortcode(\'get_review_and_rating\', \'get_review_rating\');
现在不再调用插件默认快捷码调用get_review_and_rating 你想在任何地方使用快捷代码。

echo do_shortcode(\'[get_review_and_rating id=\'.$post->ID.\']\');

SO网友:bravokeyl

你发布的代码没有问题,它应该输出指定ID的评论。我使用了相同的短代码,它可以工作。

它返回短代码文本的原因可能是haven\'t activated WP Customer Reviews plugin.

检查插件是否处于活动状态

结束

相关推荐

Shortcode with foreach

我不能对foreach使用shortcode。它给了我语法错误。我错在哪里?function create_galeri_shortcode($atts) { $atts = shortcode_atts( array( ), $atts, \'galeri\' ); if ( has_post_format( \'gallery\' )) { $images = get_post_