我试图找到一种方法来创建一个循环,查询所有类别,然后查询这些类别下的帖子。我正在使用一个自定义循环来显示我的帖子,我希望它遵循相同的循环。我使用的循环是:
<?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主题仍然相当陌生,所以任何建议都将不胜感激。谢谢