{"id":94,"date":"2025-03-01T04:57:10","date_gmt":"2025-03-01T04:57:10","guid":{"rendered":"https:\/\/notes.zj1963.com\/?p=94"},"modified":"2025-03-01T04:57:10","modified_gmt":"2025-03-01T04:57:10","slug":"post-type","status":"publish","type":"post","link":"https:\/\/notes.zj1963.com\/?p=94","title":{"rendered":"Post Type"},"content":{"rendered":"\n<p>The <code>single-{post-type}.php<\/code> template in WordPress allows you to create a custom layout for individual posts of a specific custom post type. Here&#8217;s a step-by-step guide on how to add and use it:<\/p>\n\n\n\n<p><strong>1. Create Your Custom Post Type (If You Haven&#8217;t Already):<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If you haven&#8217;t already created your custom post type, you&#8217;ll need to do that first. You can use plugins like Custom Post Type UI or write code in your theme&#8217;s <code>functions.php<\/code> file or a custom plugin.<\/li>\n\n\n\n<li>Example (using <code>functions.php<\/code>):<\/li>\n<\/ul>\n\n\n\n<p>PHP<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function create_book_post_type() {\n    $labels = array(\n        'name'               =&gt; 'Books',\n        'singular_name'      =&gt; 'Book',\n        'menu_name'          =&gt; 'Books',\n        'add_new'            =&gt; 'Add New Book',\n        'add_new_item'       =&gt; 'Add New Book',\n        'edit_item'          =&gt; 'Edit Book',\n        'new_item'           =&gt; 'New Book',\n        'view_item'          =&gt; 'View Book',\n        'search_items'       =&gt; 'Search Books',\n        'not_found'          =&gt; 'No Books found',\n        'not_found_in_trash' =&gt; 'No Books found in Trash',\n    );\n    $args = array(\n        'label'               =&gt; 'Books',\n        'description'         =&gt; 'Books information pages.',\n        'labels'              =&gt; $labels,\n        'supports'            =&gt; array( 'title', 'editor', 'thumbnail', 'custom-fields' ),\n        'hierarchical'        =&gt; false,\n        'public'              =&gt; true,\n        'show_ui'             =&gt; true,\n        'show_in_menu'        =&gt; true,\n        'menu_position'       =&gt; 5,\n        'show_in_admin_bar'   =&gt; true,\n        'show_in_nav_menus'   =&gt; true,\n        'can_export'          =&gt; true,\n        'has_archive'         =&gt; true,\n        'exclude_from_search' =&gt; false,\n        'publicly_queryable'  =&gt; true,\n        'capability_type'     =&gt; 'post',\n        'rewrite'             =&gt; array( 'slug' =&gt; 'book' ),\n    );\n    register_post_type( 'book', $args );\n}\nadd_action( 'init', 'create_book_post_type', 0 );\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In this example, we&#8217;ve created a custom post type called &#8220;book&#8221;.<\/li>\n<\/ul>\n\n\n\n<p><strong>2. Create the <code>single-{post-type}.php<\/code> Template File:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In your theme&#8217;s directory, create a new PHP file named <code>single-book.php<\/code> (replace &#8220;book&#8221; with your custom post type&#8217;s slug).<\/li>\n\n\n\n<li>This file will define the layout for single posts of your &#8220;book&#8221; post type.<\/li>\n<\/ul>\n\n\n\n<p><strong>3. Add Content to Your Template:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open <code>single-book.php<\/code> in a text editor and add the necessary WordPress template tags to display your post&#8217;s content.<\/li>\n\n\n\n<li>Example <code>single-book.php<\/code>:<\/li>\n<\/ul>\n\n\n\n<p>PHP<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/**\n * The template for displaying single book posts.\n *\n * @link https:\/\/developer.wordpress.org\/themes\/basics\/template-hierarchy\/#single-post\n *\n * @package Your_Theme_Name\n *\/\n\nget_header();\n?&gt;\n\n&lt;div id=\"primary\" class=\"content-area\"&gt;\n    &lt;main id=\"main\" class=\"site-main\"&gt;\n\n        &lt;?php\n        while ( have_posts() ) :\n            the_post();\n            ?&gt;\n\n            &lt;article id=\"post-&lt;?php the_ID(); ?&gt;\" &lt;?php post_class(); ?&gt;&gt;\n                &lt;header class=\"entry-header\"&gt;\n                    &lt;?php the_title( '&lt;h1 class=\"entry-title\"&gt;', '&lt;\/h1&gt;' ); ?&gt;\n                &lt;\/header&gt;&lt;div class=\"entry-content\"&gt;\n                    &lt;?php\n                    the_content();\n\n                    \/\/ Display custom fields\n                    if (get_post_meta( get_the_ID(), 'author', true )){\n                        echo \"&lt;p&gt;Author: \".get_post_meta( get_the_ID(), 'author', true ).\"&lt;\/p&gt;\";\n                    }\n                    if (get_post_meta( get_the_ID(), 'isbn', true )){\n                        echo \"&lt;p&gt;ISBN: \".get_post_meta( get_the_ID(), 'isbn', true ).\"&lt;\/p&gt;\";\n                    }\n\n                    wp_link_pages(\n                        array(\n                            'before' =&gt; '&lt;div class=\"page-links\"&gt;' . esc_html__( 'Pages:', 'your-theme-text-domain' ),\n                            'after'  =&gt; '&lt;\/div&gt;',\n                        )\n                    );\n                    ?&gt;\n                &lt;\/div&gt;&lt;footer class=\"entry-footer\"&gt;\n                    &lt;?php\n                    edit_post_link(\n                        sprintf(\n                            wp_kses(\n                                \/* translators: %s: Name of current post. Only visible to screen readers *\/\n                                __( 'Edit &lt;span class=\"screen-reader-text\"&gt;%s&lt;\/span&gt;', 'your-theme-text-domain' ),\n                                array(\n                                    'span' =&gt; array(\n                                        'class' =&gt; array(),\n                                    ),\n                                )\n                            ),\n                            wp_kses_post( get_the_title() )\n                        ),\n                        '&lt;span class=\"edit-link\"&gt;',\n                        '&lt;\/span&gt;'\n                    );\n                    ?&gt;\n                &lt;\/footer&gt;&lt;\/article&gt;&lt;?php\n            \/\/ If comments are open or we have at least one comment, load up the comment template.\n            if ( comments_open() || get_comments_number() ) :\n                comments_template();\n            endif;\n\n        endwhile; \/\/ End of the loop.\n        ?&gt;\n\n    &lt;\/main&gt;&lt;\/div&gt;&lt;?php\nget_sidebar();\nget_footer();\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Replace &#8220;your-theme-text-domain&#8221; with your theme&#8217;s text domain.<\/li>\n\n\n\n<li>Replace &#8220;Author&#8221; and &#8220;ISBN&#8221; with the names of your custom fields.<\/li>\n\n\n\n<li>Customize the HTML and CSS to match your desired layout.<\/li>\n<\/ul>\n\n\n\n<p><strong>4. View Your Custom Post Type Posts:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create or edit posts of your custom post type (&#8220;book&#8221; in this example).<\/li>\n\n\n\n<li>View a single post, and you should see the layout defined in your <code>single-book.php<\/code> template.<\/li>\n<\/ul>\n\n\n\n<p><strong>Template Hierarchy:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>WordPress uses a template hierarchy to determine which template file to use.<\/li>\n\n\n\n<li>For single posts of a custom post type, it will look for:\n<ul class=\"wp-block-list\">\n<li><code>single-{post-type}.php<\/code> (e.g., <code>single-book.php<\/code>)<\/li>\n\n\n\n<li><code>single.php<\/code><\/li>\n\n\n\n<li><code>singular.php<\/code><\/li>\n\n\n\n<li><code>index.php<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>If <code>single-book.php<\/code> exists, WordPress will use it. If not, it will fall back to the next template in the hierarchy.<\/li>\n<\/ul>\n\n\n\n<p><strong>Important Considerations:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Theme Compatibility:<\/strong> Ensure your custom template works well with your theme&#8217;s structure and styling.<\/li>\n\n\n\n<li><strong>Custom Fields:<\/strong> Use custom fields to add specific data to your custom post types and display them in your template.<\/li>\n\n\n\n<li><strong>CSS Styling:<\/strong> Add CSS rules to your theme&#8217;s stylesheet to style the layout of your custom post type posts.<\/li>\n\n\n\n<li><strong>Child Themes:<\/strong> If you&#8217;re modifying a third-party theme, create a child theme to avoid losing your changes when the parent theme is updated.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The single-{post-type}.php template in WordPress allows you to create a custom layout for individual posts of a specific custom post type. Here&#8217;s a step-by-step guide on how to add and use it: 1. Create Your Custom Post Type (If You Haven&#8217;t Already): PHP 2. Create the single-{post-type}.php Template File: 3. Add Content to Your Template: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-94","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/posts\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=94"}],"version-history":[{"count":1,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":95,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/posts\/94\/revisions\/95"}],"wp:attachment":[{"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}