deemi-D-nadeem
deemi-D-nadeem

Reputation: 2514

How to make custom post type single page

I make a custom post type foo_work and in theme folder, i make a folder foo, in foo folder i make single-foo_work.php

But it does not work, when i click on custom post link it show me single.php lay out,

not single-foo_work.php

Is there extra code to write in single-foo_work.php file.

In write a link like this:

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

in single-foo_work.php i write this query

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

Here is my custom post type code:

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
    ));
}

any idea what can i do

Upvotes: 0

Views: 87

Answers (1)

Akshay Paghdar
Akshay Paghdar

Reputation: 3629

If you must want to add single-foo_work.php in a foo folder,

Then You can do something like this:-

Add following code in your single.php file:-

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

I hope this will help you...

Upvotes: 1

Related Questions