我使用的大多数字段都来自ACF pro插件,其中一些使用的是ACF的中继器,我在PHP中不太好,但是请您帮我把下面的代码转换成简码吗?
<div class="row">
<?php
if( have_rows('our_golf_tours_list',161) ):
?>
<?php
while( have_rows('our_golf_tours_list',161) ): the_row();
?>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-3 m-b-30 col-package">
<div class="packages-box">
<a href="<?php the_sub_field('title_link'); ?>"><img src="<?php the_sub_field('image'); ?>" alt="img"></a>
<div class="package-details-2">
<h3><a href="<?php the_sub_field('title_link'); ?>"><?php the_sub_field('title'); ?></a></h3>
<p><?php the_sub_field('date'); ?></p>
</div>
</div>
<div class="packages-box-bottom">
<a href="<?php the_sub_field('book_now_url'); ?>" class="btn btn-default-custom book-now-small <?php the_sub_field('hubspot_class'); ?>">Book Now <i class="fa fa-angle-right" aria-hidden="true"></i></a>
<a href="<?php the_sub_field('itinearay_url'); ?>" class="btn itinerary-blank">Itinerary </a>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
下面的这段代码是否正确?如果我错了,请纠正我。
function repeater_ad () {
echo '<div class="row">
<?php
if( have_rows('our_golf_tours_list',161) ):
?>
<?php
while( have_rows('our_golf_tours_list',161) ): the_row();
?>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-3 m-b-30 col-package">
<div class="packages-box">
<a href="<?php the_sub_field('title_link'); ?>"><img src="<?php the_sub_field('image'); ?>" alt="img"></a>
<div class="package-details-2">
<h3><a href="<?php the_sub_field('title_link'); ?>"><?php the_sub_field('title'); ?></a></h3>
<p><?php the_sub_field('date'); ?></p>
</div>
</div>
<div class="packages-box-bottom">
<a href="<?php the_sub_field('book_now_url'); ?>" class="btn btn-default-custom book-now-small <?php the_sub_field('hubspot_class'); ?>">Book Now <i class="fa fa-angle-right" aria-hidden="true"></i></a>
<a href="<?php the_sub_field('itinearay_url'); ?>" class="btn itinerary-blank">Itinerary </a>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>';
add_shortcode('sss', 'repeater_ad');
您的简码有两个问题:
A。该功能需要正确关闭:
function {
// your code
} // <-- You forgot the closing bracket
B。您应该使用return语句而不是echo:
您在函数中使用echo
,这不是最安全的操作,因为它不能保证您的内容将呈现在您认为的位置。
最好将所有内容封装到完成该功能的return语句中。
大致来说,像:
function {
// Build up content (e.g. holding it in a variable)
$content = "<h1>Fruits</h1>";
$content .= "<div>apples</div>";
$content .= "<div>oranges</div>";
// When ready, finish the function and output the content to wherever the function was called (ie the shortcode)
return $content;
}