• I’ve created a custom template so that specific Pages will have a different look than the others (let’s call it “blue-page”). On that blue-page I have placed category post short code so a list of specific posts related to blue-page’s activities will be in one place. Now I would like to have those specific posts (blue-page post) display within the the blue-page template instead of the single-post default.

    Thanks in advance!

Viewing 15 replies - 1 through 15 (of 18 total)
  • This might work for you.

    • Rename single.php to single-default.php.
    • Create your single-blue-page.php template.
    • Create a new single.php with code like the following.
    $post = $wp_query->post;
    if ( in_category(array(20) )) {
      include(STYLESHEETPATH . '/single-blue-page.php'); }
    else {
      include(STYLESHEETPATH . '/single-default.php');
    }

    Change the ’20’ to your blue-page catgory id.

    Thread Starter laptophobo

    (@laptophobo)

    vtxyzzy,
    I’ve followed your instructions and though I do get my post on it’s own page, the content on “single-blue-page.php” essentially doubles. This must mean that I didn’t create the new “single.php” file the right way. Could you please look over my code to see what I’ve done wrong?:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Thank you.

    Single.php should include only the lines I showed.

    Thread Starter laptophobo

    (@laptophobo)

    You are genius. Thank you. Do you have a “donate” page?

    Thanks for the offer of a donation, but I do not personally accept donations.

    If you wish, make a contribution to your favorite charity, or see my step-dauthter’s page.

    Thread Starter laptophobo

    (@laptophobo)

    I’ve sent a donation to Emily Cross’s fund. Thank you for steering me that way. (You’re a good step-mom.)

    Best,
    Richard

    Many thanks!

    Thread Starter laptophobo

    (@laptophobo)

    Hi. One thing I still need to do is style the post (like change the background) for all “blue posts”. Currently, it’s taking the style of “single”. Here’s what the Page Source comes up with:

    <body class=”single single-post postid-1270 single-format-standard logged-in admin-bar”>

    Do you know how I could style the Blue Posts?

    You could add a new class to the body tag in single-blue-post.php and then put the styles for the new class in styles.php.

    I have not tested this, but it should be close.

    First, add this to functions.php:

    // Add specific CSS class by filter
    add_filter('body_class','my_class_names');
    function my_class_names($classes) {
       global $my_class_name;
       if ($my_class_name) {
          // add 'class-name' to the $classes array
          $classes[] = $my_class_name;
       }
       // return the $classes array
       return $classes;
    }

    then, in single-blue-post.php, change this:

    get_header();

    to this:

    // Activate body class filter
    global $my_class_name;
    $my_class_name = 'blue-post';
    
    get_header();
    
    // Turn off filter
    $my_class_name = '';

    Then add the styles using ‘body.blue-post’ as part of the selector.

    Thread Starter laptophobo

    (@laptophobo)

    Thanks for getting back. But, I wasn’t able to get that to work.
    One odd thing is that even though the post is viewing on the single-blue-page.php, the body tag in Page Source view does not show that.

    Your theme might not support the body class filter. Check to see if your header.php has a line for the body tag similar to this:

    <body <?php if(function_exists('body_class')) body_class(); ?>>
    Thread Starter laptophobo

    (@laptophobo)

    I have this in there:

    <body <?php body_class(); ?>>

    And I am able to style such pages as

    body.single, body.blog,

    etc.

    Well, your theme supports the body_class() function, so I don’t know why the filter is not working.

    Please put the code for functions.php and single-blue-post.php into separate pastebins and post links to them here.

    Thread Starter laptophobo

    (@laptophobo)

    Actually, I don’t have a single-blue-post.php page, but I do have a single-blue-page.php one.

    Sorry, I was thinking Posts. Use the single-blue-page.php.

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘How to get post to display in a custom template’ is closed to new replies.