I thought I would add a little extra about what I did, for anyone interested in doing the same thing.
Goals:
1) On a site where the blog is the home page, I wanted to have a intro “Welcome” section before the blog posts which could be surrounded with divs, etc. so that I could style it with CSS.
2) Also, I wanted this intro to appear ONLY on the main page of the site, and if users clicked the “blog” link on the pages nav menu, they would not see the intro (because if they are on the site, they have already seen it). To restate this: even though the home page IS the blog page, I only wanted them to see the intro when first arriving at the site, but not when clicking on “blog” (or for that matter on “previous entries” at the bottom of the home page).
This is what I did:
1) installed get-a-post plugin (see Moshu’s post above for link)
2) put this code before the loop on my home.php template:
<?php get_a_post(16); //this uses the get_a_post plugin to pull up a page with id=16, which contains my Welcome entry ?>
<!-- this area pulls up the title and content of entry, and is surrounded with divs that allow me to style the entry with background images etc. via the stylesheet -->
<div id="welcome">
<div>
<h3><span><?php the_title(); ?></span></h3>
<?php the_content(); ?>
</div>
<div class="welcome_footer"></div>
</div>
<!-- Normal WordPress loop goes under here -->
3) Problem: the intro shows up on every page of the blog. I only want it on the first page, and also, I don’t want it to show when someone clicks the “blog” link on the pages menu (which goes to www.mysite.com/blog/
).
Solution: Make sure that only page the intro shows up on is the naked URL of the WordPress site (e.g. www.mysite.com
or www.mysite.com/wordpress/
but NOT www.mysite.com/blog/
or www.mysite.com/wordpress/blog/
etc.).
Found the code for this on a post on Kolossus Interactive, which uses $_SERVER[’REQUEST_URI’]
to compare the location of the site to the url pulled up by get_bloginfo('url')
.
4) So now the whole thing looks like this (I’ve removed some of the annotation from the code above for brevity):
<?php
$request_loc = 'https://www.mysite.com';
$request_loc .= $_SERVER['REQUEST_URI']; //request_uri is a relative url
$blog_loc = get_bloginfo('url');
$blog_loc .= '/'; //wordpress' blog url variable has no trailing slash, so add it
?>
<?php if ( $request_loc == $blog_loc ): ?>
<?php get_a_post(16); ?>
<div id="welcome">
<div>
<h3><span><?php the_title(); ?></span></h3>
<?php the_content(); ?>
</div>
<div class="welcome_footer"></div>
</div>
<?php endif; ?>
<!-- Normal WordPress loop goes under here -->
It works – the intro will show up ONLY if the user is on the home page, but not on the blog’s paged “previous entries” nor if the user clicks “blog” from the pages menu.
There may be simpler ways of doing this, but this worked for me.