将属性传递到短码函数时出现问题

时间:2011-06-13 作者:supertrue

出于某种原因,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查询。

1 个回复
最合适的回答,由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\' );

结束

相关推荐

Nested Shortcode Detection

如果您熟悉此代码<?php $pattern = get_shortcode_regex(); preg_match(\'/\'.$pattern.\'/s\', $posts[0]->post_content, $matches); if (is_array($matches) && $matches[2] == \'YOURSHORTCODE\') { //shortcode is being used }&#