【问题标题】:Wordpress multiple loops and general loopWordpress 多循环和一般循环
【发布时间】:2016-01-13 01:25:31
【问题描述】:

我需要为我的博客 home.php 创建自定义多个循环,以显示定义类别的 3 种不同内容布局,然后继续进行一般循环,前 3 个循环中的帖子 ID 除外。

到目前为止,我制作了定义类别category_name=arts的3种不同内容布局:

<?php $posts = get_posts('numberposts=1&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "1") { break; } else { ?>

<?php the_title(); ?>

<?php $count1++; } ?>
<?php endforeach; ?>


<?php query_posts('category_name=arts&showposts=1'); ?>
<?php $posts = get_posts('numberposts=1&offset=1'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count2 = 0; if ($count2 == "1") { break; } else { ?>

<?php the_title(); ?>

<?php $count2++; } ?>
<?php endforeach; ?>


<?php query_posts('category_name=arts&showposts=1'); ?>
<?php $posts = get_posts('numberposts=1&offset=2'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count3 = 0; if ($count3 == "1") { break; } else { ?>

<?php the_title(); ?>

<?php $count3++; } ?>
<?php endforeach; ?>

我确实卡住了继续进行一般循环。任何建议都非常感谢。

【问题讨论】:

  • 你的代码非常慢而且非常糟糕。您应该完全删除query_posts,它会破坏主查询对象。使用get_postsWP_Query。从那里继续。您还将每个循环运行两次。
  • @PieterGoosen 你能分享示例代码吗?

标签: php wordpress


【解决方案1】:

这里有严重的问题

  • 对于每个循环,您运行两个查询,一个使用get_posts(),一个使用query_posts

  • 您正在使用您永远不应该使用的query_posts。当您重新运行主查询时,这会给您的查询增加巨大的开销,因此您实际上正在运行 3 个查询来为每个循环获取 1 个帖子。这会减慢您的页面速度,从而在 SEO 方面付出高昂的代价

    此外,query_posts 破坏了主查询对象,它破坏了数千个函数,一个依赖于主查询对象的插件。您真的应该花时间阅读this post,了解query_posts 有多糟糕。

  • start_wp() 已在 WordPress 1.5 版中贬值。这意味着您的代码有错误,应该不惜一切代价避免错误。您应该在开发时真正打开调试,因为调试会立即抛出一条调试消息,告诉您您正在使用折旧的功能。 Here is an article from the codex 关于如何使用 WordPress 中的调试功能。你应该使用setup_postdata( $post )

  • 仍处于调试部分,showposts 被删除以支持posts_per_page

  • 您正在运行foreach 循环,但没有确保您有要显示的帖子。在尝试对动态变量进行任何操作之前,请始终确保您具有有效值。如果您的变量返回空值,这将避免许多错误。

  • 您应该真正处理您的格式,因为当所有内容都打包在一行中时,您的代码很难阅读。使用适当的缩进。正确缩进和格式化的代码对可读性和调试有很大帮助。此外,删除 :endforeach 语法。虽然它是有效的,但它不受代码编辑器的支持,如果你的代码失败,这会使调试成为一场噩梦。我总是告诉大家使用旧式花括号,因为所有代码编辑器都支持它们。它们使调试变得非常容易

  • 始终,非常重要,始终重置自定义查询。每当您在自定义查询中调用setup_postdata()the_post() 时,使用wp_reset_postdata() 重置$post 全局。只是出于兴趣,因为我已经声明永远不要使用query_posts,您应该使用wp_reset_postdata() 重置使用query_posts 创建的自定义查询

  • 最后,您可以只在一个循环而不是三个循环中对指定的类别执行所需的操作。只需使用您的柜台。如果这纯粹是为了造型,你可以简单地使用:nth child() selector in css3

以下未测试,但您可以尝试以下

$special_cat_args = [
    'category_name' => 'art',
    'posts_per_page' => 3,
    //Add extra arguments here
];
$art_posts = get_posts( $special_cat_args );

// Setup a variable to store the post ID's so we can exclude them in the 'main' query
$ids_array = [];

// Check if we have posts before we continue
if ( $art_posts ) {
    // Start our counter
    $counter = 0;
    
    // Start the loop
    foreach ( $art_posts as $post ) {
        // Setup postdata, must use $post
        setup_postdata( $post );

        // Store the post id in an array to exclude in "main" query
        $ids_array[] = $post->ID;

        // Do something separate for every post
        if ( 0 == $counter ) {
            // Do something for post one
        } elseif ( 1 == $counter ) {
            // Do something for post two
        } elseif ( 2 == $counter ) {
            // Do something for post three
        }

        // Update the counter
        $counter++;
    } //endforeach $art_posts
    wp_reset_postdata(); // VERY VERY IMPORTANT
} //endif $art_posts

// Now we can do our "main" query and exclude the three posts from the special category
$args = [
    'post__not_in' => $ids_array, // Exclude the three post from previous query
    // Rest of your arguments
];
$q = get_posts( $args );
// Run your loop

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
  • 2018-07-26
  • 1970-01-01
  • 2017-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多