首页 文章

如何排序Wordpress Headers 标签部分?

提问于
浏览
0

我需要改变 Headers 部分的位置 . 现在我的主页呈现:

“blogname分隔符说明”

我想把blogname放在最后:

“描述分隔符blogname”

我的主题支持 title-tag ,我读到它有3个过滤器,它们是:

add_theme_support( 'title-tag' );

  • pre_get_document_title 短路 wp_get_document_title() 如果返回空值以外的任何内容 .

  • document_title_separator 过滤 Headers 部分之间的分隔符 .

  • document_title_parts 过滤组成文档 Headers 的部分,以关联数组的形式传递 .

如何使用它们来改变 Headers 部分的位置?

2 回答

  • 1

    在主页或首页WordPress上更改 Headers 部分

    @retroriff,如果您需要重新订购 titletagline 作为主页上wp Headers 的一部分,您可以使用过滤器document_title_parts,并使用函数触发 . 首先,我们需要取消它们,然后重新构建为订单 . 这是我的示例方法:

    add_filter( 'document_title_parts', 'wp36469459_document_title_parts', 1, 1 );
    function wp36469459_document_title_parts( $title )
    {
        if ( is_home() || is_front_page() ) {
    
            //we unset title and tagline
            unset( $title['title'], $title['tagline'] );
    
            //re-build and order
            $title['tagline'] = get_bloginfo( 'description', 'display' );
            $title['title']   = get_bloginfo( 'name', 'display' );
        }
    
        return $title;
    }
    

    要么

    add_filter( 'document_title_parts', 'wp36469459_document_title_parts', 1, 1 );
    function wp36469459_document_title_parts( $title )
    {
        if ( is_home() || is_front_page() ) {
            $_title   = $title['title'];
            $_tagline = $title['tagline'];
            unset( $title['title'], $title['tagline'] );
            $title['tagline'] = $_tagline;
            $title['title']   = $_title;
        }
        return $title;
    }
    

    您可以根据需要调整代码 .

  • 1

    如果您只需要反转 Headers 部分(对于主页),那么这有效:

    function theme_document_title_parts($title) {
      return (is_home() || is_front_page()) ? array_reverse($title) : $title;
    }
    
    add_filter('document_title_parts', 'theme_document_title_parts');
    

相关问题