What I'm trying to do is display the most recent post (a la my theme's "single.php") on my "home" page.
This is complicated by the fact that I have certain plugins (specifically, Scripts n Styles) that are hooking wp_head and wp_enqueue_scripts. In these hooks, they are checking is_singular() to see if my per-post javascript should be injected into the page or not. They then check attributes of the global $post variable to determine which scripts to inject. I want the per-post javascript to be properly injected on the home page if needed.
For reference, the plugin in question's source code is here and the specific function I'm concerned with running properly is called scripts_in_head()
:
static function scripts_in_head() {
// Global
$options = get_option( 'SnS_options' );
if ( ! empty( $options ) && ! empty( $options[ 'scripts_in_head' ] ) ) {
?><script type="text/javascript" id="sns_global_scripts_in_head"><?php
echo $options[ 'scripts_in_head' ];
?></script><?php
}
if ( ! is_singular() ) return;
// Individual
global $post;
$SnS = get_post_meta( $post->ID, '_SnS', true );
$scripts = isset( $SnS['scripts'] ) ? $SnS[ 'scripts' ]: array();
if ( ! empty( $scripts ) && ! empty( $scripts[ 'scripts_in_head' ] ) ) {
?><script type="text/javascript" id="sns_scripts_in_head"><?php
echo $scripts[ 'scripts_in_head' ];
?></script><?php
}
}
I'm pretty new to Wordpress development, so I've probably made a boneheaded mistake here that is making my life overly difficult. Here are the various and sundry ways I've beaten my head against the wall:
From what I understand, I can't just change my home.php
template, since the template isn't even executed until after all these hooks would have already run. Therefore, modifying the query at this point or creating a new one would be pointless.
I could use a HTTP header redirect, although I'm not sure that would work without manually updating the redirect every time the latest post changes.
I could change home.php
to have a javascript redirect, but that seems like it would be a lot of time/effort wasted loading the "dead" home page just to get redirected to another page. If this is the only way, so be it, but it seems like I should be able to do better.
I've tried creating a plugin that hooks earlier than Scripts n Styles, and modifies $wp_query, but this doesn't seem to work properly. Likely, I'm doing it wrong:
function make_single_from_home()
global $wp_query;
if ( $wp_query->is_home() ) {
$last = wp_get_recent_posts( '1');
$last_id = $last['0']['ID'];
$wp_query->set( 'p', $last_id )
$wp_query->set( 'post_count', 1);
$wp_query->set( 'is_home', false);
$wp_query->set( 'is_single', true);
}
}
I've tried attaching this to several events prior to Scripts n Styles' hook, (send_header, template_redirect, etc), and it doesn't seem to help. I don't see the javascript blocks being added to my <head>
tag. (It's possible that this is a valid solution and I or the plugin are just Doing It Wrong)
It's also possible that there's a really easy way to make my homepage a singular page displaying my latest post, and I'm just too new to figure it out.
Is there some way to create a home page that displays just the latest post, while making the $post
variable valid, and is_singular() == true
early enough for my plugin dependencies?