How to Assign Custom Single Page on WordPress Theme

- -

This tutorial is for those developers who are looking a best way to assign custom single page on WordPress while developing a plugin or theme.

In this tutorial, I’m going to share with you two ways of doing this task depending on what are you developing right now.

WordPress development material from their web is specifies on how to achieve it only while developing a theme and not through plugin.

These are the two ways on how assign custom single page on WordPress theme;

Assign it on theme

You can assign it on theme by following WordPress development procedure as guided by them by creating a file in your theme folder as follows:

single-{your custom single page}.php

Explanation:

By default, WordPress single page is assigned by file named single.php, so, if your single page is about book, then you have created a file named single-book.php. If your custom post is product, then you have to create a file and named single-book.php.

By doing that, you instructing WordPress to assign custom page for your new post type.

Assign it on plugin

If you are developing a plugin, then you don’t need above procedure to add file in theme folder, you can create a file anywhere in plugin folder and assign it as your single custom post type.

Many WordPress developers use this approach for adding special features on theme for pricing purposes.

You can assign it via plugin by first creating a folder named templates or anything, then add a file.

single-{your custom post type}.php

Second, you have to hook your plugin with single template filter to assign the file as follows:

add_filter( 'single_template', 'your_custom_single_page_function', 10, 1 );

alter the default single page with callback function hooked above as follows:

if ( ! function_exists( 'your_custom_single_page_function' ) {
    function your_custom_single_page_function($single) {
        global $post

        if ( $post->post_type === 'your custom post type' ) { 
            if ( file_exists( YOUR DIR CONSTANT . 'templates/single-ts_job.php') ) {
                return YOUR DIR CONSTANT . 'templates/single-ts_job.php';
            }
        }
         return $single;
     }
}

Done;

Leave a Comment