WP 查询多个短代码不适用于同一页面或模板页面
WP Query Multiple Shortcodes not working on same page or template page
我创建了一个短代码滑块,它获取不同页面的 ID 并显示显示滑块。 Shordcode 工作正常,但问题是每当我在同一页面/模板页面上多次复制过去的简码时,它只显示第一个。
仅当我通过相同类型的短代码时才会出现此问题,但如果我在同一页面上通过任何其他短代码,它工作正常。
这是我的代码
add_shortcode( 'objectx-pages-list', 'objectx_pages_list_func' );
function objectx_pages_list_func( $atts ) {
global $post;
ob_start();
extract( shortcode_atts( array('ids' => '1186'), $atts ) );
$id_array = explode(',', $ids);
$pages_query = new WP_Query( array(
'post_type' => 'page',
'post__in' => $id_array,
'order' => 'ASC',
'orderby' => 'title',
) );
if ( $pages_query->have_posts() ) { ?>
<div class="carousel-wrapper">
<div class="owl-carousel owl-theme carousel-1" id="carousel-rooms">
<?php while ( $pages_query->have_posts() ) : $pages_query->the_post();
$featured_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div <?php post_class('item'); ?> id="post-<?php the_ID(); ?>">
<div class="row">
<div class="col-md-7">
<div class="img-rooms">
<a href="<?php the_permalink(); ?>">
<img class="img-responsive wp-post-image" src="<?php echo $featured_image; ?>"></a>
</div>
</div>
<div class="col-md-5">
<div class="detail-rooms">
<h2 class="title-room "><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php $myvariable_pages = ob_get_clean();
wp_reset_postdata();
return $myvariable_pages;
}
}
这是简码
[objectx-pages-list id="15,16,17"]
[objectx-pages-list ids="25,26,27"]
在这里你可以看到实例
http://objextheme.wpengine.com/
这个工作正常
屋顶露台和休息室
但这不起作用
本周在 Vertigo Sky Lounge
请指导我哪里做错了。谢谢
add_shortcode( 'objectx-pages-list', 'objectx_pages_list_func' );
function objectx_pages_list_func( $atts ) {
global $post;
ob_start();
extract( shortcode_atts( array('ids' => '1186'), $atts ) );
$id_array = explode(',', $ids);
$pages_query = new WP_Query( array(
'post_type' => 'page',
'post__in' => $id_array,
'order' => 'ASC',
'orderby' => 'title',
) );
if ( $pages_query->have_posts() ) { ?>
<div class="carousel-wrapper">
<div class="owl-carousel owl-theme carousel-1" id="carousel-rooms">
<?php while ( $pages_query->have_posts() ) : $pages_query->the_post();
$featured_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div <?php post_class('item'); ?> id="post-<?php the_ID(); ?>">
<div class="row">
<div class="col-md-7">
<div class="img-rooms">
<a href="<?php the_permalink(); ?>">
<img class="img-responsive wp-post-image" src="<?php echo $featured_image; ?>"></a>
</div>
</div>
<div class="col-md-5">
<div class="detail-rooms">
<h2 class="title-room "><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php $myvariable_pages = ob_get_clean();
wp_reset_postdata();
return $myvariable_pages;
}
}
Here is the error i noticed. id="carousel-rooms" Id repeating on
same page. that is why only one time it runs perfect.
我创建了一个短代码滑块,它获取不同页面的 ID 并显示显示滑块。 Shordcode 工作正常,但问题是每当我在同一页面/模板页面上多次复制过去的简码时,它只显示第一个。 仅当我通过相同类型的短代码时才会出现此问题,但如果我在同一页面上通过任何其他短代码,它工作正常。
这是我的代码
add_shortcode( 'objectx-pages-list', 'objectx_pages_list_func' );
function objectx_pages_list_func( $atts ) {
global $post;
ob_start();
extract( shortcode_atts( array('ids' => '1186'), $atts ) );
$id_array = explode(',', $ids);
$pages_query = new WP_Query( array(
'post_type' => 'page',
'post__in' => $id_array,
'order' => 'ASC',
'orderby' => 'title',
) );
if ( $pages_query->have_posts() ) { ?>
<div class="carousel-wrapper">
<div class="owl-carousel owl-theme carousel-1" id="carousel-rooms">
<?php while ( $pages_query->have_posts() ) : $pages_query->the_post();
$featured_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div <?php post_class('item'); ?> id="post-<?php the_ID(); ?>">
<div class="row">
<div class="col-md-7">
<div class="img-rooms">
<a href="<?php the_permalink(); ?>">
<img class="img-responsive wp-post-image" src="<?php echo $featured_image; ?>"></a>
</div>
</div>
<div class="col-md-5">
<div class="detail-rooms">
<h2 class="title-room "><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php $myvariable_pages = ob_get_clean();
wp_reset_postdata();
return $myvariable_pages;
}
}
这是简码
[objectx-pages-list id="15,16,17"]
[objectx-pages-list ids="25,26,27"]
在这里你可以看到实例 http://objextheme.wpengine.com/
这个工作正常 屋顶露台和休息室 但这不起作用 本周在 Vertigo Sky Lounge
请指导我哪里做错了。谢谢
add_shortcode( 'objectx-pages-list', 'objectx_pages_list_func' );
function objectx_pages_list_func( $atts ) {
global $post;
ob_start();
extract( shortcode_atts( array('ids' => '1186'), $atts ) );
$id_array = explode(',', $ids);
$pages_query = new WP_Query( array(
'post_type' => 'page',
'post__in' => $id_array,
'order' => 'ASC',
'orderby' => 'title',
) );
if ( $pages_query->have_posts() ) { ?>
<div class="carousel-wrapper">
<div class="owl-carousel owl-theme carousel-1" id="carousel-rooms">
<?php while ( $pages_query->have_posts() ) : $pages_query->the_post();
$featured_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div <?php post_class('item'); ?> id="post-<?php the_ID(); ?>">
<div class="row">
<div class="col-md-7">
<div class="img-rooms">
<a href="<?php the_permalink(); ?>">
<img class="img-responsive wp-post-image" src="<?php echo $featured_image; ?>"></a>
</div>
</div>
<div class="col-md-5">
<div class="detail-rooms">
<h2 class="title-room "><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php $myvariable_pages = ob_get_clean();
wp_reset_postdata();
return $myvariable_pages;
}
}
Here is the error i noticed. id="carousel-rooms" Id repeating on same page. that is why only one time it runs perfect.