Wordpress:基于插件的自定义帖子类型

时间:2018-01-05 17:34:34

标签: php css wordpress

我想在Wordpress网站上制作自定义帖子类型。我是根据这两篇文章开始的:Custom post typeSite specific plugins

我的代码到目前为止:

mysite-core.php

<?php
/*
Plugin Name: My awesome site core plugin
Description: Core functions for my site
*/
/* Start Adding Functions Below this Line */

// Our custom post type function
function create_posttype() {

    register_post_type( 'bar',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Bars' ),
                'singular_name' => __( 'Bar' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'bars'),
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

/* Stop Adding Functions Below this Line */
?>

我的问题:在第一篇文章中,他们为帖子类型提供了选项:

/*
* Creating a function to create our CPT
*/

function custom_post_type() {

// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Movies', 'Post Type General Name', 'twentythirteen' ),
        'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentythirteen' ),
        'menu_name'           => __( 'Movies', 'twentythirteen' ),
        'parent_item_colon'   => __( 'Parent Movie', 'twentythirteen' ),
        'all_items'           => __( 'All Movies', 'twentythirteen' ),
        'view_item'           => __( 'View Movie', 'twentythirteen' ),
        'add_new_item'        => __( 'Add New Movie', 'twentythirteen' ),
        'add_new'             => __( 'Add New', 'twentythirteen' ),
        'edit_item'           => __( 'Edit Movie', 'twentythirteen' ),
        'update_item'         => __( 'Update Movie', 'twentythirteen' ),
        'search_items'        => __( 'Search Movie', 'twentythirteen' ),
        'not_found'           => __( 'Not Found', 'twentythirteen' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
    );

// Set other options for Custom Post Type

    $args = array(
        'label'               => __( 'movies', 'twentythirteen' ),
        'description'         => __( 'Movie news and reviews', 'twentythirteen' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );

    // Registering your Custom Post Type
    register_post_type( 'movies', $args );

}

/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/

add_action( 'init', 'custom_post_type', 0 );

后来:

  

另请注意重复使用二十四,这称为文字   域。如果您的主题已准备好翻译,并且您需要自定义   要翻译的帖子类型,那么您需要提及文本域   你的主题使用。您可以在里面找到主题的文本域   主题目录中的style.css文件。将提及文本域   在文件的标题中。

但我的自定义帖子类型是基于插件而不是基于主题的。我该如何更改twentythirteen部分? mysite-core

1 个答案:

答案 0 :(得分:1)

__()函数只是一个翻译器。见WP的function reference。因此,要继续使其可翻译,请将文本域(也称为twentythirteen)更改为应在何处进行翻译。在您的情况下mysite-core。这是最佳实践,因此您的插件可以以标准方式进行国际化。请参阅here