WordPress conditional statement for front-page or inner page - php

I need help with the templating in wordpress.
On the front-page I want a custom static header, while all the other pages should use the title for their page instead.
Also, if the page is not the front-page I want to add some custom HTML next to the title.
What is the best way to do this?

You can do it like this, including the following in header.php (I've just added some example content);
<?php
if ( is_front_page() ) {
echo '<div id="homestuff">Home stuff in here</div>';
} elseif (is_page()) {
echo '<div id="pagestuff">'. the_title() .'</div>';
}
?>
Take note, there is also a is_home() in Wordpress and it's sometimes easy to get in a bit of a tangle about whether is_front_page() or is_home() is the correct one to use, this stackexchange answer is a good source of info about that;
Whether to use is_front_page() or is_home() in Wordpress

Related

Hide wp_head () only in the homepage

I've this code in the header Homepage that find automatically the title page for homepage and the posts how title tag. But I've my personal title tag only in the home page. The source code show 2 title tag in the home.
How can I hide this code in the home?
<? php wp_head(); ? >
i think, you can use the "is_home()" and "is_frontpage()" function, depending on how your frontpage is setted up in theme (static page, blog page...), one of them or using logical and can be necessary.
with this you can do like:
<?php
if( !is_home() && !is_frontpage() ) {
wp_head();
} else {
// custom head code
}
?>
an other way is using a separate template for home/frontpage (using a frontpage.php/home.php), where you put in your code.
here the difficult for me was to learn how wordpress is using more specific templates and templateparts (like using get_header('home') to use header-home.php instead of header) and with which weight-order they are used.
https://developer.wordpress.org/reference/functions/is_home/
https://codex.wordpress.org/Creating_a_Static_Front_Page
is_front_page() & is_home() can't help you ?

Wordpress: Conditional Statements same for blog and frontpage?

I have added a video to the homepage header of my wordpress theme but it also displays on the blog page.
my php is
if ( is_home() || is_front_page() ) {
echo '
<div class="overlay-caption">
<h1 class="home-h1">Join Us</h1>
With over 30 years of experience your adventure with us will be fun, memorable and help you pursue your passion for photography.
</div>
<div class="videoWrapper">
<iframe width="560" height="315" src="https://www.youtube.com/embed/3tmFvHviYcY?rel=0&autoplay=1&modestbranding=1&controls=0&loop=1&playlist=3tmFvHviYcY" frameborder="0" allowfullscreen autoplay="1"></iframe>
</div>';
}
else {}
?>
Now if i try only is_front_page it doesn’t appear on the homepage, if I try is_home it work but also shows up on the blog page.
I want the header on the blog page to be a standard header image, and I just want the homepage to display the video from the code I have included. Do I need to switch the conditional statement to a different order, or do I have the syntax completely wrong?
Thanks,
The basic difference between the is_home() and is_front_page() is as follows.
is_home()
The blog homepage is the page that shows the time-based blog content of the site.
is_home() is dependent on the site’s “Front page displays” Reading Settings ‘show_on_front’ and ‘page_for_posts’.
If a static page is set for the front page of the site, this function will return true only on the page you set as the “Posts page”.
is_front_page()
This Conditional Tag checks if the main page is a posts or a Page. This is a boolean function, meaning it returns either TRUE or FALSE. 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.
Hence now the code is as follows.
<?php
if(is_front_page() || is_home())
{
// The Code will execute if it is a 'Front Page'
// OR
// if the page is a 'Home Page'
}
if(is_front_page() && is_home())
{
// Here the code will execute if the page is
// A Front Page and as well as the Home Page
}
?>
Hence it depends on the situation of how you need to display the video over the page and to toggle around the condition that works on.
Or you try is_page() condition and that will make up the trick for you.
// When any single Page is being displayed.
is_page();
// When Page 42 (ID) is being displayed.
is_page( 42 );
// When the Page with a post_title of "Contact" is being displayed.
is_page( 'Contact' );
// When the Page with a post_name (slug) of "about-me" is being displayed.
is_page( 'about-me' );
Hope this explanation would be clear for you to understand better.
Thanks:) Happy coding.
The order of the statements here is critical, try flipping them around such as:
if(is_front_page() || is_home())
You can also use:
if(!is_front_page() && is_home())
Try something like this
if ( is_front_page() && is_home() ) {
// Default homepage
}

Wordpress is_home() not working

I want to make background color on homepage is transparent and on otherpage background is blue.
I have using is_home() function but can't work. This is my code
<?php
if(is_home()){
// we are on the home page
echo '<header id="masthead" class="site-hulu" role="banner">';
}else {
echo '<header id="masthead" class="site-header" role="banner">';
}
?>
How to fix its guys? Thanks before for your helping
Might I suggest using is_front_page() instead
See here https://codex.wordpress.org/Function_Reference/is_home specifically
Blog Posts Index vs. Site Front Page
Since WordPress 2.1, when the static front page functionality was introduced, the blog posts index and site front page have been treated as two different query contexts, with is_home() applying to the blog posts index, and is_front_page() applying to the site front page.

Customizing a WordPress Template to Remove *One* Page Title

I'm using WordPress to build a simple personal website. I know (basic) HTML and CSS but no PHP, so I'm having a little trouble with a particular customization.
My theme inserts the title of the page at the top of the page. I would like to stop it from doing this on one page only (the home page).
Thanks for any help. The site is here: http://www.melvingauci.com/
Try going into your page.php template and remove <?php the_title(); ?>. Then add this:
<?php if ( ! is_front_page() ) { ?>
<?php the_title(); ?>
<?php } ?>
Note: This assumes your home page uses the page.php template. If another template is used, then the same approach will work.

How can I tell my wordpress to do something in the header if its a blog page?

In a previous post (Don't show this if the url contains the following) I asked how I would go about having my header echo a div if the user loaded a URL with /blog in the header.
What I didn't take into consideration, was that I don't want the div to display if its not just got /blog in the url, but if its any blog post, not just the index page of the blogs.
How do I run a bit of code from my header.php if the page I'm looking at is a blog post?
You would use the Wordpress functions is_single() and is_page.
if(!is_single() && !is_page() && !is_home() && !is_archive())
{
/* This will not display for any post, page, the home page, or an archive.
You can remove each is statement according to your needs */
}
For only posts, only use is_single, the same for page, home, and archive.
The full listing of is_statements can be found here. Here are some others:
is_home() : Home Page
is_front_page : Front Page
is_single() : Single Post
is_admin() : Admin Panel
is_sticky() : Sticky Post
is_page() : Page
is_category
is_tag
is_author
It's been long since I've played with wordpress but you can achieve a lot with using the conditional tags:
http://codex.wordpress.org/Conditional_Tags
Seems like is_single() might do what you want. Eg:
if (!is_single())
{
// display div
}

Categories