5

调用WordPress分类描述和去除WordPress分类描述P标签

 2 years ago
source link: https://yjyj.net/learn/wordpress-learn/6199.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分类描述和去除WordPress分类描述P标签

2021-05-03

我们使用WordPress程序建网站时,在后台创建了分类目录后,可以给分类目录添加分类描述。Wordpress分类描述可以帮助我们网站做SEO优化,可以用于分类的description标签,也可以用于增加分类目录关键词密度。

file

WordPress分类描述调用方法:

分类目录页的调用

<?php echo category_description(); ?>

其它页面调用

<?php echo category_description( $category_id ); ?>

文章页面调用所属分类的描述

<?php foreach((get_the_category()) as $category){echo $category->category_description;}?>

WordPress分类描述去除P标签

使用上面的代码调用Wordpress分类描述,会自动出现P标签,不适合用于description标签,可以使用下面的代码去除Wordpress分类描述的P标签。

<?php $cat_des = category_description(); echo str_replace(array("<p>","","</p>", "\r", "\n"),"",$cat_des);?>

方法2: (添加到当前使用的WordPress主题的functions.php中)

//分类描述删除p标签
function xintheme_delete_cat_p($description) {
  $description = trim($description);
  $description = strip_tags($description,"");
  return ($description);
}
add_filter('category_description', 'xintheme_delete_cat_p');

感谢xinthemes: https://www.xintheme.com/wpjiaocheng/49754.html


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK