为所有类别创建多个部分,然后查询每个类别的所有帖子

时间:2015-03-11 作者:simpleminded

我试图找到一种方法来创建一个循环,查询所有类别,然后查询这些类别下的帖子。我正在使用一个自定义循环来显示我的帖子,我希望它遵循相同的循环。我使用的循环是:

<?php 
    // BEGIN LOOP
    if(have_posts()) :

    // Get the initial array with random numbers
    $randomArray = getRandomNumbers();

    // Open the initial row.
    echo \'<div class="row">\';

        // CHECK FOR POSTS
        while (have_posts()) : the_post();

            // CREATE ROWS

            if (count($randomArray) < 1) {
                // Close the row and start a new one:
                echo \'</div><div class="row">\';
                // Get a fresh array with random numbers:
                $randomArray = getRandomNumbers();
            }

            $nextRandomNumber = array_pop($randomArray); // This takes the next number.


            // CREATE RANDOM COLUMN 
            echo \'<div class="col-md-\' . $nextRandomNumber . \' col-lg-\' . $nextRandomNumber . \' post clearfix"><article class="entry-content">\';

            // FETCH THUMBNAIL
            echo "<figure><a href=\'" . get_permalink() . "\'>";
            if ( $nextRandomNumber == "6" ) {
                the_post_thumbnail(\'medium\', array(\'class\' => \'img-responsive pull-left\')); 
            }
            elseif ( $nextRandomNumber == "8" ) {
                the_post_thumbnail(\'thumb-image\', array(\'class\' => \'img-responsive pull-right\')); 
            }
            elseif ( $nextRandomNumber == "3" ) {
                the_post_thumbnail(\'medium\', array(\'class\' => \'img-responsive\')); 
            }
            elseif ( $nextRandomNumber == "4" ) {
                the_post_thumbnail(\'medium\', array(\'class\' => \'img-responsive\')); 
            }
            echo "</a></figure>";


            // FETCH TITLE
            echo "<h2><a href=\'" . get_permalink() . "\'>" . get_the_title() . "</a></h2>";

            // FETCH PUBLISHED DATE
            echo "<span>Published " . get_the_date() . "</span>";

            // FETCH POST\'S CATEGORY
            foreach((get_the_category()) as $category) { echo "<h6>In <a href=\'/wordpress/category/". $category->cat_name . \' \' . "\'>" . $category->cat_name . \' \'."</a></h6>"; };
            // echo "<h6>In " . $category[0]->cat_name . "</h6>";

            // FETCH CONTENT
            the_excerpt();
            echo \' <div class="entry-meta clearfix"><a href="\' . get_author_posts_url(get_the_author_meta( \'ID\' )) .\'" class="topic-tag">\'. get_the_author_meta( \'display_name\' ) .\'</a><nav><a href="#" data-toggle="popover" data-trigger="hover" title="Popover title" data-content="And here is some amazing content. It is very engaging. Right?"><i class="fa fa-paper-plane-o"></i></a><a href="#" rel="tooltip" data-original-title="Like"><i class="fa fa-heart-o"></i></a> <a href="#" rel="tooltip" data-original-title="Add to favorites"><i class="fa fa-plus"></i></a></nav></div></article></div>\';

            // END
            endwhile;

        // Close the final row.
        echo \'</div>\';

    // CLOSE LOOP
    endif;
?>
我找到了一个帖子,它几乎满足了我的需求:Get all categories and posts in those categories. 我只是不确定如何将自定义循环添加到循环中,以获取所有类别和帖子。我想这样查询帖子:

[query each category]
<div class="bb-item">
    <div class="content">
        <div class="container scroller">
            [display custom loop for posts]
        </div>
    </div>
</div>
[end query for each category]
我对PHP和WordPress主题仍然相当陌生,所以任何建议都将不胜感激。谢谢

1 个回复
SO网友:simpleminded

想出了一个解决方案。最终使用“foreach”查询每个类别,并将我的自定义函数插入到循环中。我不确定这是不是最好的方法,但我成功了,所以呃。似乎没有其他人愿意帮忙。

结束

相关推荐

Loop for sticky posts

我用过Justin Tadlock\'s 关于如何创建仅包含粘性帖子的循环的教程。代码大致如下所示:$sticky = get_option( \'sticky_posts\' ); rsort( $sticky ); $sticky = array_slice( $sticky, 0, 2 ); query_posts( array( \'post__in\' => $sticky, \'caller_get_posts\' => 1 ) ); 根据教程,我