从 WordPress 主题的 Body 类中删除作者姓名

问题描述 投票:0回答:1

有什么方法可以从 body_class(); 中消除作者姓名吗? ?是否有任何特定的过滤器可用,仅从正文类中删除作者姓名?

请帮我解决这个问题

php wordpress-theming wordpress
1个回答
0
投票

您可以通过在functions.php 文件中添加过滤器来从body_class() 函数中删除类。在你的例子中是“作者”类。

add_filter('body_class', function (array $classes) {
   if (in_array('author', $classes)) {
      unset( $classes[array_search('author', $classes)] );
   }
   return $classes;
});

您可以在以下位置找到类名称参考的完整列表: https://developer.wordpress.org/reference/functions/get_body_class/ 并从 https://developer.wordpress.org/reference/functions/body_class/

查看更多详细信息

您还可以查找特定的类名结果并替换它。您有两个:一个带有作者姓名,另一个带有作者 ID。以下内容将author-name中的名称“bob”替换为author-hello

add_filter( 'body_class', 'replace_author_bob_name' );
function replace_author_bob_name( $classes ) {
  // You have all the classes in $classes
  // Replaces author-bob with author-hello
  $new_classes = array();
  foreach($classes as $cls) {
  
    if ($cls == "author-bob") $new_classes[] = "author-hello";
    else $new_classes[] = $cls;
  }
  return $new_classes;
}
© www.soinside.com 2019 - 2024. All rights reserved.