如何制作自定义帖子类型单页

时间:2014-06-25 11:35:43

标签: php wordpress wordpress-plugin

我制作了自定义帖子类型foo_work,在主题文件夹中,我创建了一个文件夹foo,在foo文件夹中我制作single-foo_work.php

但它不起作用,当我点击自定义帖子链接时,它会显示single.php布局,

不是single-foo_work.php

是否有额外的代码可以写入single-foo_work.php文件。

写一个这样的链接:

<a href="<?php the_permalink(); ?>">
    <?php the_title(); ?>
</a>

single-foo_work.php我写这个查询

<?php
if(have_posts()) :
    while(have_posts()) :
        the_post();
?>
Content
<?php
    endwhile;
    else :
        echo "No Post";
    endif;
 ?>

这是我的自定义帖子类型代码:

add_action('init', 'foo_articles');
function foo_articles() {

    // Get Knowledge Base slug from settings
    $foo_slug_option = 'foo_work';
    $foo_slug_option = get_option('foo_plugin_slug');

    $labels = array(
        'name' => _x('work', 'post type general name'),
        'singular_name' => _x('work', 'post type singular name'),
        'add_new' => _x('Add New work', 'work'),
        'add_new_item' => __('Add New work'),
        'edit_item' => __('Edit work'),
        'new_item' => __('New work'),
        'view_item' => __('View work'),
        'search_items' => __('Search work'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );

    $foo_rewrite = array(
        'slug'                => $foo_slug_option,
        'with_front'          => false,
        'pages'               => true,
        'feeds'               => true,
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'menu_icon' => WP_WORK.'images/icon-foo.png',
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 3,
        'supports' => array('title','editor','thumbnail','comments'),
        'rewrite' => $foo_rewrite,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => true,
        'publicly_queryable'  => true,
        'capability_type'     => 'post'
    );

    register_post_type( 'foo_work' , $args );
}

add_action( 'init', 'foo_taxonomies', 0 );
// Article taxonamy
function foo_taxonomies() {

    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name' => _x( 'work Category', 'taxonomy general name' ),
        'singular_name' => _x( 'work Category', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search work Category' ),
        'all_items' => __( 'All work Categories' ),
        'parent_item' => __( 'Parent work Category' ),
        'parent_item_colon' => __( 'Parent work Category:' ),
        'edit_item' => __( 'Edit work Category' ),
        'update_item' => __( 'Update work Category' ),
        'add_new_item' => __( 'Add New work Category' ),
        'new_item_name' => __( 'New work Category Name' ),
    );  

    register_taxonomy( 'foo_cat', array( 'foo_work' ), array(
        'hierarchical' => true,
        "label" => 'Category',
        "singular_label" => "work Category",
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => false
    ));
}

任何想法我能做什么

1 个答案:

答案 0 :(得分:1)

如果你想在foo文件夹中添加single-foo_work.php,

然后你可以这样做: -

single.php文件中添加以下代码: -

<?php 
global $post;
if($post->post_type == 'foo_work')
{
    get_template_part( 'foo/single', 'foo_work' );
}
else
{
    //Your Original Single.php Content.
}

我希望这会对你有帮助......