我用WooCommerce插件设置了WordPress,在此之前,产品一直循环运行out...all。
我想添加一个可能性,即访问者可以对产品进行提升。所以我在找一个插件,找到了一个。
但这才是真正的问题所在!这个名为“点赞照片”的插件为我提供了WordPress短码功能。如果我在WordPress编辑器中插入短代码(在图像之前和之后),一切都可以正常工作。
但我需要将这些代码放在PHP文件(循环输出产品的那个文件)中。
因此,我尝试对短码使用PHP echo函数,如下所示。它根本不起作用。当我在浏览器中打开检查器工具时,我只看到第二个以文本呈现的短码部分,它应该创建一个div (当我在WordPress帖子编辑器中粘贴短码时,它的作用是什么)。
我该如何解决这个问题呢?
<?php echo do_shortcode('[add_voting]'); ?> <!-- shortcode beginning-->
<?php do_action( 'woocommerce_before_shop_loop_item' ); ?> <!-- The loop for the items-->
<div class="image-box">
<div class="voteup_layer">
<div class="voteup_layer_triangle">
<div class="voteup_layer_triangle-inner"></div>
</div>
<p>CLICK 2 UPVOTE</p>
</div>
<div class="sb_title"><?php the_title(); ?></div>
<div class="arrow-up"></div>
<div id="num-id" class="count-num">20.453</div>
<a href="<?php the_permalink(); ?>">
<?php
/**
* woocommerce_before_shop_loop_item_title hook
*
* @hooked woocommerce_show_product_loop_sale_flash - 10
* @hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
if ( has_post_thumbnail() ) {
//echo get_the_post_thumbnail( get_the_ID(), array(370,370) );
echo get_the_post_thumbnail( get_the_ID(), 'home-small-box' );
}
?>
</a>
</div>
<?php echo do_shortcode('[/add_voting]'); ?> <!-- shortcode end-->我得到了这个HTML输出:
<div class="home_small_box ">
<div class="image-box">
<div class="voteup_layer">
<div class="voteup_layer_triangle">
<div class="voteup_layer_triangle-inner"></div>
</div>
<p>CLICK 2 UPVOTE</p>
</div>
<div class="sb_title">FOSSIL <br> <span class="thin">Moon Explorer</span></div>
<div class="arrow-up"></div>
<div id="num-id" class="count-num">20.453</div>
<a href="http://online.com/product/fossil-moon-explorer/">
<img width="360" height="360" src="http://online.com/wp-content/uploads/2015/08/shopping-1-360x360.jpg" class="attachment-home-small-box wp-post-image" alt="shopping-1">
</a>
</div>
"[/add_voting]"和want (这就是我在WordPress编辑器中添加快捷代码后,HTML语言的呈现方式-它在我放置快捷代码的图像周围创建了一个名为"like-photo-wrapper“的div,并添加了投票功能):
<div class="like-photo-wrapper">
<a href="http://online.com/wp...2.jpg">
<img src="http://online.com/wp...300.jpg" alt="shopping-2" >
</a>
<div class="votes"><span class="currentVotes">Votes: 0</span>
<a href="http://online.com" title="vote on this image">vote on this image</a>
</div>
</div>在我的PHP代码中,短码不能正常工作。
发布于 2015-08-28 22:57:45
查看do_shortcode的文档。
它的要点是,对包装内容的短码的do_shortcode调用应该如下所示
// In case there is opening and closing shortcode.
echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );您可以尝试这样做,使用输出缓冲来捕获输出并将其传递到您的短代码中。
ob_start();
<?php do_action( 'woocommerce_before_shop_loop_item' ); ?> <!-- The loop for the items-->
<div class="image-box">
<div class="voteup_layer">
<div class="voteup_layer_triangle">
<div class="voteup_layer_triangle-inner"></div>
</div>
<p>CLICK 2 UPVOTE</p>
</div>
<div class="sb_title"><?php the_title(); ?></div>
<div class="arrow-up"></div>
<div id="num-id" class="count-num">20.453</div>
<a href="<?php the_permalink(); ?>">
<?php
/**
* woocommerce_before_shop_loop_item_title hook
*
* @hooked woocommerce_show_product_loop_sale_flash - 10
* @hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
if ( has_post_thumbnail() ) {
//echo get_the_post_thumbnail( get_the_ID(), array(370,370) );
echo get_the_post_thumbnail( get_the_ID(), 'home-small-box' );
}
?>
</a>
</div>
$out = ob_get_clean();
<?php echo do_shortcode('[add_voting] ' . $out . '[/add_voting]'); ?> <!-- shortcode end-->https://stackoverflow.com/questions/32273197
复制相似问题