3

I have this snippet in a "portfolio-loop.php" file. I'm using get_template_part to display the loop on the front page and on other pages.

I have this within the loop:

<?php if( !is_home() || !is_front_page() ) { ?>
  <p><?php the_time('F Y'); ?></p>
<?php endif } ?>

I can't seem to get it to exclude the front page. Could it because it's in it's own template?

edit

Ok I fixed some of my semantic errors thanks to s_ha_dum and Tom Nowell. I've tried the following variations:

<?php if ( !is_home() && !is_front_page() ) { ?> 
    <!--nada -->
<?php } else { ?>
    <p><?php the_time('F j, Y'); ?></p>
<?php } ?>



//dropping is_home() and trying to keep it as simple as possible
<?php if(!is_front_page()) { ?>
    <p><?php the_time('F j, Y'); ?></p>
<?php } ?>

I still can't hide the time on the front page. At best '<--nada-->' will appear on both my home page (home.php) and on my archive (category-blog.php). Is my custom category page seen as a front page as well?

edit 2

What I'm trying to do in plain english:

I have a loop on my front page (static, home.php) displaying three of my latest blog posts via get_template_part. The same loop is used for my category-blog.php/archive listing but shows the last 10 posts. I do not want to show the date/time on the front page to keep the layout cleaner, only on the other pages that this loop is displayed on.

5
  • 1
    && instead of || maybe?
    – Richard B
    Commented May 16, 2013 at 13:40
  • I have tried using && as well. still doesn't work...
    – Alan
    Commented May 16, 2013 at 13:56
  • It is almost always helpful to write out, in human-readable language, what it is you're trying to accomplish, and then construct a corresponding conditional statement. So: can you edit your question and add an explicit, human-readable statement of what you're trying to accomplish? Commented May 16, 2013 at 16:49
  • Site front page, or simply the first page of the blog posts index? Commented May 16, 2013 at 17:01
  • I'm trying to prevent the_time from showing up on the site front page that is static and is set as the home page through the reading settings
    – Alan
    Commented May 16, 2013 at 17:09

5 Answers 5

8

Let's see if I can confuse myself.

If either of your two OR conditions is true the code executes.

is_home and is_front_page can return true for different pages, negated in your case. If you have a static from page, which it sounds like you do, then is_home is the blog index page.

Note: WordPress 2.1 handles this function differently than prior versions - See static Front Page. If you select a static Page as your frontpage (see is_front_page()), this tag will be applied to your "posts page".

http://codex.wordpress.org/Function_Reference/is_home

What that means is that is_home will be false for every page except your blog index, which means that your !is_home conditional is true for every page except that blog index page and your code will run almost all the time.

Now the docs for is_front_page.

It returns TRUE when the main blog page is being displayed and the Settings->Reading->Front page displays is set to "Your latest posts", or when is set to "A static page" and the "Front Page" value is the current Page being displayed.

http://codex.wordpress.org/Function_Reference/is_front_page

There are two possibilities depending on your settings. It might return true for your blog index page, or it might return true for your static page. I think that you have a static front page so is_front_page will return true for that page and the !is_front_page conditional will be false. Because !is_home is (probably, if I am following you) true the code still executes.

If you want to exclude <p><?php the_time('F Y'); ?></p> from both the blog index and the static front page, then negate the entire OR statement:

if( !(is_home() || is_front_page()) )

If either is true then the whole thing is false.

If you want to exclude it only from the front page, then you shouldn't need the !is_home part at all.

If you use && instead of || then both is_home and is_front_page would have to be false for the code to run. That is, you are not on the blog index and not on the front at the same time.

1
  • For more detail- I have a static page set as the home page. The loop shows up on the front page, and on the category/archive page. I want to show the_time on the category/archive page but not on the "home front static page". I appreciate your help... I'm dissecting your reply as I'm a complete php newb
    – Alan
    Commented May 16, 2013 at 14:52
6

If the goal is such:

I do not want to show the date/time on the front page to keep the layout cleaner, only on the other pages that this loop is displayed on.

Then you should be able to use a simple conditional:

<?php if ( ! is_front_page() ) { ?>
    <p><?php the_time('F Y'); ?></p>
<?php } ?>

Without knowing exactly what you're trying to accomplish, it's not really possible to provide the exact conditional statement you need. That said, here are the conditions when either/or/both of is_home() and is_front_page() will be true and/or false.

