Here's what I need to do. Insert a short Javascript in the footer using wp_footer. No problem there for single posts, I have this.

add_action( 'wp_footer', 'prefix_wpfooter' );
function prefix_wpfooter() {
    global $post;

    if( is_singular() ) {
        $hide_page = ( empty( get_post_meta( $post->ID, '_prefix_hide_page', true ) ) ) ? '' : get_post_meta( $post->ID, '_prefix_hide_page', true ) ;

    if( $hide_page == '1' ) {
        <!-- Output JS here -->

However, this won't fly for front_page or category archives. I'm having a brain fart and thought I'd seen a solution to this before. Can anybody help start me in the right direction or point me to a solution because I don't think I'm using the right search terms and coming up empty.

Many, many thanks!

  • What exactly do you mean by "won't fly"? The JS isn't loading or is there some other problem? – jdm2112 May 7 '14 at 20:25
  • The only circumstance where the code in your second if would execute is when the value associated with the _prefix_hide_page key is 1. Is that what you are trying to do? – s_ha_dum May 7 '14 at 21:04
  • The JS won't load at all in archive pages and front pages. e.g looking at my code, the <script></script> doesn't appear at all. It also doesn't appear if I remove the is_singular condition on these archive pages. @s_ha_dum yes. I need it to only execute if the meta value for _prefix_hide_page = 1 – tbm May 7 '14 at 21:07

Well guys, I ended up doing something like this which I am certain isn't the best way. Seems like a lot of work just to get to the nugget. Could've sworn I've seen a simpler way but it's escaped me all day. This is ugly and probably highly inefficient but it works. Please do let me know or point me to a better solution if you know one.

global $posts, $post;
if( is_front_page() || is_archive() ) {     
$hide_pages = array();
        foreach ($posts as $post) {
            $hide_pages[] = ( empty( get_post_meta( $post->ID, '_prefix_hide_page', true ) ) ) ? '0' : get_post_meta( $post->ID, '_prefix_hide_page', true ) ;          
        if( in_array('1', $hide_pages, true) ) {
            $hide_page = '1';
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.