WordPress 如何调用网站所有tag标签一般有二种方法,第一种是使用WORDPRESS提供的标签直接调用。
<?php wp_tag_cloud('smallest=12&largest=18&unit=px&number=0&number=30&orderby=count&order=DESC');?>
上面这种可以直接调用出网站中所有的tag标签,但是样式比较单调,比较难控制它的样式。如果想更灵活的调用tag标签,可以使用以下的代码:
<?php
$tags = get_tags( array( 'orderby' => 'count', 'order' => 'DESC', 'number' => '30') );
foreach ( $tags as $tag ) {
$tag_link = get_tag_link( $tag->term_id );
echo '<li><a href="'.$tag_link.'">'.$tag->name.'</a></li>';
}
?>
排序预设是以名称(name)做排序,若是自己写函数取标签清单,我们有两个栏位可以做排序,一是count、二是term_id。orderby的后面若是留空,则对term_id 排序。
我们可以通过TAG标签的ID来获取这个标签下的所有文章。
<?php
$tag = $tag->term_id; //标签名/ID
$args=array(
'tag' => $tag->term_id,
'showposts'=>5, //输出的文章数量
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" ><?php the_title(); ?></a>
<?php
endwhile;
}
wp_reset_query();
?>
还可以直接从数据库中读取所有的TAG标签。
<?php
$varstag = $wpdb -> get_results("SELECT * FROM `wp_term_taxonomy` where taxonomy='post_tag' LIMIT 4000", ARRAY_A);
foreach($varstag as $var){
?>
<a href="<?php echo get_tag_link($var['term_id']); ?>" target="_blank"><?php $tagnqme = get_tag($var['term_id']);echo $tagnqme->name;?></a>
<?php }?>
调用指定栏目下的tag标签相关文章列表
<?php
global $post;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
foreach ($post_tags as $tag) {
// 获取标签列表
$tag_list[] .= $tag->term_id;
}
$cat = get_the_category();
foreach($cat as $key=>$category){
$catid = $category->term_id;
}
// 随机获取标签列表中的一个标签
$post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];
// 该方法使用 query_posts() 函数来调用相关文章,以下是参数列表
$args = array(
'tag__in' => array($post_tag),
'cat' => $catid, // 不包括的分类ID
'post__not_in' => array($post->ID),
'showposts' => 6, // 显示相关文章数量
'caller_get_posts' => 1
);
query_posts($args);
if (have_posts()) {
while (have_posts()) {
the_post(); update_post_caches($posts); ?>
<div id="post-<?php the_ID(); ?>" class="col-md-4 mb-4">
<div class="portfolio">
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<div class="image">
<img src='<?php if ( has_post_thumbnail() ) { ?>
<?php
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
echo $large_image_url[0];
?>
<?php } else {?>
<?php bloginfo('template_url'); ?>/images/noneimg-portfolio.png
<?php } ?>' class='' alt='<?php the_title(); ?>'>
<div class="hover-effect">
<div class="hover-effect-inn"></div>
</div>
</div>
<h1><?php the_title(); ?></h1>
<p><?php the_excerpt(); ?></p>
</a>
</div>
</div>
<?php
}
}
else {
echo '<li>* 暂无相关文章</li>';
}
wp_reset_query();
}
else {
echo '<li>* 暂无相关文章</li>';
}
?>
评论前必须登录!
注册