wordpress通过指定的tag标签调用文章列表来制作主题页,从而拥有特色的分类列表主题.

IT资讯
IT资讯
IT资讯
1172
文章
239
评论
2019-01-0204:40:25
评论
3,556 2124字

一,首先需要将tag自动显示出自身的ID,这一点可以通过函数实现,将下面这段代码放到主题的functions.php文件当中:

//获得当前 TAG 标签 ID
function get_current_tag_id() {
$current_tag = single_tag_title('', false);//获得当前 TAG 标签名称
$tags = get_tags();//获得所有 TAG 标签信息的数组
foreach($tags as $tag) {
if($tag->name == $current_tag) return $tag->term_id; //获得当前 TAG 标签 ID,其中 term_id 就是 tag ID
}
}

 

二,其次,就是在自定义模板当中放入调用代码:

//调用文章的特色图像,如果没有,则调用默认图像
<?php
function catch_that_image() {
global $post, $posts;
$first_img = '';
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "http://localhost/default.jpg";
}
return $first_img;
}
?>
 
//调用参数
<?php
    $args=array(
        'include' => '113,100,119,118,109,110,116'  //可指定单个tag的ID或是多个tag的ID,中间用英文逗号间隔
    );
	$tags = get_tags($args);
	// 循环所有标签 
	foreach ($tags as $tag) { 
		// 得到标签ID 
		$tagid = $tag->term_id; 
		// 得到标签下所有文章 
		query_posts("showposts=-1&tag_id=$tagid"); 
    if(have_posts()) : while (have_posts()) : the_post();
?>
 
 
 
//列表模版  
 
  <li>
<div class="article">
  <!-- 输出标签标题及链接 --> 
<h2>标签: <a href="<?php echo get_tag_link($tagid);?>" title="<?php echo $tag->name?>"><?php echo $tag->name; ?></a></h2>
					<div class="articleHeader">
						<h1 class="articleTitle"><a href="<?php the_permalink();?>"><?php the_title();?></a></h1>
 
					</div>
					<div class="articleBody clearfix">
						<!--缩略图-->
						<div class="articleThumb">
							<a href="<?php the_permalink();?>"><img src="<?php echo catch_that_image(); ?>" alt="<?php the_title();?>" title="<?php the_title();?>"></a>
						</div>
						<!--摘要-->
						<div class="articleFeed">
							<p><?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 520,"……"); ?>
							</p>
						</div>
					</div>
					<div class="articleFooter clearfix">
						<ul class="articleStatu">
							<li><i class="fa fa-calendar"></i><?php echo get_the_time('Y-m-d') ?></li>
							<li><i class="fa fa-eye"></i><?php if(function_exists('custom_the_views') ) custom_the_views($post->ID); ?>次浏览</li>
							<li><a href=""><i class="fa fa-folder-o"></i><?php the_category(,) ?></a></li>
						</ul>
						<a href="<?php the_permalink();?>" class="btn btn-readmore btn-info btn-md">阅读更多</a>
					</div>
				</div>
                              </li>
                              	<?php endwhile; endif; wp_reset_query(); ?>
    <?php    }   ?>

 

第二步当中的调用特色图像部分,可有可无,视具体情况而定,如果能正常调用特色图像,那这部分的调用特色图像功能可忽视.

 

 

wordpress通过指定的tag标签调用文章列表来制作主题页,从而拥有特色的分类列表主题.

  • Copyright ©  PC在线云端  版权所有.
  • 转载请务必保留本文链接:https://nrcs.xyz/web/8131.html
WordPress 使用 Github & JsDelivr 加速静态文件 WEB技术

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

一个网站的打开速度至关重要,会直接影响搜索引擎排名和用户体验。如果您的网站加载越慢,用户离去的就会越快,而用户又是我们生存的根本。 当然网站加载速度受很多因素影响,今天我们使用 Github &...
Orgorg速率最高可达1000Mbps,流畅观看Youtube 4K、TikTok,支持 Windows、Android、iOS、Mac,支持 微信、支付宝 付款!
全球数据中心,多点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: