February 21, 2007

Is home or is frontpage?

Version 2.1 of Wordpress introduced native support for a static frontpage, which only has been supported by plugins before. When using this option however, the is_home function doesn’t work. This is because is_home is supposed to return true for the “blog” home page, where your most recent posts show up, not the “front” home page. An is_frontpage function is not available yet.

Therefore I whipped together this small plugin, called is_frontpage. Just download the plugin drop the file in your plugin folder, activate the plugin through the admin interface and the is_frontpage function is available to you. Use it just like the is_home function. It returns true if you’re at the frontpage you set, otherwise it returns false.

Syntax

To be complete here the syntax: is_frontpage()

if (is_frontpage()) {
do something
}

6 trackbacks

  1. [...] 有两个方法可以解决这个烦人的问题(感觉WordPress团队在WP2.1版本中经常耍小聪明 XD): 一、使用一个独立的首页,只在这个页面中添加“index”,不使用ishome()。具体方法可以参考E-Space的打造CMS教程。 二、使用isfrontpage这个插件,这样就可以准确地取到首页,那一页。 [...]

  2. [...] to emulate the pre-2.1 function of returning true for the “front” home page, use the is_frontpage plugin (http://www.bos89.nl/1197) or use [...]

  3. [...] to emulate the pre-2.1 function of returning true for the “front” home page, use the is_frontpage plugin (http://www.bos89.nl/1197) or use [...]

  4. [...] thought I’d found a fix with the Is_Frontpage plugin from Bos89 but it caused the stuff to stop showing on the front page as well. It’s like Wordpress [...]

  5. [...] to emulate the pre-2.1 function of returning true for the “front” home page, use the isfrontpage plugin or use ispage(home) where the slug of your desired front page is [...]

  6. [...] bugs and added the option to redirect links to search pages. 2.8 The plugin now plays nice with the is_frontpage plugin. 2.7 Added verification for MSN Webmaster Portal. 2.2 Added verification for Yahoo! Site Explorer. [...]

25 comments

  1. Thank you very much! Is very useful ;)

  2. Spencer says:

    Martijn, I’m using WP 2.2 and this plugin didn’t seem make a difference. I found this page from a discussion of the “currentpageitem” glitch in WP. For the record, I used this somewhat inelegant PHP in my header to fix the problem:

           <a href="/" rel="nofollow">Home</a>
           <a href="/clips" rel="nofollow">Clips</a>
    
    
    
          <a>"&gt;RSS</a>
    

    I have a static home page and a posts page defined in WordPress and this code makes everything highlight properly.

    In “exclude=141,158″ 141 and 158 are my the page IDs of my home page and index pages respectively (be sure to write the lower number first). In “a href=”/clips”>Clips” clips and Clips are the page slug and page name of my index page.

  3. Spencer says:

    Meh! The HTML was stripped.

  4. SeanC says:

    I was having trouble as well at first. I think part of the problem is that Martijn has a couple posts on the same thing (as CMS, fixed page as the homepage, etc). I finally got it to work like this (until then I was receiving the is_frontpage() error. This is on WP 2.2 (March 07).

    Default install of WP2.2 Make a page called ‘Home’ and a page, ‘Blog’ Install the plugin from above and activate it Use the Options> Reading > Frontpage feature in WP to set the home and blogs page.

    This seems to work for me ok. It didn’t for awhile. Something was breaking (and I was using another static homepage plugin mentioned on another Martijn post) and doing it in that order seemed to work. I was able to enable Martijn’s CMS v2 theme as well, which has some cool dropdown features and things in it.

    thanks Martijn!

    :)

    sean

  5. SeanC says:

    Martijn, Oddest thing I just noticed… I put some text in the footer, through WP on your CMS2 theme and IE7 (always… I know) shows the page fine if you link to it, type in the URL, etc… but if hit reload it throughs an error and nothing but the bg is displayed?? Just doouble checked and it doesn’t seem to be related to having footer text or not…

    http://intranet.iel.spokane.edu if you want to take a look. I am replacing the current (private) static version of the site, but I can leave the WP project up for a few days if you want to look at it.

  6. Spencer: the function only returns true if you are at the frontpage you set on the Options> Reading page. It is not needed for highlighting the current page, the wp_list_pages function will do that for you. Unfortunately except for the page you set as weblog page. But that can’t be solved with y plugin.

    SeanC: indeed the posts were a bit of a clutter. I reorganised the lot and only kept what’s needed for the current Wordpress version. With this version and my CMS theme you don’t need any plugin to use Wordpress as CMS. The is_frontpage plugin is only needed if you would like to display (or hide) something at the frontpage only.

    I did take a look at your site. No problem with Safari of IE6. With IE7 the problem showed up, IE tell me there is an error on the page. I checked the latest CMS theme and this works fine. It must be something you added or the use of some plugin. Try downloading the latest version of the theme and disabling all plugins.

  7. SeanC says:

    M,

    I have a couple of plugins going, so it might be one of those. I will have to do a few tests to see what is going on.

    About the is_frontpage…. I get that error if I don’t enable that plugin and are using a basic install with your CMS enabled. The kubrick and that other theme work fine using WP to enable ‘home’ and ‘blog’… sounds strange, but it’s true. I clean installed a couple times in a row to make sure. So…

    is_frontpage + WP blog/home settings + your CMS theme = works fine.

    WP blog/home setings + default WP themes = works fine.

    WP blog/home settings + your CMS theme – is_frontpage = frontpage (cms/page.php line 10) error.

    If that helps any… sorry I am not more help, I know some CSS but not much PHP, so I just tweek code here and there to get it to work. :)

  8. SeanC says:

    Also… I locked down the site using some LDAP info, but I have an account you can use if you would like to look at the site again. Just email me if you are interested! Thanks -S

  9. SeanC says:

    I just seem to post, post, post… :)

    on side bar.php in the theme it reads (line 10): … just to let you know.

  10. Pete says:

    Cannot download the file? It just takes me to your post list. I am using IE7 on Windows Vista Ultimate?

  11. Beth Mandel says:

    Thanks so much Martijn:

    It worked perfectly!

    My only problem is that the page originaly published next to Home, as it wasn’t named home.

    When I selected the blank navigation option in Options Reading, after the fact, the navigation didn’t corrrect itself. I now hae an extra space in the navigation.

    Any ideas how to correct this?

    Any direction on this will be greatly appreciated.

    Thanks again–

    Beth

  12. Marci says:

    Wow, thanks for this plugin. I can’t believe WP didn’t account for the is_home function which so many people have implemented for the home page. After all, didn’t they include the feature because so many people were using the plugin??? :)

    Anyway, I have run into a slight problem…not sure if it is the plugin or my templates. My theme is custom, and I just upgraded from 2.0 to 2.1.

    The plugin does great on the home page itself, but I am having trouble with the sidebar.

    I have changed all ishome in the sidebar to isfrontpage. But still the conditional sections of the sidebar (“if is_frontpage”) are not showing up. I get the parts of the sidebar that are constant, not the conditional ones.

    I also can’t seem to use “if isfrontpage getsidebar” (leaving out the code on purpose :)) — but I can use a simple “php get_sidebar”. This works alright for me, since this template is only used for the home page….er….front page. But ideally I would like it as a conditional for future expansion.

    I would be glad to answer questions if I have not provided enough information here.

    Thanks!

  13. mike says:

    sweet – 2 seconds to impliment and it worked perfect! thanks…

  14. Hi Martijn, I just added compatibility with your plugin to my robots meta plugin. Thanks for writing your plugin!

  15. Bram says:

    Doesn’t seem to be working with WP 2.3… I don’t know, I used it on my site to try and show a “latest project” on the homepage, but unless it’s got smth to do with my theme, it’s not working for me.

  16. ovidiu says:

    hello, how would I use this with a home.php ? I do use a home.php template file and would like to only show 5 posts on this page. I tried the Custom Query String 2.9 plugin and manually putting:

    before my loop, but still I see 10 posts on my frontpage …

    just in case my php will be stripped out of this comment, here is another try: queryposts(‘cat=1&postsper_page=5′)

  17. ovidiu says:

    seems I was using old syntax? solved it using: query_posts(‘cat=1&showposts=5′)

  18. ovidiu says:

    how does this work with me using home.php ?

    I have problems getting my homepage/frontapge whatever showing only posts from a certain category :-( can someone help?

  19. Home.php is used for the home page only, also see here for more explanation. For this page is_home is always true and is_frontpage never. To show only posts from a category on a page has nothing to do with is_frontpage. Checkout query_posts in the codex for that.

  20. Vova says:

    Hello, the plugin works perfectly for me in general, but in some particular cases it doesn’t. I need to check whether the page which is being loaded is frontpage before any response headers are sent. So I wrote smth like this:

    addaction(‘init’, ‘myinit’); function myinit() { header(“frontpage: !”.isfrontpage().”!”); }

    But it always returns false… Do you know if there is any way to do this?

    Thanks!

  21. What you try to do is impossible with the plugin. It requires the post ID which is not there before any headers are sent.

  22. Kimochi says:

    sorry… i have problem this true is_frontpage() or this or please help/

  23. I would need a more precise description of the problem to be able to help you.

  24. marujo says:

    hi. i active this plugin.in my case (www.sorayagervasio.com.br/site) i setup in options/reader to make the page “em foco” to be the initial page. but the default page “home” is showing the “em foco” content too. i need to “home” show the posts… what i must do to setup your plugin to do it? what file i must change? and what tag i must edit? im not a programmer… thanks a lot. sorry for my english (im from brazil)

  25. Anthony says:

    The download link doesn’t work… and can you add the suscribe to comment plugin to your blog ? It’s more easy to follow the thread…

Post a comment

Your e-mail is never shared, required fields marked with *

Subscribe to this posts comments RSS feed