• Hello everyone

    I have to prepare different custom single post pages to apply within the same website becaue my client probably wants me to apply different templates to two different types of service detail pages.

    I understand single-post-[slug].php or single-[post-type].php can let you separately apply single post templates. The problem is that the client doesn’t want to enter a page slug or select a custom post type for each post.

    Consequently, I spent hours lookng for ways to achieve the separate application of templates and Method 1 introduced here seems most promising.

    Method 1 presented above indeed returns some kind of an array containing the category name using this code:

    $category = $wp_query->get_queried_object();

    However, I cannot work out how to make WordPress get only the category name, property-detail, from the array:

    4312018-03-14 10:28:182018-03-14 01:28:18property-detailpublishclosedclosedproperty-listing2018-03-20 13:20:312018-03-20 04:20:310https://localhost:8080/aozora-estate/?page_id=430page0raw

    The next code introduced in Method 1 also fails to get the category name:

    $cat_name = $category->name;

    So I would like to know how you can only get the category name using get_queried_object() or if there is any better tried and tested way to achieve the same goal. Please someone help me.

    Thank you in advance,

    Ead

Viewing 7 replies - 1 through 7 (of 7 total)
  • I understand single-post-[slug].php or single-[post-type].php can let you separately apply single post templates. The problem is that the client doesn’t want to enter a page slug or select a custom post type for each post.

    You need something specify that this post belongs to one type and other to other type. maybe we can use for it post meta data.
    Something like this create for each post metadata that will default set in ‘type one’ and when user wants create second type he set checkbox or radiobutton and will get second type.

    In general here many variants, it doesn’t matter what realization you’ll chose.

    • This reply was modified 6 years, 8 months ago by neo332.
    Moderator bcworkz

    (@bcworkz)

    When the query is for a single post, you will not normally be able to get any categories from the query object. To get categories in that situation, determine the queried object’s ID. This will be a post ID for single post queries. Use wp_get_post_categories() to get the post’s assigned category terms.

    Have you considered using Post Formats to achieve your template goals? It’s really just another taxonomy, but it’s specifically setup to allow themes to style posts differently based on the assigned format. The display could be solely CSS, based on the class output by post_class(). Or the theme template can conditionally output different content based on the return value of get_post_format(). In a sense, all the different single post templates you desire would all be contained in a single template. However, you can load different template parts based on the return value from get_post_format(), so in fact there can still be separate files.

    The best way to implement custom formats is to call add_theme_support('post-formats', [/* array list of desired formats */]) from functions.php of a child theme. This overrides any default formats of the parent, so include any formats you wish to retain. The “standard” format is always available, don’t include standard in the list.

    Thread Starter eadwig

    (@eadwig)

    Hello

    Thanks for the replies.

    I read the descriptions of wp_get_post_categories() and Post Formats.
    On the subject of using wp_get_post_categories(), could this be used to identify a group of single post pages? When I embed it in a single post template with echo, the page shows 467, which is different from its post ID, 632…

    I understood what Post Formats are, but after reading through this page (https://codex.www.ads-software.com/Post_Formats)
    I am confused why the page suddenly refers to Adding Post Type Support halfway down and cannot ready them.

    Please provide me with some more detail on these issues.

    Thank you in advance,

    Ead

    Moderator bcworkz

    (@bcworkz)

    I’m not sure what you actually were echoing to get 467, I would expect it to be the category term ID. The function can be configured to return only the term IDs, or entire term objects. If you were to only assign a particular term to a certain group of posts, then yes, that can be used to identify the group. You could conditionally load certain template parts on the single post template when a particular category is assigned, and/or output a particular element class to uniquely style the posts having that term assigned. Since both categories and formats are taxonomies, they can be both used in the same way.

    The adding post type support section is significant if you have a custom post type that you want to use post formats with. By default formats only apply to post post_types. To have them work with your CPT, you need to explicitly tell WP to relate the post format taxonomy to your CPT. The same requirement applies to any taxonomy. You can also relate taxonomies to CPTs when the CPT is first registered.

    Most of the post format docs page is a general discussion about post formats that apply to any usage of post formats. The page isn’t organized that well, the section on CPTs should be moved further down since it only applies to a less common usage. All common usage verbiage should have occurred first.

    Thread Starter eadwig

    (@eadwig)

    I enabled the custom post type by embedded the following code,
    but my custom post template named single-office-magazines.php does not apply to individual custom post pages:

    function create_posttype(){
        register_post_type('office-magazines',
          array(
            'labels' => array(
              'name' => __('office-magazines'),
              'singular_name' => __('office-magazine'),
            ),
            'rewrite'=>array('slug' => 'office-magazines'),
            'show_ui' => true,
            'show_in_menu' => true,
            'supports' => array( 'title', 'editor', 'thumbnail' )
            )
          );
        }
        flush_rewrite_rules();
        add_action('init', 'create_posttype');

    Making a custom post type menu successfully appear in the admin panel looks promising in terms of separating the custom posts from the default posts. But all the same, I have to get the custom post template to be applied to custom posts so they adopt a different design from default posts.

    If someone spots any potential error, please let me know.

    Troubleshooting such as this did not help: https://stackoverflow.com/questions/16403405/wordpress-single-post-type-template-not-displaying-post

    • This reply was modified 6 years, 8 months ago by bcworkz. Reason: code fixed, otherwise syntax errors occur with use
    Moderator bcworkz

    (@bcworkz)

    I tried your code, but had trouble with it. I added 'public' => true, to the main arguments array; and added 'with_front' => true, to the ‘rewrite’ array. With those additions, it worked as expected and my single-office-magazines.php template was utilized in viewing the new CPT that I added.

    You will not need flush_rewrite_rules() on every request, it’s quite expensive computationally to call it. You should comment it out after it’s used once. FYI, you can achieve the same result by visiting the permalinks settings screen.

    If you still have trouble with your template, it is located in your theme’s folder, right? Also be sure the file has proper permissions (usually 644). Check your error logs for any clues.

    If none of the above helps, temporarily copy your CPT code into the bottom of twentyseventeen theme’s functions.php. Also copy your CPT single template to the twentyseventeen folder. Install and activate the health-check plugin and enter the troubleshooting mode. You’ll find your CPT single template is used. Restore your normal theme, then your plugins, each one by one, using the troubleshooting menu bar item. When you template is no longer used, the last activated module is causing the trouble.

    BTW, when you post code in these forums, please demarcate with backticks or use the code button. Failure to do so makes it difficult for others to test your code or provide workable code corrections.

    Thread Starter eadwig

    (@eadwig)

    Thank you so much!

    Adding ‘public’ => true worked like a charm and no need for flush_rewrite_rules() or a plugin!

    To sum up, I only needed the code as shown below in functions.php:

    function create_posttype(){
    register_post_type(‘office-magazines’,
    array(
    ‘labels’ => array(
    ‘name’ => __(‘office-magazines’),
    ‘singular_name’ => __(‘office-magazine’),
    ),
    ‘rewrite’=>array(‘slug’ => ‘office-magazines’),
    ‘public’ => true,
    ‘show_ui’ => true,
    ‘show_in_menu’ => true,
    ‘supports’ => array( ‘title’, ‘editor’, ‘thumbnail’ )
    )
    );
    }
    add_action(‘init’, ‘create_posttype’);

    Hope this helps other developers,

    Ead

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Want to apply different custom single post page templates in a single site’ is closed to new replies.