出于某种原因,my shortcode函数中的WP\\u Query实例不会接受从该快捷码发送的查询属性。短代码如下:[postlist query=“post_type=any&;posts_per_page=5”style=“list”]
这是代码。
function hey_query_shortcode( $atts ) {
extract( shortcode_atts( array(
\'query\' => \'\',
\'style\' => \'\'
), $atts ) );
ob_start();
$the_query = new WP_Query( $query );
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
<h2>I know $style is set bc it shows up here: <?php echo $style; ?></h2>
<h2>I know $query is set bc it shows up here: <?php echo $query; ?></h2>
<?php
endwhile;
echo \'<hr>\';
wp_reset_postdata();
$list = ob_get_clean();
return $list;
}
add_shortcode( \'postlist\', \'hey_query_shortcode\' );
知道这是怎么回事吗$查询回显良好,但由于某种原因,它不会影响WP\\U查询。
最合适的回答,由SO网友:Hameedullah Khan 整理而成
很抱歉,我删除了我的评论,因为我发现了问题。您需要对查询变量执行html实体解码。使用以下代码。
function hey_query_shortcode( $atts ) {
extract( shortcode_atts( array(
\'query\' => \'\',
\'style\' => \'\'
), $atts ) );
$query = html_entity_decode( $query );
ob_start();
$the_query = new WP_Query( $query );
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
<h2>I know $style is set bc it shows up here: <?php echo $style; ?></h2>
<h2>I know $query is set bc it shows up here: <?php echo $query; ?></h2>
<?php
endwhile;
echo \'<hr>\';
wp_reset_postdata();
$list = ob_get_clean();
return $list;
}
add_shortcode( \'postlist\', \'hey_query_shortcode\' );