WordPress:随机显示 4 个类别和 3 个最新帖子
WordPress: Show random 4 categories with 3 last posts
我正忙于建立一个随机显示 4 个类别的行,其中包含类别标题和其下的 3 个最新帖子。但是我不知道如何以随机顺序获取类别,因为 'orderby' 不工作...
有人可以帮我吗?
我使用的代码:
<?php
//for each category, show all posts
$cat_args = array(
'orderby' => 'rand',
'order' => 'ASC'
);
$limit = 4;
$counter = 0;
$categories = get_categories($cat_args);
foreach ($categories as $category):
if ($counter < $limit) {
$args = array(
'showposts' => 3,
'category__in' => array(
$category->term_id
),
'caller_get_posts' => 1
);
$posts = get_posts($args);
if ($posts) {
echo '<h3><a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>' . $category->name . '</a> </h3>';
foreach ($posts as $post) {
setup_postdata($post);
?>
<p><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>
<?php
$counter++;
endforeach;
?>
您可以使用它来随机获取猫:
$categories = get_the_category($my_query->post->ID);
$catCount = count($categories);
//select a random category id
$id = rand(0,$catCount-1);
//cat id
$catId = $categories[$id]->term_id;
and replace to lines like
if(is_array($cat)) {
$categories = get_the_category($my_query->post->ID);
$catCount = count($categories);
//select a random category id
$id = rand(0,$catCount-1);
//cat id
$catId = $categories[$id]->term_id;
} else {
$catId = $cat;
}
删除下面的代码。这仅适用于帖子。
$cat_args = array(
'orderby' => 'rand',
'order' => 'ASC'
);
使用如下代码:
$categories = get_categories();
shuffle ($categories); // Just add shuffle and you will get random categories.
我正忙于建立一个随机显示 4 个类别的行,其中包含类别标题和其下的 3 个最新帖子。但是我不知道如何以随机顺序获取类别,因为 'orderby' 不工作...
有人可以帮我吗?
我使用的代码:
<?php
//for each category, show all posts
$cat_args = array(
'orderby' => 'rand',
'order' => 'ASC'
);
$limit = 4;
$counter = 0;
$categories = get_categories($cat_args);
foreach ($categories as $category):
if ($counter < $limit) {
$args = array(
'showposts' => 3,
'category__in' => array(
$category->term_id
),
'caller_get_posts' => 1
);
$posts = get_posts($args);
if ($posts) {
echo '<h3><a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>' . $category->name . '</a> </h3>';
foreach ($posts as $post) {
setup_postdata($post);
?>
<p><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>
<?php
$counter++;
endforeach;
?>
您可以使用它来随机获取猫:
$categories = get_the_category($my_query->post->ID);
$catCount = count($categories);
//select a random category id
$id = rand(0,$catCount-1);
//cat id
$catId = $categories[$id]->term_id;
and replace to lines like
if(is_array($cat)) {
$categories = get_the_category($my_query->post->ID);
$catCount = count($categories);
//select a random category id
$id = rand(0,$catCount-1);
//cat id
$catId = $categories[$id]->term_id;
} else {
$catId = $cat;
}
删除下面的代码。这仅适用于帖子。
$cat_args = array(
'orderby' => 'rand',
'order' => 'ASC'
);
使用如下代码:
$categories = get_categories();
shuffle ($categories); // Just add shuffle and you will get random categories.