翻译自定义帖子类型

时间:2017-03-26 17:07:18

标签: wordpress translation custom-post-type

我在Wordpress中制作了自定义帖子类型。 我想在帖子类型名称的文章上显示标签。

我这样做:

<?php echo get_post_type(); ?>

输出英文名称。

现在我想用Poedit翻译它,但这不起作用。 我的自定义帖子类型如下所示:

    <?php


function register_cpt_news() {

    $singular = 'News';
    $plural = 'News';


    $labels = array(
        'name'               => _x( $plural, 'ndi' ),
        'singular_name'      => _x( $singular, 'ndi' ),
        'menu_name'          => _x( 'News', 'ndi' ),
        'name_admin_bar'     => _x( $singular, 'ndi' ),
        'add_new'            => __( 'Add New' ),
        'add_new_item'       => __( 'Add New '.$singular ),
        'new_item'           => __( 'New '.$singular ),
        'edit_item'          => __( 'Edit '.$singular ),
        'view_item'          => __( 'View '.$singular ),
        'all_items'          => __( 'All '.$plural ),
        'search_items'       => __( 'Search '.$plural ),
        'parent_item_colon'  => __( 'Parent '.$plural.':' ),
        'not_found'          => __( 'No '.$plural.' found.' ),
        'not_found_in_trash' => __( 'No '.$plural.' found in Trash.' ),
    );

    $args = array(
        'label'               => __( $plural, 'ndi' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 4,
        'can_export'          => true,
        'has_archive'         => true   ,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
        'menu_icon'           => 'dashicons-format-aside',
    );

    register_post_type( 'news', $args );

}


add_action( 'init', 'register_cpt_news' );

?>

我在做错了吗?

1 个答案:

答案 0 :(得分:0)

你可以使用这种方法

$obj = get_post_type_object('postName') ;

然后,当您想通过英语使用标签名称

回显自定义帖子名称时
$title_lan = $obj->labels->name ;
echo $title_lan ;

您在注册自定义帖子时用英语制作

然后当您想通过其他语言使用菜单名称

回显自定义帖子名称时
$title_lan = $obj->labels->menu_name ;

echo $title_lan ;

您在注册自定义帖子时使用其他语言

相关问题