Danish Iftikhar
Danish Iftikhar

Reputation: 41

Unable to get custom post type name with get_post_type()

Unable to get my custom post type name to load single-jobs.php page can some please help me where i'm doing something wrong in code or is there any permalink issue.

Code to load single page template for jobs post type

function wwp_job_portal_single_page($original_template){
        //check post type to job portal single page
        $type=get_post_types(); 
        if(get_query_var( 'post_type' ) !== 'jobs'){

            return ;

        }
        elseif(is_single('jobs')){
        //check if file exit of single job page template
            if(file_exists(file_exists( get_stylesheet_directory(). '/single-jobs.php' ))){

                return get_stylesheet_directory() . '/single-jobs.php';

            }
            else{

                return plugin_dir_path( __FILE__ ) . 'templates/single-jobs.php';
            }

        }
        else{
            echo "<h1>jobs page loaded</h1>";
            return plugin_dir_path( __FILE__ ) . 'templates/single-jobs.php';

        }
        return $original_template;
    }
     add_action('template_include','wwp_job_portal_single_page');

Custom post type registration code

function create_jobs_post_type(){

    // set up labels
    $labels = array(
        'name' => 'Job',
        'singular_name' => 'Job',
        'add_new' => 'Post New Job',
        'add_new_item' => 'Post New Job',
        'edit_item' => 'Edit Job',
        'new_item' => 'New Job',
        'all_items' => 'All jobs',
        'view_item' => 'View jobs',
        'search_items' => 'Search Job',
        'not_found' =>  'No Job Found',
        'not_found_in_trash' => 'No Job found in Trash', 
        'parent_item_colon' => '',
        'menu_name' => 'Jobs',
    );
    //register post type
    register_post_type( 'jobs', array(
        'labels' => $labels,
        'has_archive' => true,
        'public' => true,
        'supports' => array('title','thumbnail'),
        'publicly_queryable'  => true,
        'exclude_from_search' => true,
        'show_in_nav_menus'   => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 10,
        'menu_icon'           => 'dashicons-id-alt',
        'can_export'          => true,
        'delete_with_user'    => false,
        'hierarchical'        => false,
        'has_archive'         => true,
        'query_var'           => true,
        'capability_type'     => 'post',
        'map_meta_cap'        => true,
        // 'capabilities' => array(),

        'rewrite'             => array( 
            'slug'          => 'jobs',
            'with_front'    => true,
            'pages'         => true,
            'feeds'         => false,

        ),
        )
    );
    $taxonomy_args = array( 
      'labels'                    => array( 'name' => 'Job Category' ),
      'show_ui'                   => true,
      'hierarchical'              => true,
      'rewrite' => array( 'slug'  => 'jobs' )
    );
    register_taxonomy(
        'jobs_category',
        'jobs',
        $taxonomy_args
    );
}

//hook for theme setup
add_action('init','create_jobs_post_type');

Upvotes: 2

Views: 3273

Answers (2)

Benoti
Benoti

Reputation: 2210

The function get_post_type() can be use outside the loop but then require the optional argument ($post_id) to be set.

In your code, you'll need to set the post ID as argument.

function wwp_job_portal_single_page($original_template){
    global $post;
    //check post type to job portal single page
    $type=get_post_types($post->ID); 
  .....

Then your function will be able to find the related post ID you are looking for.

Upvotes: 1

Tristup
Tristup

Reputation: 3673

Can you please try the same function with the post ID, just like :

get_post_type( get_the_ID() )

Hope that will do

Upvotes: 0

Related Questions