Wordpress自定义帖子类型未在外观中显示>菜单

时间:2015-01-23 18:11:52

标签: wordpress custom-post-type

是的,我一直在搜索,似乎唯一的原因是因为它没有在WordPress的“屏幕选项”菜单中检查。我的问题是,自定义帖子类型没有显示在屏幕选项中,所以我想知道是否有人可以查看我的代码,看看我是否做错了。

function my_custom_post_maryland() {
$labels = array(
    'name'               => _x( 'Maryland', 'post type general name' ),
    'singular_name'      => _x( 'Maryland', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'Maryland page' ),
    'add_new_item'       => __( 'Add New Maryland Page' ),
    'edit_item'          => __( 'Edit Page' ),
    'new_item'           => __( 'New Page' ),
    'all_items'          => __( 'All Pages' ),
    'view_item'          => __( 'View Page' ),
    'search_items'       => __( 'Search Maryland Pages' ),
    'not_found'          => __( 'No Maryland pages found' ),
    'not_found_in_trash' => __( 'No Maryland pages found in the Trash' ), 
    'parent_item_colon' => __( 'Parent Page' ),
    'parent_item_colon'  => '',
    'menu_name'          => 'Maryland'
);

$args = array(
    'labels'        => $labels,
    'description'   => 'Holds our Maryland pages specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
    'show_ui'       => true,
    'has_archive'   => true,
    'show_in_nav_menus' => true
);
register_post_type( 'maryland', $args ); 
}
add_action( 'init', 'my_custom_post_maryland' );

4 个答案:

答案 0 :(得分:1)

您仍然需要使用

将其添加到菜单中
function setup_bonsai_theme_admin_menus()
{
    add_menu_page('MyTheme settings', 'My Theme Settings', 'manage_options', 
        'my_theme_settings', 'my_page_settings');

    add_submenu_page('my_theme_settings', 
        'My Page Elements', 'maryland', 'manage_options', 
        'my_questions_settings', 'my_maryland_settings');
}

add_action('admin_menu', 'setup_bonsai_theme_admin_menus');

function my_page_settings()
{
    ?>
    <div class="wrap">
    <h2>There are no theme options at this stage.</h2>
    </div>
    <?php

}

function my_maryland_settings()
{
    ?>
    <div class="wrap">
    <h2>Maryland</h2>
    </div>
    <?php
}

之后,您还需要为单个帖子以及存档帖子等添加新模板文件,以便您可以实际查看它们。 “主题模板”的wordpress codex应该可以帮到你,因为它非常简单。

答案 1 :(得分:1)

这是一个有效的例子。

的functions.php

    <?php
    function setup_bonsai_theme_admin_menus()
    {
        add_menu_page('SCHS Theme settings', 'SCHS Theme Settings', 'manage_options', 
            'SCHS_theme_settings', 'SCHS_page_settings');

        add_submenu_page('SCHS_theme_settings', 
            'SCHS Page Elements', 'Questions', 'manage_options', 
            'SCHS_questions_settings', 'SCHS_questions_setings'); 
        add_submenu_page('SCHS_theme_settings', 
            'SCHS Page Elements', 'Answers', 'manage_options', 
            'SCHS_ansers_settings', 'SCHS_answers_settings'); 

    }

    function SCHS_page_settings()
    {
        ?>
        <div class="wrap">
        <h2>There are no theme options at this stage.</h2>
        </div>
        <?php

    }

    function SCHS_questions_setings()
    {
        ?>
        <div class="wrap">
        <h2>Questions</h2>
        </div>
        <?php
    }
    function SCHS_answers_settings()
    {
        ?>
        <div class="wrap">
        <h2>Answers</h2>
        </div>
        <?php
    }
    function question_custom_post()
    {
        $labels = array(
            'name'               => _x( 'Questions', 'post type general name' ),
            'singular_name'      => _x( 'Questions', 'post type singular name' ),
            'add_new'            => _x( 'Add New', 'book' ),
            'add_new_item'       => __( 'Add New Question' ),
            'edit_item'          => __( 'Edit Question' ),
            'new_item'           => __( 'New Question' ),
            'all_items'          => __( 'All Questions' ),
            'view_item'          => __( 'View Questions' ),
            'search_items'       => __( 'Search Questions' ),
            'not_found'          => __( 'No question found' ),
            'not_found_in_trash' => __( 'No question found in the trash' ), 
            'parent_item_colon'  => '',
            'menu_name'          => 'SCHS Questions'
        );
        $args = array(
            'labels'        => $labels,
            'description'   => 'Holds our questions and question specific data',
            'public'        => true,
            'menu_position' => 5,
            'supports'      => array( 'title', 'editor', 'thumbnail'),
            'has_archive'   => true,
        );
        register_post_type( 'question', $args );
    }

    add_action( 'init', 'question_custom_post' );
    add_action('admin_menu', 'setup_bonsai_theme_admin_menus');
    ?>

