我已经设置了自定义帖子类型“Partnerzy”。在添加新的“合作伙伴”时,您可以选择一个类别(kredyty、leasingi等)。有了这些,我需要在页面上显示它们,如下所示:
我发现我所需要做的就是把所有“Partnerzy”帖子按类别分组,最后在网站上显示出来。
我的代码如下所示:
$catArgs = array(
\'type\' => \'post\',
\'child_of\' => 0,
\'parent\' => \'\',
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'hide_empty\' => 1,
\'hierarchical\' => 1,
\'exclude\' => \'\',
\'include\' => \'\',
\'number\' => \'\',
\'taxonomy\' => \'category\',
\'pad_counts\' => false
);
$categoryList = get_categories( $catArgs );
echo \'Sum: \'.count($categoryList);
for ($i=0; $i < count($categoryList); $i++) {
echo \'<br>Number of loop: \' . $i;
echo \'<br>Category name: \' . $categoryList[$i]->name;
$the_query_partnerzy = new WP_Query( array( post_type=>\'Partnerzy\', category_name=>$categoryList[$i]->name ) );
echo count($the_query_partnerzy);
if ($the_query_partnerzy->have_posts()) {
echo \'I got it!\';
echo \'<div class="post-list container">\';
echo \'<p class="post-title">\'. $categoryList[$i]->name .\'</p>\';
while ($the_query_partnerzy->have_posts()) {
$the_query_partnerzy->the_post();
echo the_post_thumbnail();
} // while
echo \'</div>\';
} // query partnerzy
} // for
由于我的类别很少,所以我有一个想法,将其包装在FOR循环中。就这条线而言: if ($the_query_partnerzy->have_posts())
更有趣的是。。该行:echo count($the_query_partnerzy);
实际上是在计算这个查询中的元素,但只显示1(奇怪)。有了这些,我不能再往前走了。我将感谢任何形式的帮助。