Question about custom plugin

时间:2016-12-27 作者:Igor S.

我是WP的新手,创建了一个小插件来显示该类别的最后10篇帖子。我使用它两次,在页面上显示这两个类别的帖子。在每篇帖子上显示图像、标题、类别和日期。插件“查询监视器”报告他对数据库进行了55次查询。我认为这太多了,为了比较,我下载了插件“分类帖子小部件”,它在相同的条件下可以减少数据库查询。我做错了什么?我从抄本上获取了信息。下面是我的周期。

<div>
    <?php
    global $post;
    $args = array(
        \'post_type\' => \'post\',
        \'post_status\' => \'publish\',
        \'posts_per_page\' => 2,
        \'category__in\' => array($instance[\'category_select\'])
    );
    $cnews = get_posts($args);
    foreach ($cnews as $post) :
        setup_postdata($post);
        ?>

        // display two first posts in one css style

    <?php endforeach;
    wp_reset_postdata(); ?>
</div>

<?php
    global $post;
    $args = array(
        \'post_type\' => \'post\',
        \'post_status\' => \'publish\',
        \'offset\' => 2,
        \'posts_per_page\' => 8,
        \'category__in\' => array($instance[\'category_select\'])
    );
    $cnews = get_posts($args);
    $i = 0;
    foreach ($cnews as $post) {
        setup_postdata($post);
        if ($i == 0) {
            echo \'<div">\';
            echo \'<ul>\';
        }
    ?>

        // display eight next posts in other css style

        <?php
        $i++;
        if ($i == 2) {
            $i = 0;
            echo \'</div>\';
        }
    }
    wp_reset_postdata();
    if ($i > 0) {
        echo \'</div>\';
    }
    echo \'</ul>\'
    ?>

1 个回复
最合适的回答,由SO网友:Tunji 整理而成

你应该改变你的get_postsWP_Query.

您正在进行大量查询,因为您正在运行setup_postdata 正在运行自己的一组查询,以从您通过的帖子中获取帖子数据get_posts.

这可以通过使用WP_Query 相反

根据您的代码,当您更改为WP\\u Query时,应该有如下内容

<div>
    <?php
    $args = array(
        \'post_type\' => \'post\',
        \'post_status\' => \'publish\',
        \'posts_per_page\' => 2,
        \'category__in\' => array( $instance[\'category_select\'] )
    );
    $cnews = new WP_Query( $args );
    if ( $cnews->have_posts() ) :
        while ( $cnews->have_posts() ) : $cnews->the_post();
    ?>

        // display two first posts in one css style

    <?php endwhile;
        wp_reset_postdata();
    endif;
    ?>
</div>

<?php
    $args = array(
        \'post_type\' => \'post\',
        \'post_status\' => \'publish\',
        \'offset\' => 2,
        \'posts_per_page\' => 8,
        \'category__in\' => array($instance[\'category_select\'])
    );
    $cnews = new WP_Query( $args );
    $i = 0;
    if ( $cnews->have_posts() ) :
        while ( $cnews->have_posts() ) : $cnews->the_post();
        if ($i == 0) {
            echo \'<div">\';
            echo \'<ul>\';
        }
        ?>

        // display eight next posts in other css style

        <?php
        $i++;
        if ($i == 2) {
            $i = 0;
            echo \'</div>\';
        }
        endwhile;
        wp_reset_postdata();
    endif;
    if ($i > 0) {
        echo \'</div>\';
    }
    echo \'</ul>\'
?>
更新要将其简化为一个查询,可以执行以下操作:

<div>
    <?php
    $args = array(
        \'post_type\' => \'post\',
        \'post_status\' => \'publish\',
        \'posts_per_page\' => 10,
        \'category__in\' => array( $instance[\'category_select\'] )
    );
    $cnews = new WP_Query( $args );
    $count = 1;
    $style = \'remaining-eight\';
    if ( $cnews->have_posts() ) :
        while ( $cnews->have_posts() ) : $cnews->the_post();
        if ( in_array( $count, range(1,2) ) )
            $style = \'first-two\';
    ?>
            <div class="<?php echo $style; ?>" >
                //post content in here
            </div>
    <?php
        $count++;
        endwhile;
        wp_reset_postdata();
    endif;
    ?>
</div>
然后您可以根据选择器进行样式设置first-tworemaining-eight.

相关推荐

当in_the_loop()为假时,何时以及为什么is_Single(‘my_cpt’)为真?

我正在使用模板系统的示例代码。此地址的页码:http://project.test/my_cpt/hello-post/.无法理解原因is_singular( \'my_cpt\' ) 是true 虽然in_the_loop() 是false.在页面模板中The Loop "E;“工程”:if ( have_posts() ) { while ( have_posts() ) { the_post(); ?>