single-question.php

    <?php
    /**
     * The template for displaying questions and answers
     *
     * @package WordPress
     * @subpackage SCHS
     * @since Twenty Fifteen 1.0
     */
    get_header();
    ?>
        <div id="primary" class="content-area">
            <main id="main" class="site-main" role="main">
                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <header class="entry-header">
                        <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
                    </header>
                    <div class="entry-content">
                    <?php while ( have_posts() ) : the_post(); ?>

                        <nav id="nav-single">
                            <h3 class="assistive-text"><?php _e( 'Post navigation', 'catchbox' ); ?></h3>
                            <span class="nav-previous"><?php previous_post_link( '%link', __( '<span class="meta-nav">&larr;</span> Previous', 'catchbox' ) ); ?></span>
                            <span class="nav-next"><?php next_post_link( '%link', __( 'Next <span class="meta-nav">&rarr;</span>', 'catchbox' ) ); ?></span>
                        </nav><!-- #nav-single -->
                    <?php endwhile; // end of the loop. ?>
                    </div>
                    <?php 
                    wp_reset_query();
                    the_content();
                    ?>
                </article>
            </main>
        </div>
        </div><!-- #primary -->
    <?php
    get_sidebar();
    get_footer();
    ?>
SCHS-questions.php
<?php
/*
    Template Name: SCHS Questions
    Description: This part is optional, but helpful for describing the Post Template
*/
get_header();
?>
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <header class="entry-header">
                <span id="schs-ask-a-question">Ask a question</span>
                <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
            </header>
            <div class="entry-content">
            <?php
                the_content();
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                $args = array( 'post_type' => 'question','posts_per_page' =>0,'paged' => $paged,'orderby'=>'title','order'=>'asc');
                $myposts = query_posts($args);
                ?>
                <fieldset id="schs-question-list">
                <legend>The Questions</legend>
                <ul>
                <?php
                foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
                <li class="main_question_list_item">
                    <a href="<?php echo get_permalink($post->id);?>">
                    <span  class="question_class"><?php the_title();?></span><br/>
                    <!--
                    <?php $custom = get_post_custom($post->ID);?>
                    <?php $grower = $custom["tree_grower"][0];?>
                    <span class="grower_name"><?php echo $grower;?></span>
                    -->
                    </a>
                </li>
                <?php endforeach; ?>
                </ul>
                </fieldset>
                <?php
            ?>
            </div>
        </article>
        </main>
    </div>
    </div>
    <?php
get_sidebar();
get_footer();
?>

答案 2 :(得分:0)

您还需要在主题中的functions.php中register the men u:

add_action( 'after_setup_theme', 'register_my_menu' );

function register_my_menu() {
   register_nav_menu( 'primary', __( 'Primary Menu', 'theme-slug' ) );
}

答案 3 :(得分:-1)

function pb_custom_post_cases() {
    $labels = array(
        'name'               => _x( 'Recent Cases', 'post type general name' ),
        'singular_name'      => _x( 'Recent Case', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'case' ),
        'add_new_item'       => __( 'Add New Case' ),
        'edit_item'          => __( 'Edit Case' ),
        'new_item'           => __( 'New Case' ),
        'all_items'          => __( 'All Cases' ),
        'view_item'          => __( 'View Cases' ),
        'search_items'       => __( 'Search Cases' ),
        'not_found'          => __( 'No cases found' ),
        'not_found_in_trash' => __( 'No cases found in the Trash' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'Recent Cases'
    );
    $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our recent cases specific data',
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields' ),
        'has_archive'   => true,
        'rewrite' => array('slug' => 'cases', 'with_front'=> false)
    );
    register_post_type( 'cases', $args );   
}
add_action( 'init', 'pb_custom_post_cases' );

/* Taxonomy Recent cases */
function my_taxonomies_cases() {
    $labels = array(
        'name'              => _x( 'Case Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Case Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Case Categories' ),
        'all_items'         => __( 'All Case Categories' ),
        'parent_item'       => __( 'Parent Case Category' ),
        'parent_item_colon' => __( 'Parent Case Category:' ),
        'edit_item'         => __( 'Edit Case Category' ), 
        'update_item'       => __( 'Update Case Category' ),
        'add_new_item'      => __( 'Add New Case Category' ),
        'new_item_name'     => __( 'New Case Category' ),
        'menu_name'         => __( 'Case Categories' ),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
    );
    register_taxonomy( 'cases_category', 'cases', $args );
}
add_action( 'init', 'my_taxonomies_cases', 0 );

/* Custom Messages - Recent Cases */

function pb_cases_updated_messages( $messages ) {
    global $post, $post_ID;
    $messages['cases'] = array(
        0 => '', 
        1 => sprintf( __('Recent Case Updated. <a href="%s">View case</a>'), esc_url( get_permalink($post_ID) ) ),
        2 => __('Custom field updated.'),
        3 => __('Custom field deleted.'),
        4 => __('Case updated.'),
        5 => isset($_GET['revision']) ? sprintf( __('Case restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
        6 => sprintf( __('Recent Case published. <a href="%s">View Case</a>'), esc_url( get_permalink($post_ID) ) ),
        7 => __('Product saved.'),
        8 => sprintf( __('Case submitted. <a target="_blank" href="%s">Preview case</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
        9 => sprintf( __('Case scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview case</a>'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
        10 => sprintf( __('RecentCase draft updated. <a target="_blank" href="%s">Preview case</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    );
    return $messages;
}
add_filter( 'post_updated_messages', 'pb_cases_updated_messages' );