wordpress随机当前分类下的文章

ė 440 6 0
编者:

不懂代码的我终于把这个功能完成了,太难了。四处找代码,试错,试错,再试错,终于搞定。只随机当前分类下的文章

<?php
/*
Template Name: random
*/
?>

<?php if ( is_single() || is_page(-19705) ) {
$cat = get_the_category();
foreach($cat as $key=>$category){
$catid = $category->term_id;
}
$args = array('orderby' => 'rand','showposts' => 1,'cat' => $catid );
$query_posts = new WP_Query();
$query_posts->query($args);
while ($query_posts->have_posts()) : $query_posts->the_post();
?>

<div style=" background: #e4a10a; text-align: center; width: 91%; margin: auto;font-weight: 700; overflow: auto;">
<a href="<?php the_permalink(); ?>" style=" color: white;">试试手气</a></div>
<?php endwhile;?>
<?php wp_reset_query(); ?>

<?php } elseif(is_category() ) {
global $post;
$categories = get_the_category();
foreach ($categories as $category) ; ?>
<?php $posts = get_posts('numberposts=1&orderby=rand&category='. $category->term_id);foreach($posts as $post) : ?>
<script> location="<?php the_permalink(); ?>";</script><?php endforeach; ?>

<?php } else { ?>
<?php $rand_post=get_posts('numberposts=1&orderby=rand'); foreach($rand_post as $post) : ?>
<script> location="<?php the_permalink(); ?>";</script>
<?php endforeach; ?>
<?php ;}?>

然后把这个代表放到文章页和分类页 demo

来源-无名 - 通过投稿渠道发表
  • 灵魂发问:谁告诉你我国防空洞位置了吗?怕是99%中国人不知道,我也不知道,为什么,因为没告诉咱们啊!怕是很多当官的都不晓得吧!所以别老想着打这个那个,真打起来,你躲都不知道躲哪?最好的位置是到没有军事区,机场等落后位置,因为人家炸这些位置不仅没有利益还会被国际谴责。 --- 2025/03/08
主机推荐
:网友留言区仅供网友表达其个人观点,并不表明栖息邦最终立场。



管理员登录
注册

提示:点击上下面区域关闭

字号调整

View My Stats
空位 空位
本次加载耗时:0.563s