簡體   English   中英

WordPress的多個循環和一般循環

[英]Wordpress multiple loops and general loop

我需要為我的博客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; ?>

我確實遇到了繼續進行一般循環的麻煩。 任何建議,不勝感激。

你這里有嚴重的問題

  • 對於每個循環,您將運行兩個查詢,一個查詢使用get_posts() ,一個查詢使用query_posts

  • 您正在使用不應使用的query_posts 當您重新運行主查詢時,這會增加查詢的開銷,因此您實際上正在運行3個查詢,每個循環獲得1條帖子。 這會減慢您的頁面速度,使您在搜索引擎優化上付出高昂的代價

    此外, query_posts破壞了主查詢對象,這破壞了依賴主查詢對象的插件的數千個功能。 您應該真正花時間閱讀這篇文章 ,了解query_posts到底有多糟糕。

  • 在WordPress 1.5版中, start_wp()已被棄用。 這意味着您的代碼中有錯誤,並且應該不惜一切代價避免錯誤。 在進行開發時,您應該真正打開調試功能,因為調試將立即引發一條調試消息,告訴您您正在使用的折舊函數。 這是來自法典的文章,介紹如何使用WordPress中的調試功能。 您應該使用setup_postdata( $post )

  • 仍然在調試部分,刪除了showposts posts_per_page

  • 您正在運行一個foreach循環,但不確定要顯示的帖子。 在嘗試對動態變量執行任何操作之前,請始終確保您具有有效的值。 如果您的變量返回空值,這將避免許多錯誤。

  • 您應該真正進行格式設置,因為當所有內容打包在一行中時,代碼很難閱讀。 使用適當的縮進。 適當縮進和格式化的代碼確實有助於可讀性和調試。 另外,刪除:endforeach語法。 盡管它是有效的,但是代碼編輯器不支持此功能,這會使調試在您的代碼失敗時成為噩夢。 我總是告訴所有人使用舊的花括號,因為所有代碼編輯器都支持它們。 它們使調試非常容易

  • 總是非常重要,總是重置自定義查詢。 使用wp_reset_postdata()重置$post ,只要你撥打全球setup_postdata()the_post()中的自定義查詢。 只是出於興趣,因為我已經聲明過永遠不要使用query_posts ,所以您應該使用wp_reset_postdata()重置使用query_posts創建的自定義查詢

  • 最后,您可以在一個循環中而不是三個循環中使用指定的類別執行所需的操作。 只需使用您的櫃台即可。 如果僅用於樣式設置,則可以在css3中使用:nth child()選擇器

以下未經測試,但您可以嘗試以下

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM