WordPress.org

Ready to get started?Download WordPress

Forums

Template does not work (14 posts)

  1. TheSethness
    Member
    Posted 1 month ago #

    Hey folks,

    Kind of new to wordpress. Looking to merge a template into one page of another theme. So far, no luck.

    I put this in the top of my new template file.

    <?php
    $tpl_body_id = 'pageAltoBlog';
    ?>

    I put the following in 'header.php' of the current theme.

    <?php
    global $tpl_body_id;
    if (!$tpl_body_id) { $tpl_body_id = 'default'; }
    ?>
    </head>
    <body id="<?php echo $tpl_body_id; ?>">

    I also name the new template file 'pageAltoBlog.php' and I put the following in the top of the template.

    <?php
    /**
     *
     * Template Name: pageAltoBlog
     *
     */
    ?>

    When I log into the dashboard and go to the blog page to edit it. pageAltoBlog is there in the dropdown template selection list. I select it. Then go to view the page on the site. The id of the body tag is always default. So, I have no confidence that that the template is actually being used. know that the php function to initialize the id property of the body tag works, because the default body id name will show whatever I put in the default if clause. I have no confidence that the template is being used for the page. Basically I just want the id on the body tag to reflect a unique identifier when I'm on that page so that I can place appropriate unique CSS. Any help greatly appreciated! :)

  2. esmi
    Forum Moderator
    Posted 1 month ago #

    Your main posts page uses the theme's index.php template file by default - not any page template you might try to apply.

  3. TheSethness
    Member
    Posted 1 month ago #

    When you write 'main posts page', do you mean the landing page of the site, or the page I'm actually trying to work on the we have called 'blog'? Also, do you have a recommendation or other suggestion for how to uniquely name that pages id attribute so that I can route the correct CSS to it? Thanks!

  4. TheSethness
    Member
    Posted 1 month ago #

    Also, if the main posts page uses the index.php file 'by default', then why am I able to select a template from the drop down for the page and my pageAltoBlog is there as a selection? Clearly wordpress has built this functionality in order for me to change the template for the page, otherwise I would not have the option. Yes?

  5. stephencottontail
    Member
    Posted 1 month ago #

    In your Dashboard, under Appearance > Customize > Static Front Page, do you have "A static page" selected? There may be a terminology conflict here.

  6. esmi
    Forum Moderator
    Posted 1 month ago #

  7. TheSethness
    Member
    Posted 1 month ago #

    I do have 'a static page' selected under Appearance > Customize > Static Front Page. Yes.

  8. stephencottontail
    Member
    Posted 1 month ago #

    In the same place (Appearance > Customize > Static Front Page), do you have "pageAltoBlog" selected in the dropdown box under "Posts page"?

    Edit: Never mind, I see what's going on now. You could target .blog for custom CSS on your blog posts index when you're using a static front page.

  9. TheSethness
    Member
    Posted 1 month ago #

    @stephencottontail - where would I target .blog for custom CSS on blog posts index? Do you mean link to that CSS file from the header of the index.php page? It actually seems that since the header.php includes elements beyond the </head> tag, it would not be possible to include a separate css file link or any css instructions in the index.php file itself.

  10. stephencottontail
    Member
    Posted 1 month ago #

    No, I meant that you could write your CSS like this:

    a {
        color: blue;
    }
    
    .blog a {
        color: orange;
    }

    In that example, your links would be colored blue, except for on the blog posts index, where your links would be colored orange. You could write other CSS rules the same way. I may have misunderstood what you want, but it sounds like this would do what you want to do.

  11. TheSethness
    Member
    Posted 1 month ago #

    Right. That's kind of what I'm doing right now. All the CSS goes in the style.css file, and I just give the index.php body tag a unique ID, say pageAltoBlog. Then every CSS selector for that page gets #pageAltoBlog before it.

  12. stephencottontail
    Member
    Posted 1 month ago #

    One thing I forgot. For .blog to work, you need to call body_class() on your <body> tag like this:

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

    That way, your custom system would continue to work, as well as gaining access to a lot of custom classes tailored to the type of page being displayed. The only change is that your custom ids would be classes, instead of ids, so you would need to target them like .tpl-id instead of #tpl-id.

  13. TheSethness
    Member
    Posted 1 month ago #

    Hmmm, so I take it you are recommending I go with the original plan from above, more or less and call the variable tpl_body_class? Just curious, why do you prefer to alter the class, rather than the id?

  14. stephencottontail
    Member
    Posted 1 month ago #

    WordPress doesn't have a builtin way to do what you were trying to accomplish, so I suggested using body_class() as a workaround. You could use the same code that you posted in your original post, but instead of using <body id="<?php echo $tpl_body_id; ?>">, you use <body <?php body_class( $tpl_body_id ); ?>> I suggest reading the docs for body_class(); it outputs a lot of HTML classes that can be used to target specific pages.

Reply

You must log in to post.

About this Topic