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.
Let’s see if I can confuse myself.
If either of your two
OR
conditions istrue
the code executes.is_home
andis_front_page
can returntrue
for different pages, negated in your case. If you have a static from page, which it sounds like you do, thenis_home
is the blog index page.What that means is that
is_home
will befalse
for every page except your blog index, which means that your!is_home
conditional istrue
for every page except that blog index page and your code will run almost all the time.Now the docs for
is_front_page
.There are two possibilities depending on your settings. It might return
true
for your blog index page, or it might returntrue
for your static page. I think that you have a static front page sois_front_page
will returntrue
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 entireOR
statement:If either is
true
then the whole thing isfalse
.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 bothis_home
andis_front_page
would have to befalse
for the code to run. That is, you are not on the blog index and not on the front at the same time.Before I address your main issue, I must point out a glaring syntax error:
Why is the
endif
there? It makes no sense whatsoever, and everything in programming has a purpose/reason. You might as well change it tomarigolds-potatoe-farm
, it will have the same effect, that of a fatal error. So get rid of it.This is the basic if statement:
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:
Don’t make things hard for yourself
Moving on to your logic issue
Is this in english prose:
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:
Which is:
Or it can be written as:
Or further still:
So To Summarize
If the goal is such:
Then you should be able to use a simple conditional:
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()
andis_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 pageis_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 pageFront Page Displays: Blog Posts
When the Site Front Page is set to display the Blog Posts Index:
'posts' == get_option( 'show_on_front' )
is_home()
andis_front_page()
are true when the first page of the Blog Posts Index is displayed.is_home()
andis_front_page()
are trueis_home()
is true, andis_front_page()
is false, on subsequent pages (i.e. paginated display) of the blog posts indexFront 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, andis_front_page()
is true, on the Site Front Page (i.e. Page ID =get_option( 'page_on_front' )
)is_home()
is true, andis_front_page()
is false, on the static page designated to display the Blog Posts Index (i.e. Page ID =get_option( 'page_for_posts' )
is_home()
andis_front_page()
are false on every other pageI assume
is_home
andis_front_page
are similar tois_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:
Add a variable prior to that line and make it global so that you can use it in your Loop later like so:
Then, inside your Loop, you can use this variable as a condition for your Loop:
Why not simply take the code out of the template and hook it in from your child themes functions file like this:
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.