wordpress随机当前分类下的文章

ė 207 6 0
【腾讯云】云产品618限时秒活动广告
编者:

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

<?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

所属分类: 转载来源:无名 - 通过投稿渠道发表
XM 广告
评论区 (0 条评论/207阅) 网友评论仅供其表达个人看法,并不表明一介小民立场。