WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Including content on _only_ the blog homepage page? (9 posts)

  1. jonwolfe
    Member
    Posted 7 years ago #

    Hi. On my blog, there's an introductory blurb which I'd like to have _only_ show up on the homepage (page 1), not on /page/2/ or /page/3/, etc.

    In this case, using the conditonal tag is_home() won't work, since it returns true on /page/2/, etc. Any ideas?

    Thanks very much for your help!

  2. re5et
    Member
    Posted 7 years ago #

    This doesn't work because /page/1 is not actually your home. Your home is just '/' or '/index.php' Are you doing something to make /page/1 the first thing a visitor sees?

  3. jonwolfe
    Member
    Posted 7 years ago #

    Hi, thanks for the reply. To answer your question, no, I'm not doing anything special in the organization of my pages.

    The thing is, is_home() returns true whether you go to '/', '/index.php', '/page/1', or '/page/2/'. I wish it only returned true for '/' or '/index.php'.

    You can test this for yourself by putting the following code into your header template:

    	<?php
    		if (is_home())
    		{
    			echo 'is_home() returned true';
    		}
    		else
    		{
    			echo 'is_home() returned false';
    		}
    	?>
    
  4. Chris_K
    Member
    Posted 7 years ago #

    Hmm, a few months ago I would've suggested the Adhesive plugin. However, it hasn't done so well with the last few versions.

    Try searching here though, seems like someone recently posted an adhesive alternative plugin within the last week or so.

  5. jonwolfe
    Member
    Posted 7 years ago #

    Thanks for the suggestion. I looked up Adhesive, but I don't think it has the functionality I'm looking for -- rather than make a certain post always appear on the blog homepage, I need to conditionally include a bit of html that's outside of the posts loop, but _only_ if the current page is the actual blog homepage, not "/page/2", /page/3", etc. If there was a conditional tag such as "is_blog_homepage()" (or an equivalent variable I could access in PHP) that'd be ideal...

    I suppose I could use javascript to look at the document's location and if it's the site root, then document.write() in what I need. However, I'd rather have this logic on the server side.

  6. jonwolfe
    Member
    Posted 7 years ago #

    I figured out a solution, and I'm posting it here in case someone else needs to do something similar with their blog.

    I figured out I could access part of the PHP global variable $_SERVER, specifically $_SERVER['REQUEST_URI'], and compare that to my blog's homepage url. If they are the same, I include my custom header template with the special content I want to appear _only_ on the homepage, else I include the standard header template for my blog theme.

    Why is this better than querying the wordpress function is_home()? Because is_home() returns true even when you page through your older entries. My technique allows you to include custom content on your blog's frontpage and _only_ on the frontpage.

    Here's the code:

    
    <?php
    
    	$request_loc = 'http://www.yourdomainname.com';
    	$request_loc .= $_SERVER['REQUEST_URI'];
    	$blog_loc = get_bloginfo('url');
    	$blog_loc .= '/'; //wordpress seems to not allow trailing slashes on the blog url variable
    
    	if ( $request_loc == $blog_loc )
    	{
    		include (TEMPLATEPATH . '/your_custom_template.php');
    	}
    	else
    	{
    		get_header(); //include the standard header
    	}
    ?>
    
    
  7. jonwolfe
    Member
    Posted 7 years ago #

    I just posted an article that summarizes this issue and my technique for getting conditional content only on my blog homepage:

    http://www.kolossus.com/wordpress/wordpress-tip-homepage-only-content

    If anyone has any feedback about how to do this with just built-in WordPress functionality, that'd be great.

  8. frankp
    Member
    Posted 7 years ago #

    g your article now, as I want to do the same thing.

    Like you, I'd love to do it with WP build in functionality.

    I thought is_paged() would help but it doesn't seem to so I'm confused as to what that does now...

    Anyone know what is_paged does?

  9. frankp
    Member
    Posted 7 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic