动态覆盖花哨标题

时间:2017-04-12 作者:Monica Maria Crapanzano

我试图覆盖使用7主题创建的花式标题。此功能将生成7个标题:

function presscore_get_page_title() {
    $title = \'\';

    if ( is_page() || is_single() ) {
        $title = get_the_title();

    } else if ( is_search() ) {
        $title = sprintf( __( \'Search Results for: %s\', \'the7mk2\' ), \'<span>\' . get_search_query() . \'</span>\' );

    } else if ( is_archive() ) {

        if ( is_category() ) {
            $title = sprintf( __( \'Category Archives: %s\', \'the7mk2\' ), \'<span>\' . single_cat_title( \'\', false ) . \'</span>\' );

        } elseif ( is_tag() ) {
            $title = sprintf( __( \'Tag Archives: %s\', \'the7mk2\' ), \'<span>\' . single_tag_title( \'\', false ) . \'</span>\' );

        } elseif ( is_author() ) {
            the_post();
            $title = sprintf( __( \'Author Archives: %s\', \'the7mk2\' ), \'<span class="vcard"><a class="url fn n" href="\' . esc_url( get_author_posts_url( get_the_author_meta( "ID" ) ) ) . \'" title="\' . esc_attr( get_the_author() ) . \'" rel="me">\' . get_the_author() . \'</a></span>\' );
            rewind_posts();

        } elseif ( is_day() ) {
            $title = sprintf( __( \'Daily Archives: %s\', \'the7mk2\' ), \'<span>\' . get_the_date() . \'</span>\' );

        } elseif ( is_month() ) {
            $title = sprintf( __( \'Monthly Archives: %s\', \'the7mk2\' ), \'<span>\' . get_the_date( \'F Y\' ) . \'</span>\' );

        } elseif ( is_year() ) {
            $title = sprintf( __( \'Yearly Archives: %s\', \'the7mk2\' ), \'<span>\' . get_the_date( \'Y\' ) . \'</span>\' );

        } else {
            $title = __( \'Archives:\', \'the7mk2\' );

        }

    } elseif ( is_404() ) {
        $title = __( \'Page not found\', \'the7mk2\' );

    } else {
        $title = __( \'Blog\', \'the7mk2\' );

    }

    return apply_filters( \'presscore_get_page_title\', $title );
}
我使用一个插件创建了一些动态页面,该插件可以查询数据库并将结果放入动态生成的页面中:每个页面都是在调用时生成的,因此不会存储在数据库中。

现在,所有页面都从插入短代码的默认页面获取标题。我需要根据生成的页面动态更改标题。

我尝试使用此函数,但它不会影响任何内容:

function presscore_get_page_title() {
    $title = \'\';

    if ( is_page() || is_single() ) {
        $title = get_the_title();

    } else if ( is_search() ) {
        $title = sprintf( __( \'Search Results for: %s\', \'the7mk2\' ), \'<span>\' . get_search_query() . \'</span>\' );

    } else if ( is_archive() ) {

        if ( is_category() ) {
            $title = sprintf( __( \'Category Archives: %s\', \'the7mk2\' ), \'<span>\' . single_cat_title( \'\', false ) . \'</span>\' );

        } elseif ( is_tag() ) {
            $title = sprintf( __( \'Tag Archives: %s\', \'the7mk2\' ), \'<span>\' . single_tag_title( \'\', false ) . \'</span>\' );

        } elseif ( is_author() ) {
            the_post();
            $title = sprintf( __( \'Author Archives: %s\', \'the7mk2\' ), \'<span class="vcard"><a class="url fn n" href="\' . esc_url( get_author_posts_url( get_the_author_meta( "ID" ) ) ) . \'" title="\' . esc_attr( get_the_author() ) . \'" rel="me">\' . get_the_author() . \'</a></span>\' );
            rewind_posts();

        } elseif ( is_day() ) {
            $title = sprintf( __( \'Daily Archives: %s\', \'the7mk2\' ), \'<span>\' . get_the_date() . \'</span>\' );

        } elseif ( is_month() ) {
            $title = sprintf( __( \'Monthly Archives: %s\', \'the7mk2\' ), \'<span>\' . get_the_date( \'F Y\' ) . \'</span>\' );

        } elseif ( is_year() ) {
            $title = sprintf( __( \'Yearly Archives: %s\', \'the7mk2\' ), \'<span>\' . get_the_date( \'Y\' ) . \'</span>\' );

        } else {
            $title = __( \'Archives:\', \'the7mk2\' );

        }

    } elseif ( is_404() ) {
        $title = __( \'Page not found\', \'the7mk2\' );

    } elseif(  is_page_template( \'accomodations.php\' ) ){
    $title = __( \'Test\', \'the7mk2\' );
}   else {
        $title = __( \'Blog\', \'the7mk2\' );

    }

    return apply_filters( \'presscore_get_page_title\', $title );
}
有什么建议吗?

1 个回复
最合适的回答,由SO网友:cjbj 整理而成

原始函数以此行结束:

return apply_filters( \'presscore_get_page_title\', $title );
这就是你的线索。你可以build a filter 这完全推翻了$title 由该函数生成。像这样:

add_filter (\'presscore_get_page_title\',\'wpse263380_presscore_get_page_title\',10,1);
function wpse263380_presscore_get_page_title ( $title ) {
  $title = \'My awesome title\';
  return $title;
  }

相关推荐

Div-Wrap with Functions.php in ChildTheme Using Shorcode!

我只想为一个简单样式的DIV包装器创建一个短代码。在WordPress的网站上,我想添加如下内容:[quotehead]Headline text[/quotehead] ...a normal blockquote from wordpress... 输出应为:<div class=\"block_header\">Headline text</div> 我的内部功能functions.php (在childtheme中)包含以下内容:/**