WordPress.org

Ready to get started?Download WordPress

Forums

WP Page as static home page (72 posts)

  1. markp
    Member
    Posted 9 years ago #

    Hi all,

    In WP1.5 we have the new option to create Pages not just Posts.

    What I want to do is create a Page which will be the site's home page. When users go to the site I want them to see that page.

    I've tried a few things. I'm already managing to get the site home page to appear in the Page template (as in default Kubrick) and not the Posts template (or any other template) by fiddling with the end of wp-blog-header.php:
    } else if ( is_home() && file_exists(get_page_template()) ) {
    include(get_page_template());
    exit;

    but it still shows the latest posts and not the page. I've tried writing the 'ID' of the page into a variable but I'm getting nowhere there either.

    Any ideas?

    I'm imagining that this type of setting might just become a standard option in the admin console.

    I'm using build 'wordpress-2005-01-17'

    Cheers,

    Mark

  2. Ryan Boren
    WordPress Dev
    Posted 9 years ago #

    There have been requests for a home.php or front.php template that would be loaded instead of index.php for the site home. Would that work for you?

  3. markp
    Member
    Posted 9 years ago #

    Hi,

    I don't really want to redirect a user when they arrive at the domain. I guess I could do a header redirect upon detection that there are no post or get variables.

    So, not really what I'm looking for. It must be possible to do what I want to achieve.

    Where is it set that a call for the index.php with no post or get variables gives the user a page full of posts? If I can change this code then I'm sorted.

    Cheers,

    Mark

  4. Ryan Boren
    WordPress Dev
    Posted 9 years ago #

    It won't be a redirect. home.php will be included from the blog header and no one will be the wiser.

    If the template loaded for the home page has a post loop in it, you will get a page of posts. The default index.php and page.php have a post loop.

  5. markp
    Member
    Posted 9 years ago #

    Yeah. I just tried taking out the post loop (from page.php) and saw that I ended up with just the last post.

    So if home.php is included in the blog header where would it go? Surely I have to also take something out to prevent it getting a load of posts as well?

    I also want the home page editable as a wp Page (by the way!) and to appear in the automatically generated list of Pages.

    It appears that blog-header.php somehow sets up the $wp-query globally used object. It seems to me that if I can fill that object with the data I want (that will determine the page I want to show) then I will have acheived what I want to do. I'm just not experienced with php objects. :(

  6. Ryan Boren
    WordPress Dev
    Posted 9 years ago #

    home.php would go in the theme directory. It wouldn't have a post loop. It would directly contain the content you want to show up on the front page rather than pulling from the DB. But, since you want to be able to use the WP page feature, you'll need a "Make this page the front page" plugin. I'll write one tonight.

  7. markp
    Member
    Posted 9 years ago #

    I'll be interested to see what you come up with.

    I'm still trying to suss out this $wp_query object. It must default to certain values that must be able to be changed.

  8. navid
    Member
    Posted 9 years ago #

    rboren, the home.php idea is phenomenal. I hope to see it implemented soon. Currently, I used a "template" page to achiev the same effect but a home.php or frontpage.php would be the most efficient. Thanks.

  9. Ryan Boren
    WordPress Dev
    Posted 9 years ago #

    Here's the plugin. Hopefully the forum won't munch on it too much. The plugin intercepts the query string before it is passed to wp_query. If we're on the home page we insert a page id into the query string and return it. This altered string is what is passed to wp_query, making it look like a specific page was requested.

    <?php
    /*
    Plugin Name: Page to Front
    Version: 1.0
    Plugin URI: http://wordpress.org/
    Description: Choose a page from the DB to display as the front page of your weblog.
    Author: Ryan Boren
    Author URI:
    */

    // Put the id of the page you want to display here. To find the page id,
    // go to Manage->Pages in the admin UI and look at the ID column.
    $ptf_front_page = 1;

    function ptf_front_page($query_string) {
    global $ptf_front_page;

    $query = new WP_Query();
    $query->parse_query($query_string);

    if ($query->is_home) {
    $query_string = add_query_arg('page_id', $ptf_front_page, $query_string);
    }

    return $query_string;
    }

    add_filter('query_string', 'ptf_front_page');
    ?>

  10. markp
    Member
    Posted 9 years ago #

    DONE IT!!

    I've inserted the following code after line 187 in classes.php
    if ( $this->is_home == true) {
    $this->is_home = false;
    $this->is_page = true;
    $this->query_vars['page_id'] = 2;
    }

    where '2' is the ID of the page (in wp.posts) that you want to show on the home page.
    I don't really know why this works. This is my first attempt at using objects in PHP.

  11. markp
    Member
    Posted 9 years ago #

    Well done rboren! I've just tried your plugin and it works great. Basically it is doing just what my mod to classes does, but in a more elegant manner.
    However, I am getting:
    Warning: Cannot add header information - headers already sent by...page2front.php:29)
    in /home/..../wp-admin/admin.php on line 7
    then lines 8, 9 and 10.
    But *only* in the admin console.
    Note, this is WP 1.5 build 'wordpress-2005-01-17'

  12. Ryan Boren
    WordPress Dev
    Posted 9 years ago #

    Probably some extra whitespace at the end of the plugin.

  13. markp
    Member
    Posted 9 years ago #

    Sorted - it was whitespace that I'd accidentally pasted in.
    Gorgeous!
    Thanks Ryan.
    I've ditched my code and I'm now relying on your new plugin.

  14. Thierry
    Member
    Posted 9 years ago #

    ryan > Since your plugin redirects any request to the WordPress home page to a "static" page, how do you access the real blog index displaying the last posts ?

  15. mike3k
    Member
    Posted 9 years ago #

    Home.php would be perfect. It would be awsome if it ccan be displayed without a database query, so it will work even if my database server is down.

  16. tavarish
    Member
    Posted 9 years ago #

    Used your plugin Ryan ... nice one. But do anyone know if it's possible to remove the "no comment" link and "directory + date" link from just that page?

    Looks cleaner as a static home page when those are gone :-)

  17. Frenchie91
    Member
    Posted 9 years ago #

    The plugin works great with standard and default templates but when I try it with "Devenir-en-gris" I get these message errors :
    Warning: comments_template(/home/userhtml/d/i/a/diapason/3w/wp-content/themes/devenir_en_noir): failed to open stream: Success in /home/userhtml/d/i/a/diapason/3w/wp-includes/comment-functions.php on line 24

    Fatal error: comments_template(): Failed opening required '/home/userhtml/d/i/a/diapason/3w/wp-content/themes/devenir_en_noir' (include_path='./') in /home/userhtml/d/i/a/diapason/3w/wp-includes/comment-functions.php on line 24
    URL= http://www.diapason-france.com
    Help, anyone, please?

  18. Frenchie91
    Member
    Posted 9 years ago #

    OK, I finished up finding the problem. In the index file supplied with the devenir-en-gris zip, there is a strange indication:
    <?php comments_template( is_single() ); // Get wp-comments.php template ?>
    Taking it out fixes the theme :
    <?php comments_template(); // Get wp-comments.php template ?>

  19. Probably because there is no wp-comments.php in WP v1.5. ^_-

  20. trevorturk
    Member
    Posted 9 years ago #

    how would you display the regular blog posts on this front page? i.e. i'd like to have the normal "last ten posts" loop in a small box on this page. would you need a plugin to run php on the page you are showing on the front page?

  21. trevorturk
    Member
    Posted 9 years ago #

    I'm trying to add this to the codex. Please check it out:
    http://codex.wordpress.org/CMS

  22. mxtrader
    Member
    Posted 9 years ago #

    Excuse me, I'm completely novice on this thing of WordPress (but pretty seasoned in PHP). I added the plugin, because I needed for the things I want, it works perfect. Thing is, how do I link to the real blog page from the new home page?

    Sorry for bothering.

  23. mxtrader
    Member
    Posted 9 years ago #

    I have used also the home.php approach, it also works well. But thing still is... once I get to the first page, that could be an "about" or "warning" or whatever you like, how do I link to the blog page? which is the address I should refer to?

    Somebody in other post suggested: place wp inside other directory, like /wp and then write a index.php page that links to whatever.com/wp, it would be easy, but it would render the templates mechanism unusable for this first page. So I really think both, the plugin or the home.php ways are the way to go. But I have no clue on how to get to the blog from there.

    Any help?

  24. If you've implemented Ryan's quick and dirty plugin, can you link to your site in here? I'd like to see how things look. Thanks! :)

  25. lawtai
    Member
    Posted 9 years ago #

    heh mxtrader, i'm not using this plugin myself, but it seems like one solution that you could do would be to create a blog.php template, and then just put the loop in there and create it as a page. I'd think this could work.

  26. trevorturk: I use this on my archives page...

    <h2>Last 15 Posts</h2>
    <ul>
    <?php wp_get_archives('type=postbypost&limit=15'); ?>
    </ul>

    Sorry Ryan, I'm munching on it. Why? I'd like to use it for kyl.org. It's really bad right now. As soon as I can convince whoever is in charge to switch hosts, I'm going to push WordPress as much as I can.

  27. mxtrader
    Member
    Posted 9 years ago #

    I just don't get it.

    In wp-blog-header.php there is a line which says include(get_home_template()) which it goes to:


    function get_home_template() {
    $template = '';

    if ( file_exists(TEMPLATEPATH . "/home.php") )
    $template = TEMPLATEPATH . "/home.php";
    else if ( file_exists(TEMPLATEPATH . "/index.php") )
    $template = TEMPLATEPATH . "/index.php";

    return apply_filters('home_template', $template);
    }

    1. So, if I create a blog.php based on index.php (in the template directory) how do I link to the blog.php page just created? a link to blog.php won't work. And then I do a link from my home.php to blog.php how the thing will know it is a page and not home?

    2. Or the blog.php should be in the root directory and should be a copy of the main index.php? Note: Forget it, I already done this, and doesn't work.

    I'll be trying all options, but if you have something that can help, I'll really apreciate that. Maybe I should write a different wp-blog-header.php ????

  28. mxtrader
    Member
    Posted 9 years ago #

    Damn... this is frustrating.

    1. Turned on the plugin, going to page 2.
    2. In the Page 2 I created a link to blog.php in the root directory
    3. The thing recognizes that as a home page, so it goes to page 2

    It behaves exactly the same using the home.php in the template directory. So how the heck do I link to the blog page? is there any way to inform WP that the page you're seeing is NOT the home page (function is_home() return false, I guess)? I don't care if it is with a URL parameter. Of course using ?p= indicates also that what you see is_home.

    I could tweak wp-blog-header.php to do so... but is there any "supported" way to do it?

  29. mxtrader
    Member
    Posted 9 years ago #

    Ok managed to work. I think. More or less... enough for me, for now:

    1. Deactivate the plugin!!!
    2. In root directory:
      1. Copied wp-blog-header.php to wp-blog-header2.php
      2. Copied index.php to blog.php
      3. In blog.php changed the reference from wp-blog-header.php to wp-blog-header2.php
      4. In wp-blog-header2.php eliminated lines 215 thru 217
    3. In template directory
      1. Created home.php based on index.php (basically eliminating the loop
      2. In home.php put whatever you like and a link at the end to blog.php

    Looks like it is working now. (Or at least enough for me)

  30. lawtai
    Member
    Posted 9 years ago #

    you have a link where we can take a look? and glad you got it to work

Topic Closed

This topic has been closed to new replies.

About this Topic