In General

  • is_home() is true when the Blog Posts Index is being displayed, whether on the site front page, or a designated static page
  • is_front_page() is true when the Site Front Page is being displayed, whether the site front page displays the Blog Posts Index, or a designated static page

Front Page Displays: Blog Posts

When the Site Front Page is set to display the Blog Posts Index:

  • 'posts' == get_option( 'show_on_front' )
  • Both is_home() and is_front_page() are true when the first page of the Blog Posts Index is displayed.
  • On the Site Front Page, both is_home() and is_front_page() are true
  • is_home() is true, and is_front_page() is false, on subsequent pages (i.e. paginated display) of the blog posts index
  • On any other page, both are false

Front Page Displays: Static Page

When the Site Front Page is set to display a Static Page:

  • 'page' == get_option( 'show_on_front' )
  • is_home() is false, and is_front_page() is true, on the Site Front Page (i.e. Page ID = get_option( 'page_on_front' ) )
  • is_home() is true, and is_front_page() is false, on the static page designated to display the Blog Posts Index (i.e. Page ID = get_option( 'page_for_posts' )
  • Both is_home() and is_front_page() are false on every other page
5

Before I address your main issue, I must point out a glaring syntax error:

<?php if( !is_home() || !is_front_page() ) { ?>
  <p><?php the_time('F Y'); ?></p>
<?php endif } ?>

Why is the endif there? It makes no sense whatsoever, and everything in programming has a purpose/reason. You might as well change it to marigolds-potatoe-farm, it will have the same effect, that of a fatal error. So get rid of it.

This is the basic if statement:

if ( this statement is true ) {
    do this
} else {
    do that
}

There is shorthand for if (): endif; etc, don't use those, they are more trouble at this stage than they're worth.

All of this should have been caught and highlighted by your editor. If it wasn't, you're using an incompetent editing program. Go get an editor with brace autocompletion, PHP linting/syntax checking, colour coding, and autocompletion.

Examples include but aren't limited to:

  • Netbeans
  • PHPStorm
  • SublimeText
  • Komodo Edit/IDE
  • Eclipse

Don't make things hard for yourself

Moving on to your logic issue

if( !is_home() || !is_front_page() ) {

Is this in english prose:

If it is not the homepage, do this, also do this if it is not the front page

So if it's the homepage, it will do it. If it is the frontpage, it will do it.

What I believe you meant is:

if it is neither the homepage or the frontpage, then do it.

Which is:

if ( !( is_home() || is_front_page() ) ) {

Or it can be written as:

if ( !is_home() && !is_front_page() ) {

Or further still:

if ( !is_home() ) {
    if ( !is_front_page() ) {

So To Summarize

  • This is basic logic
  • You dont need PHP skills to know basic logic, people figured out boolean true/false logic in Ancient Greece, some children do it in school
  • A grasp of the proper use of full stops will help you here as it demonstrates how moving the comma over 1 word changes the meaning entirely, just as moving the ! && or || can do the same. It's all about semantics.
  • Get a decent editor, it saves you a massive amount of time and effort
  • You're check worked beautifully, it was just checking the wrong thing
1
  • +1 I did not spot that syntax error. Also, +1 again (if I could) for discouraging the shorthand conditionals.
    – s_ha_dum
    Commented May 16, 2013 at 17:12
0

I assume is_home and is_front_page are similar to is_page and the likes in that they cannot be used inside The Loop.

My current work around, at least, is to send a variable to The Loop.

So, as a rough example, let's say in you homepage you have a portfolio loop... maybe something like this line below:

get_template_part( 'loop', 'portfolio' );

Add a variable prior to that line and make it global so that you can use it in your Loop later like so:

global $hide_time;
$hide_time = true;
get_template_part( 'loop', 'portfolio' );

Then, inside your Loop, you can use this variable as a condition for your Loop:

<?php
global $hide_time;
if( $hide_time != true ) { ?>
    <p><?php the_time('F Y'); ?></p>
<?php } ?>

NOTE: the variable has to be global in both files.

0

Why not simply take the code out of the template and hook it in from your child themes functions file like this:

    add_action ( 'your_themes_hook', 'display_date_exclude_front_page' );
    function display_date_exclude_front_page() {
if ( !is_front_page() ) {
    echo the_time('F Y');
        }
    }

Note: Don't forget to replace your_themes_hook with your themes hook or a WordPress hook.

is_front_page always returns true on the front page.

is_home() returns true for your posts page regardless of where that is.

It can be any page using a blog page template or the front page if using the default Reading Settings otherwise you change it there.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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