- 首页
- PHP
- WordPress – 如何在 PHP 文件中使用短代码
WordPress – 如何在 PHP 文件中使用短代码
[英] WordPress – How to use shortcode in PHP files
本文介绍了WordPress – 如何在 PHP 文件中使用短代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 WooCommerce 插件设置了 WordPress,产品被循环使用......直到那里都很好.
我想增加访问者能够对产品进行投票的可能性.所以我正在寻找一个插件并找到了一个.
但这才是真正的问题!名为Like-Photo"的插件为我提供了 WordPress 短代码功能.如果我在 WordPress 编辑器中插入短代码(图像前后),一切正常.
但我需要将该代码放入 PHP 文件(循环出产品的文件)本身.
所以我尝试对短代码使用 PHP echo 函数,如下所示.它根本不起作用.当我在浏览器中打开检查器工具时,我只能看到以文本形式呈现的第二个短代码部分,它应该创建一个 div(当我在 WordPress 帖子编辑器中粘贴短代码时,它会做什么).
我该如何解决这个问题?
<?php echo do_shortcode('[add_voting]');?><!-- 短代码开头--><?php do_action('woocommerce_before_shop_loop_item');?><!-- 项目的循环--><div class="image-box"><div class="voteup_layer"><div class="voteup_layer_triangle"><div class="voteup_layer_triangle-inner"></div>
<p>点击 2 个 UPVOTE</p>
<div class="sb_title"><?php the_title();?></div><div class="箭头向上"></div><div id="num-id" class="count-num">20.453</div><a href="<?php the_permalink(); ?>"><?php/*** woocommerce_before_shop_loop_item_title 钩子** @hooked woocommerce_show_product_loop_sale_flash - 10* @hooked woocommerce_template_loop_product_thumbnail - 10*/do_action('woocommerce_before_shop_loop_item_title');如果 ( 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>
<?php echo do_shortcode('[/add_voting]');?><!-- 短代码结束-->
我得到了这个 HTML 输出:
<div class="image-box"><div class="voteup_layer"><div class="voteup_layer_triangle"><div class="voteup_layer_triangle-inner"></div>
<p>点击 2 个 UPVOTE</p>
<div class="sb_title">FOSSIL <br><span class="thin">Moon Explorer</span></div><div class="箭头向上"></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>
[/add_voting]"
并且想要(这就是我在 WordPress 编辑器中添加短代码后 HTML 的呈现方式 – 它会在我放置短代码的图像周围创建一个名为like-photo-wrapper"的 div 并添加投票功能):
<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">投票数:0</span><a href="http://online.com" title="投票给这张图片">投票给这张图片</a>
短代码在我的 PHP 代码中无法正常工作.
解决方案
查看 的文档do_shortcode.
它的要点是调用 do_shortcode
来包装内容的短代码应该是这样的
//如果有打开和关闭短代码.echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );
您可以尝试使用输出缓冲来捕获输出并将其传递到您的短代码中.
ob_start();<?php do_action('woocommerce_before_shop_loop_item');?><!-- 项目的循环--><div class="image-box"><div class="voteup_layer"><div class="voteup_layer_triangle"><div class="voteup_layer_triangle-inner"></div><p>点击 2 个 UPVOTE</p>
<div class="sb_title"><?php the_title();?></div><div class="箭头向上"></div><div id="num-id" class="count-num">20.453</div><a href="<?php the_permalink(); ?>"><?php/*** woocommerce_before_shop_loop_item_title 钩子** @hooked woocommerce_show_product_loop_sale_flash - 10* @hooked woocommerce_template_loop_product_thumbnail - 10*/do_action('woocommerce_before_shop_loop_item_title');如果 ( 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>
$out = ob_get_clean();<?php echo do_shortcode('[add_voting] ' . $out . '[/add_voting]');?><!-- 短代码结束-->
I set up WordPress with the WooCommerce Plugin and the products get looped out...all good till there.
I wanted to add the possibility that visitors are able to upvote a product. So I was looking for a plugin and found one.
But that's the actual problem! The plugin called "Like-Photo" offers me the WordPress shortcode function. If I insert the shortcode in the WordPress editor (before and after the image) all is working fine.
But I need to put that code in the PHP file (the one that loops out the products) itself.
So I tried using the PHP echo function for the shortcode as you can see below. It's not working at all. When I open up the inspector tools in my browser I only see the second shortcode part rendered out in text and it's supposed to create a div (what it does, when I paste in the shortcodes inside the WordPress post editor).
How can I fix this?
<?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-->
I'm getting this HTML output:
<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]"
And want (this is how the HTML gets rendered once I add the shortcode inside the WordPress editor – it creates a div called "like-photo-wrapper" around the image where I placed the shortcode and adds the ability to vote):
<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>
The shortcode isn't working properly in my PHP code.
解决方案
Check out the documentation for do_shortcode.
The gist of it is that the call to do_shortcode
for a shortcode that wraps content should be like this
// In case there is opening and closing shortcode.
echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );
You can try something like this using output buffering to capture the output and pass it into your shortcode.
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-->
这篇关于WordPress – 如何在 PHP 文件中使用短代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文