0

With a plugin I'm trying to develop, I'm trying to place some content on the front-page of the website after the header.

In my plugin PHP file I have this (I've cut the html content out to save space):

add_action( '__after_header' , 'add_promotional_text', 4 );
function add_promotional_text() {
    // If we're on the home page, do something
    if ( ! is_admin() ) {
        if ( is_front_page() && is_home() ){

            return;


            //HTML goes here
        }
    }
}

do_action ( '__after_header' );

This almost does what I want it to do. The content is not showing up in the admin area... and it does show up on the front-page like it is supposed to... however, it is also showing up on every page or post, which is not supposed to happen.

It would be desirable for it to only show up on the front-page, and not on any other front-end facing pages.

I am not sure how to do that.

EDIT:

I have simplified the plugin code to just this:

if ( is_front_page() && is_home() ){
    echo "<script>console.log( 'Debug Objects: is_front_page() && Home()' );</script>";
} else {
    echo "<script>console.log( 'Debug Objects: ! is_front_page() && Home()' );</script>";
}

And no matter how things are set in Settings > Reading, this if condition is never true. It is always false no matter where I navigate to in the site whether it is the front end or admin area.

Settings > Reading: enter image description here

Front Page with console output: enter image description here

1

After turning on WP_DEBUG,

define('WP_DEBUG', true);

I was receiving the error: Conditional query tags do not work before the query is run. Before then, they always return false.

After googling that error, I found a solution that works:

add_action( 'template_redirect', 'check_for_frontpage' );

function check_for_frontpage() {
    if ( is_front_page() || is_home() ) {
        echo "<script>console.log( 'Debug Objects: is_front_page() && Home()' );</script>";
    } else {
        echo "<script>console.log( 'Debug Objects: ! is_front_page() && Home()' );</script>";
    }
}
0

This code appears wrong. Your version was doing a return if the front page or home was true. Change that part to this:

if ( is_front_page() && is_home() ){
    // output the content because frontpage/home is true
   } else
    {
      // output content for any not frontpage/home
    }

Add whatever code needed for each condition

  • I had tried that, but for some reason, the content was appearing in the admin area at he top of the pages as well. I'll try what you just typed in case I had some sort of typo previously. – Livi17 Jul 25 '17 at 17:50
  • The problem is not a typo, it's that your 'insert html code' was in the wrong part of the if/else. You were 'returning' if the statement was true. If that statement is true, then you want to output the front-page-only content. If the statement is false (not on the home/front page), then that is where you put the return. – Rick Hellewell Jul 25 '17 at 17:52
  • Ive simplified my plugin code to just the code in your solution... and no matter what, the condition is never true. No matter what I have set in Settings > Reading, it is always false. See edit in my question above. – Livi17 Jul 27 '17 at 12:00
  • You may need, as others have suggested, to split your 'if' condition into checking for just one of the is_front_page or is_home conditions. And making sure that your test is within the loop (after the WP_query). – Rick Hellewell Jul 27 '17 at 18:05
0

So in the docs say that the is_home and is_front_page, can return diferent values depending on the page. Here are some examples:

  • If 'posts' == get_option( 'show_on_front' ):

    • On the site front page:

      • is_front_page() will return true
      • is_home() will return true
    • If assigned, WordPress ignores the pages assigned to display the site front page or the blog posts index

  • If 'page' == get_option( 'show_on_front' ):

    • On the page assigned to display the site front page:

      • is_front_page() will return true
      • is_home() will return false
    • On the page assigned to display the blog posts index:

      • is_front_page() will return false
      • is_home() will return true

So if you set a static page to apper on front of the site home_url will return false. If you change you code to this:

if ( is_front_page() && is_page('home') ){
  // output the content because frontpage/home is true
} 
else {
 // output content for any not frontpage/home
}

Where 'home' is the slug of the page where you marked to show on front, will work.

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.