

WordPress的TAG页面如何添加标签缩略图与简介
source link: https://www.huhexian.com/10747.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

WordPress的TAG页面如何添加标签缩略图与简介
使用WordPress的用户都知道,我们的标签页面是类似于其他cms的专题功能的,WordPress的TAG页面一般用到的都比较少,也不是很在意,但也会有一些追求极致或者美观的朋友,想要给tag标签页面添加缩略图和简介,那么我们应该如何去调用这些标签呢?下面给大家分享一下WordPress的TAG页面如何添加标签缩略图与简介。
标签页的模板名
WordPress TAG标签页的模板名为tag.php;
标签名的调用
在tag.php中,调用当前标签名可以直接使用调用当前分类名的标签。
- <?php the_title(); ?>
标签简介的调用
WordPress TAG标签与分类一样,是可以写简介和摘要的,它的调用代码:
- <?php $catdess= tag_description(); echo str_replace(array('\r\n', '\r', '\n','<p>','</p>'), '', $catdess);?>
标签下文章列表调用
调用当前标签下的文章列表与分类目录是一样的。
- <?php if (have_posts()) : ?>
- <?php while (have_posts()) : the_post(); ?>
- <?php endwhile;?>
- <?php endif; ?>
标签缩略图的调用
如果想给TAG标签添加一个缩略图,可以通过相关插件来实现。调用缩略图的代码:
- <?php echo get_term_meta(get_current_tag_id(),'catimg',true);?>
当前标签的ID调用
如果想调用出当前标签ID号,需要先在FUNCTIONS.php 里添加下函数:
- 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
再使用以下的代码调用出TAG标签的ID号;
- <?php echo get_current_tag_id();?>
获取当前TAG的文章数量
想获取当前TAG的文章数量也需要先放上函数代码:
- //根据标签ID获取文章数
- function get_tag_post_count_by_id( $tag_id ) {
- $tag = get_term_by( 'id', $tag_id, 'post_tag' );
- _make_cat_compat( $tag );
- return $tag->count;
再使用下面的代码调用:
- <?php echo get_tag_post_count_by_id( get_current_tag_id() ) ?>
通过TAG标签ID获取文章列表
- <?php
- $args=array(
- 'tag_id' => $tag->term_id,
- 'posts_per_page' => 5,
- query_posts($args);
- if(have_posts()) : while (have_posts()) : the_post();
- ?>
- <a href='<?php the_permalink(); ?>' title='<?php the_title(); ?>'><?php the_title(); ?></a>
- <?php endwhile; endif; wp_reset_query();?>
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK