简码显示在页面构建器中但破坏了前端
Shortcode showing in page builder but breaks front end
我正在使用 elementor,它是 Wordpress 的页面构建器,我创建了一个短代码来显示其中的自定义 post 类型循环...
当我插入短代码时,它在编辑器中正确显示,但是当我保存它并尝试正常访问页面时,代码似乎破坏了页面并且页面一直重复......这是页面 link:http://webserver-meetandengage-com.m11e.net/about-us/ 加载需要一点时间,但您会看到所有内容都在重复...
我在想我可能没有正确关闭循环之类的,但我看不出哪里出错了!还值得注意的是,当直接添加到模板文件中时,循环工作正常。
循环在这里:
<div class="container team-members-container">
<h2 style="font-weight: bold; text-align: center; margin:70px 0 70px 0;">The Team</h2>
<div class="row">
<?php
$args = array(
'post_type' => 'team_members'
// 'orderby' => 'none'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="col-sm-4">
<a href="<?php the_permalink(); ?>">
<?php
$image = get_field('photo');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
<h2 class="team-name"><?php the_field('name'); ?></h2>
<p class="team-position"><?php the_field('position'); ?></p>
</a>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
</div>
</div>
循环包含在它自己的名为 team.php 的文件中。我用来创建短代码的 functions.php 文件中的代码是:
function get_team($atts) {
ob_start();
get_template_part('inc/team');
return ob_get_clean();
}
add_shortcode('team', 'get_team');
正在创建要在我的页面编辑器中使用的简码 [team]
。
谁能看出问题出在哪里?感谢观看:)
尝试将 if ( have_posts() )
更改为 if ( $the_query->have_posts() )
条件需要正确访问 $the_query 对象。
试试这个
<div class="row">
<?php
$args = array(
'post_type' => 'post'
// 'orderby' => 'none'
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="col-sm-4">
<a href="<?php the_permalink(); ?>">
<?php
$image = get_field('photo');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
<h2 class="team-name"><?php //the_field('name'); ?></h2>
<p class="team-position"><?php //the_field('position'); ?></p>
</a>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
</div>
</div>
我正在使用 elementor,它是 Wordpress 的页面构建器,我创建了一个短代码来显示其中的自定义 post 类型循环...
当我插入短代码时,它在编辑器中正确显示,但是当我保存它并尝试正常访问页面时,代码似乎破坏了页面并且页面一直重复......这是页面 link:http://webserver-meetandengage-com.m11e.net/about-us/ 加载需要一点时间,但您会看到所有内容都在重复...
我在想我可能没有正确关闭循环之类的,但我看不出哪里出错了!还值得注意的是,当直接添加到模板文件中时,循环工作正常。
循环在这里:
<div class="container team-members-container">
<h2 style="font-weight: bold; text-align: center; margin:70px 0 70px 0;">The Team</h2>
<div class="row">
<?php
$args = array(
'post_type' => 'team_members'
// 'orderby' => 'none'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="col-sm-4">
<a href="<?php the_permalink(); ?>">
<?php
$image = get_field('photo');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
<h2 class="team-name"><?php the_field('name'); ?></h2>
<p class="team-position"><?php the_field('position'); ?></p>
</a>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
</div>
</div>
循环包含在它自己的名为 team.php 的文件中。我用来创建短代码的 functions.php 文件中的代码是:
function get_team($atts) {
ob_start();
get_template_part('inc/team');
return ob_get_clean();
}
add_shortcode('team', 'get_team');
正在创建要在我的页面编辑器中使用的简码 [team]
。
谁能看出问题出在哪里?感谢观看:)
尝试将 if ( have_posts() )
更改为 if ( $the_query->have_posts() )
条件需要正确访问 $the_query 对象。
试试这个
<div class="row">
<?php
$args = array(
'post_type' => 'post'
// 'orderby' => 'none'
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="col-sm-4">
<a href="<?php the_permalink(); ?>">
<?php
$image = get_field('photo');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
<h2 class="team-name"><?php //the_field('name'); ?></h2>
<p class="team-position"><?php //the_field('position'); ?></p>
</a>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
</div>
</div>