[英]Shortcode showing in page builder but breaks front end
I'm using elementor, a page builder for Wordpress and I've created a short code to display a custom post type loop inside of it... 我正在使用elementor,它是Wordpress的页面构建器,并且创建了一个简短的代码以在其中显示自定义帖子类型循环。
When I insert the shortcode, it shows up correctly in the editor but when I save it and try and visit the page normally, the code seems to have broken the page and the page just keeps repeating forever... Here's the page link: http://webserver-meetandengage-com.m11e.net/about-us/ it take a little while to load but you will see it all repeating... 当我插入简码时,它会在编辑器中正确显示,但是当我保存它并尝试正常访问该页面时,该代码似乎破坏了该页面,并且该页面一直不断重复出现……这是页面链接: http ://webserver-meetandengage-com.m11e.net/about-us/加载需要一些时间,但您会看到所有重复的内容...
I'm thinking I might not have closed the loop properly or something, but I cant see where I'm going wrong! 我以为我可能没有正确地关闭循环之类的东西,但是我看不到哪里出了问题! It's also worth noting the loop works fine when added directly into a template file.
还要注意的是,将循环直接添加到模板文件后可以正常工作。
The loop is here: 循环在这里:
<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>
and the loop is contained in its own file called team.php. 循环包含在自己的名为team.php的文件中。 The code in the functions.php file I'm using to create the shortcode is:
我用来创建简码的functions.php文件中的代码是:
function get_team($atts) {
ob_start();
get_template_part('inc/team');
return ob_get_clean();
}
add_shortcode('team', 'get_team');
Creating the shortcode [team]
to use in my page editor. 创建要在我的页面编辑器中使用的简码
[team]
。
Can anyone see where the problem might be? 谁能看到问题所在? Thanks for looking :)
感谢您的光临:)
Try changing if ( have_posts() )
to if ( $the_query->have_posts() )
尝试将
if ( have_posts() )
更改为if ( $the_query->have_posts() )
The conditional needs to access the $the_query object properly. 有条件的需要正确访问$ the_query对象。
https://codex.wordpress.org/Class_Reference/WP_Query https://codex.wordpress.org/Class_Reference/WP_Query
Try this 尝试这个
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.