我正在尝试获取我的ShortCode的第一次和最后一次出现,以打印引导程序面板及其结束标记的开始HTML标记。
我知道如何使用静态变量在页面内容中找出ShortCode的第一个实例。以下代码是我目前的ShortCode
代码的情况add_download
在我的帖子内容中使用此ShortCode的示例就是这样。
[add_download name =' The.Hollars.2016.720p' DOWNLOAD_LINK =' http://localhost/mp4fs/The.Hollars.2016.720p.mp4' DOWNLOAD_TITLE =' The.Hollars.2016.720p'] [add_download name =' The.Dinner.2017.720p.WEB-DL' DOWNLOAD_LINK =' http://localhost/mp4fs/httThe.Dinner.2017.720p.WEB-DL.mp4' DOWNLOAD_TITLE =' The.Dinner.2017.720p.WEB-DL' ]
正如您所看到的,在帖子的内容中发生了这个ShortCode的两个实例。我想在bootstrap面板中显示下载链接。
在下文中,您将看到此ShortCode的代码。
<?php
function add_download_func( $atts ) {
$name = $atts['name'];
$download_title = $atts['download_title'];
$download_link = $atts['download_link'];
static $download_link_count;
$download_link_count++;
// check for first instance of shortCode
if ($download_link_count == 1){?>
<div class="panel panel-default videos">
<div class="panel-heading">Download Movie</div>
<div class="panel-body">
<?php } ?>
<div class="download_title"><?php echo $download_title; ?></div>
<div class="download_link">
<a href="<?php echo $download_link; ?>" target="_blank">Direct Download Link</a>
</div>
<?php
// check to see for the last occurrence of shortcode
if (/* $download_link_count is the last one */){
?>
</div>
</div>
<?php } ?>
<?php
}
add_shortcode( 'add_download', 'add_download_func' );
提前感谢您的任何帮助和建议。