Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress多重循环和一般循环_Php_Wordpress - Fatal编程技术网

Php Wordpress多重循环和一般循环

Php Wordpress多重循环和一般循环,php,wordpress,Php,Wordpress,我需要为我的blog home.php创建自定义多个循环,以显示定义类别的3个不同内容布局,然后继续执行常规循环,在前3个循环中排除帖子ID 到目前为止,我对已定义类别的3种不同内容布局进行了分类category\u name=arts: <?php $posts = get_posts('numberposts=1&offset=0'); foreach ($posts as $post) : start_wp(); ?> <?php static $count1 =

我需要为我的blog home.php创建自定义多个循环,以显示定义类别的3个不同内容布局,然后继续执行常规循环,在前3个循环中排除帖子ID

到目前为止,我对已定义类别的3种不同内容布局进行了分类
category\u name=arts

<?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; ?>


我确实被困在继续进行常规循环。非常感谢您的建议。

您在这里遇到了严重的问题

  • 对于每个循环,您运行两个查询,一个使用
    get\u posts()
    ,另一个使用
    query\u posts

  • 你正在使用你永远不应该使用的
    query\u posts
    。当您重新运行主查询时,这会给您的查询增加巨大的开销,因此您实际上要运行3个查询,以便为每个循环获得1个post。这会降低你的页面速度,这在搜索引擎优化方面会让你付出高昂的代价

    此外,
    query\u posts
    破坏了主查询对象,从而破坏了依赖于主查询对象的数千个函数和插件。你真的应该花点时间看看
    query\u posts
    到底有多糟糕

  • 在WordPress版本1.5中已经贬值。这意味着您的代码有bug,应该不惜一切代价避免bug。您应该在开发时真正启用debug,因为debug会立即抛出一条调试消息,告诉您正在使用已折旧的函数。关于如何使用WordPress中的调试功能。您应该使用
    setup\u postdata($post)

  • 仍然在调试部分,
    showposts
    被删除,取而代之的是
    posts\u per\u page

  • 您正在运行一个
    foreach
    循环,但没有确保要显示帖子。在尝试使用动态变量执行任何操作之前,请始终确保具有有效的值。这将避免在变量返回空值时出现大量错误

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

  • 总是,非常重要,总是重置自定义查询。无论何时调用自定义查询中的
    setup\u postdata()
    the\u post()
    ,都可以使用
    wp\u reset\u postdata()
    重置
    $post
    全局。出于兴趣,因为我已经声明绝不使用
    query\u posts
    ,所以您应该使用
    wp\u reset\u postdata()
    重置使用
    query\u posts创建的自定义查询

  • 最后,您只需在一个循环(而不是三个循环)中使用指定的类别即可完成所需的操作。只需利用你的计数器。如果这纯粹是为了设计样式,您只需使用

以下内容未经测试,但您可以尝试以下内容

$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

你的代码非常慢而且非常糟糕。您应该完全删除
query\u posts
,它会中断主查询对象。使用
get\u posts
WP\u Query
。从那里开始。您还将每个循环运行两次。@PieterGoosen您能分享示例代码吗?感谢您提供的有用且信息丰富的解决方案。非常有帮助:)我的荣幸。享受;-)你能帮我个忙吗?我也在试着做类似的事情,但我想不出来。。