• I have a custom post type of ‘brand’, and I’m using the ‘Post name’ permalink structure.

    In a template, how would I check what the current brand being displayed is?

    So for example, the URL is https://www.mydomain.com/brand/apple/ – how can I can I add content to the template which only relates to apple?

Viewing 1 replies (of 1 total)
  • harveyf

    There’s no direct way of doing it in WordPress for now, but here’s something I came up with that will help you out,

    You have custom post type named brand, so its single page template will be named
    single-brand.php
    in that file, in the top, write these lines of code

    global $post;
    if (file_exists(STYLESHEETPATH . '/single-' . $post->post_type . '-' . $post->post_name . '.php')) {
        include STYLESHEETPATH . '/single-' . $post->post_type . '-' . $post->post_name . '.php';
        exit;
    } else if (file_exists(STYLESHEETPATH . '/single-' . $post->post_type . '-' . $post->ID . '.php')) {
        include STYLESHEETPATH . '/single-' . $post->post_type . '-' . $post->ID . '.php';
        exit;
    }

    then it will perfectly load your custom post type’s single file specific for your custom post, which, in this case, will be
    single-brand-apple.php

    It will also take care of single custom post by ID
    single-brand-23.php

    replace 23 with the ID of your post

Viewing 1 replies (of 1 total)
  • The topic ‘detect custom post type page’ is closed to new replies.