• Resolved iwriteblogs

    (@iwriteblogs)


    In Reading Settings I can set the front page to display either my latest post, or a static page. Can I set it to go to my first post? Mine should be read in chronological order.

    I thought I was smart by replacing index.php with a index.html file specifying a meta redirect to the permalink of my first blog post, but that resulted in an infinite redirect loop.

    Is there a way to redirect my naked domain to the URL of the first blog post? Or is there some other configuration setting I can adjust other than what I see in the Reading Settings? Any guidance is appreciated. Thank you!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Well…you could use this in your theme’s functions.php file:

    add_action( 'init', 'redirect_front' );
    
    function redirect_front() {
        if ( is_home() ) {
            wp_redirect( 'https://example.com/full-url-for-your-page' );
            exit;
        }
    }

    If you plan on making this permanent, you should add a 301 to that:

    wp_redirect( 'https://example.com/full-url-for-your-page', 301 );

    This will cause an actual redirect (url change) in your visitor’s browsers though. A more complicated and seemless solution would be to use a rewrite rule in your .htaccess. You could also try setting “Blog pages show at most” in Settings > Reading to 1 and set that post as static. Finally, you could put whatever is on the first post on a page, set it as your static front page and then manually add a nav link to the “next” post in the series.

    Thread Starter iwriteblogs

    (@iwriteblogs)

    Thanks, Big Bagel, for the reply.

    I modified the functions.php file in my theme’s folder to use your code with the 301. I added it to the end of the document, just above the ?>. I’ve browsed to the naked domain on two computers on different continents (one which hadn’t gone to my site before so it wouldn’t see a cached copy) but I still see the latest post instead of the URL defined in my wp_redirect line.

    Are there any other files I need to modify to call the redirect_front() function, or does it execute automatically just by my adding it to the functions.php file?

    I modified my Reading Settings to display a max of 1 post per page, but I don’t know how to set that post as static. I understand I could just copy the content to a static page but that wouldn’t work so nicely with one of the widgets I’m using. I suppose worst case scenario I try editing the code for the widget to use a different form URL for the one button… It would be ideal to get the redirect working, though.

    Assuming you added exactly this (changing the url of course) to your functions.php file;

    add_action( 'init', 'redirect_front' );
    
    function redirect_front() {
        if ( is_home() ) {
            wp_redirect( 'https://example.com/full-url-for-your-page', 301 );
            exit;
        }
    }

    it should work fine. The other things I mentioned were separate suggestions and don’t need to be done together. Are you using any cache that might need to be cleared? A link to the site might be helpful. If it refuses to work you can also try using this insead:

    <?php
        if ( is_home() ) {
            header ( 'HTTP/1.1 301 Moved Permanently' );
            header( 'https://example.com/full-url-for-your-page' );
            exit;
        }
    ?>

    This has to be literally the first thing in your theme’s header.php file.

    Thread Starter iwriteblogs

    (@iwriteblogs)

    The code I added to my functions.php matches exactly what you have there, except the URL inside the single quotes is different.

    I placed your last code as literally the first line in my theme’s header.php, right above the “<!DOCTYPE html PUBLIC…” bit. When I refreshed my browser I saw a blank page, and the title bar in my browser showed only my naked URL instead of the blog name. When I removed your code in header.php and refreshed my browser again, I once again saw my latest post, the naked URL stayed in my address bar, and my blog’s name was in the title bar.

    For this reason I don’t believe I have a caching issue, because I could see the changes immediately after making them upon a refresh of my browser. Placing code in my theme’s header.php did affect the way my browser (Google Chrome, latest version) read the site, but unfortunately there was no redirect to my first post. It may be worth noting your first code was still in the functions.php while I was testing the changes in header.php. It seemed that function simply never executed.

    My theme is Easel 2.0.7 which you can apply from a search of the text “easel” within your wp-admin. I don’t know if which theme I’m using has any affect on this, though.

    Thanks for your help anyway. I’ll keep playing with this and if I find something which works I’ll post a reply with the solution.

    A blank page means php exploded somewhere, but the code looks alright. Are you sure a bracket, a semicolon, or the <?php and ?> didn’t get lost along the way?

    Anyways…I realize the goof I made in the first example. is_home() is returning false because it’s hooked in too early. Change:

    add_action( 'init', 'redirect_front' );

    to:

    add_action( 'template_redirect', 'redirect_front' );

    and it should work.

    Edit: Nevermind, ‘template_redirect’ is too late for wp_redirect(). Bah. Let me actually test something.

    Well, finding a way to check for the home page before the headers are sent is a bit of a challenge. Try this in your functions.php:

    add_action( 'init', 'redirect_front' );
    
    function redirect_front() {
        if ( $_SERVER['REQUEST_URI'] == '/' ) {
            wp_redirect( 'https://example.com/full-url-for-your-page', 301 );
            exit;
        }
    }
    Thread Starter iwriteblogs

    (@iwriteblogs)

    That last example did the trick! Thank you so much, Big Bagel! ?? ?? ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Can front page display first blog post?’ is closed to new replies.