Forums

How to get post to display in a custom template (19 posts)

  1. laptophobo
    Member
    Posted 5 months ago #

    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!

  2. vtxyzzy
    Member
    Posted 5 months ago #

    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.

  3. laptophobo
    Member
    Posted 5 months ago #

    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.

  4. vtxyzzy
    Member
    Posted 5 months ago #

    Single.php should include only the lines I showed.

  5. laptophobo
    Member
    Posted 5 months ago #

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

  6. vtxyzzy
    Member
    Posted 5 months ago #

    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.

  7. laptophobo
    Member
    Posted 5 months ago #

    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

  8. vtxyzzy
    Member
    Posted 5 months ago #

    Many thanks!

  9. laptophobo
    Member
    Posted 4 months ago #

    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?

  10. vtxyzzy
    Member
    Posted 4 months ago #

    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.

  11. laptophobo
    Member
    Posted 4 months ago #

    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.

  12. vtxyzzy
    Member
    Posted 4 months ago #

    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(); ?>>
  13. laptophobo
    Member
    Posted 4 months ago #

    I have this in there:

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

    And I am able to style such pages as

    body.single, body.blog,

    etc.

  14. vtxyzzy
    Member
    Posted 4 months ago #

    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.

  15. laptophobo
    Member
    Posted 4 months ago #

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

  16. vtxyzzy
    Member
    Posted 4 months ago #

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

  17. laptophobo
    Member
    Posted 4 months ago #

    I've never used the pastebin thing before. Hope this is what you want.

    This is the functions.php

    <script src="http://pastebin.com/embed_js.php?i=egeQYpxe"></script>

    This is the single-voice-page.php (I've changed "blue" to "voice"):

    <script src="http://pastebin.com/embed_js.php?i=PeWWwWvi"></script>
  18. vtxyzzy
    Member
    Posted 4 months ago #

    You posted code without the changes! I need to see the changes IN the code.

  19. laptophobo
    Member
    Posted 4 months ago #

    I tried your instructions (per above) and now it works! I must have missed something the first time.

    Thank you for helping me -- again. (I'll be visiting your Emily donor page next.)

Reply

You must log in to post.

About this Topic