3

https://www.huhexian.com/wp-content/uploads/2021/11/default1.jpg

 2 years ago
source link: https://www.huhexian.com/12242.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的输出数量吗

2021-11-1914:25:41评论605字

我们在使用WordPress建立站点的文章页、分类页等页面,都喜欢顺便输出文章对应的标签,如果我们发布的文章标签不多的话倒没什么问题,但是如果某篇文章标签特别多的情况下,将全部的标签都输出很有可能会影响美观。

我们可以通过设置CSS代码来隐藏超出范围的标签,其实我们还可以通过代码来控制文章标签输出的数量,实现的方法很简单,只需要找到想要控制文章标签输出的文件,一般是首页、分类页或其他页面,找到以下代码:

  1. <?php the_tags('','',''); ?>

修改为以下代码:

  1. <?php
  2. $posttags = get_the_tags();
  3. $count=0;
  4. if($posttags) {
  5. foreach($posttags as $tag) {
  6. $count++;
  7. if($count<6){
  8. echo '<a href="' . get_tag_link( $tag ) . '" rel="tag" target="_blank">' . $tag->name . '</a> ';
  9. ?>

代码说明:第7行代码的6表示输出5个标签,若想要输出10个标签则改为11即可,即可比实际输出多1即可。

保存并更新文章,最后清空缓存并刷新页面即可看到文章标签的输出已经根据我们所设定的数量输出了。如下图中就仅仅输出5个标签,符合预期。

综上所述,WordPress文章不想输出全部标签是可以通过代码来控制标签输出数量的。如果也遇到这方面的困扰,不妨参考本文代码操作试试。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK