如何在 WordPress 帖子编辑器中更改帖子“摘录”框标签的文本?

7 月前 734次浏览 0条评论 ,

我的wordpress主题让网站所有者可以选择使用每个帖子的“摘录”字段来填写每个页面的“元描述”标签。

如果我可以过滤在帖子编辑器中查看时出现在默认摘录输入控件上的文本标签和描述文本,这将很有帮助。

是否存在过滤器或钩子?可以使用一个过滤器挂钩,它在gettext 这里:

add_filter( 'gettext', 'wpse22764_gettext', 10, 2 );
function wpse22764_gettext( $translation, $original )
{
    if ( 'Excerpt' == $original ) {
        return 'My Excerpt label';
    }else{
        $pos = strpos($original, 'Excerpts are optional hand-crafted summaries of your');
        if ($pos !== false) {
            return  'My Excerpt description';
        }
    }
    return $translation;
}

也可以试试这个代码:

add_filter('gettext', 'wp_video_link_excerpt_rename', 10, 2);
function wp_video_link_excerpt_rename($translation, $original)
{
    if (get_post_type( ) == 'video_link') {
        if ('Excerpt' == $original) {
            return 'Add Video Link';
        } else {
            $pos = strpos($original, 'Excerpts are optional hand-crafted summaries of your');
            if ($pos !== false) {
                return  'Add any video link in the box above';
            }
        }
    }
    return $translation;
}

我使用了原始答案并对其进行了修改以处理不同的帖子类型:

// Customise the Excerpt field title and description via WP translation.
// Change the $post_types array to change the default Excerpt metabox title and description. The description is generic but substitutes the post_type to make it specific to the current post type.

add_filter( 'gettext', 'change_excerpt_info', 10, 2 );

function change_excerpt_info ( $translation, $original ) {
    $post_types = array( "service", "workshop", 'team' );
    $post = get_post( $post );
    $posttype = '';
    if ( $post ) {
        $posttype =  $post->post_type;
    }
    if ( in_array($posttype, $post_types) ) {
        if ( 'Excerpt' == $original ) {
             return ucfirst($posttype) . ' Teaser'; // Will appear as Service Teaser, Team Teaser, etc.
        } else $pos = strpos($original, 'Excerpts are optional hand-crafted summaries');
        if ($pos !== false) {
            // The Summary will incorporate the post_type into the description.
            $summary = 'This field will be displayed as a teaser or summary of anywhere this ' . $posttype . ' appears in a grid layout. You should always write a teaser of about 30 words. It will never be displayed on the actual ' . $posttype . ' page, so you can repeat content from other fields on this editing screen if you want.';
              return $summary;
        }
    }
    return $translation;
}

希望以上3种方法能让你在Wordpress主题开发中起到些帮助!

发布评论