wordpress调用指定分类下的随机文章(图文列表)

IT资讯
IT资讯
IT资讯
1163
文章
257
评论
2019-05-3005:37:15
评论
2,712 1632字

代码如下,意思很简单,相信诸君看一下就能明白了:

 

<!--  相关推荐  -->
				<div id="Recommend">
					<h2 id="Recommend_title">
						相关推荐:
					</h2>
					<div id="Recommend_list">
						<ul>
							<?php $cat = get_the_category(); foreach($cat as $key=>$category) {$catid = $category->term_id;} $args = array('orderby' => 'rand','showposts' => 6,'cat' => $catid ); $query_posts = new WP_Query(); $query_posts->query($args); while ($query_posts->have_posts()) : $query_posts->the_post(); ?>
							<li>
								<a href="<?php the_permalink();?>" class="Recommend_image" title="点击查看作品【<?php the_title();?>】详情">
									<img src="<?php $full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full'); echo $full_image_url[0]; ?>" alt="作品【<?php the_title();?><?php echo wp_trim_words( get_the_excerpt(), 40 ); ?>"  style="width:400px;height:200px;" />
								</a>
								<h2 class="Recommend_entrytitle">
									<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
								</h2>
							</li>
							<?php endwhile;?>
							<?php wp_reset_query(); ?>
						</ul>
					</div>
				</div>

 

 

 

PS:   如果还是不行,请在当前主题的functions.php文件当中添加如下代码即可:

 

//文章列表缩略图
function wpforce_featured() {
 
global $post;
 
$already_has_thumb = has_post_thumbnail($post ->ID);
 
if (!$already_has_thumb)  {
 
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
 
if ($attached_image) {
 
foreach ($attached_image as $attachment_id => $attachment) {
 
set_post_thumbnail($post ->ID, $attachment_id);
 
}
 
}
 
}
 
}  
 
add_action('the_post', 'wpforce_featured');
 
add_action('save_post', 'wpforce_featured');
 
add_action('draft_to_publish', 'wpforce_featured');
 
add_action('new_to_publish', 'wpforce_featured');
 
add_action('pending_to_publish', 'wpforce_featured');
 
add_action('future_to_publish', 'wpforce_featured');
  • Copyright ©  PC在线云端  版权所有.
  • 转载请务必保留本文链接:https://www.gaofumei.net/web/wordpress_web/8223.html
WordPress 使用 Github & JsDelivr 加速静态文件 WEB技术

WordPress 使用 Github & JsDelivr 加速静态文件

一个网站的打开速度至关重要,会直接影响搜索引擎排名和用户体验。如果您的网站加载越慢,用户离去的就会越快,而用户又是我们生存的根本。   当然网站加载速度受很多因素影响,今天我们使用 Gith...
Orgorg速率最高可达1000Mbps,流畅观看Youtube 4K、TikTok,支持 Windows、Android、iOS、Mac,支持 微信、支付宝 付款!
超高性价比,新用户注册送1元,可直接在商店购买套餐。节点和负载服务器很多,稳定性和速度有保障。有共享流媒体会员。有三端VPN程序。最萌の云 ☁️顶级云服务商,超过50个G口节点,上百台服务器负载加速,稳且快,高峰秒8K。提供网飞等会员共享,超值!限时免费尝鲜!
全球数据中心,多点BGP保证速度,无视晚高峰,全天4K秒开,IPLC专线无惧封锁
全IEPL /青云跨境,高峰时期稳定8K播放,流媒体影视, ChatGPT 解锁保障,客户端无日志保护您的隐私安全,稳定运行5年+
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: