WordPress functions php if is home

What are the different ways to detect home page in wordpress?

I don’t know WordPress well, but could you do it quick and easy with $isHome = ($_SERVER[‘REQUEST_URI’] == ‘/’) (or whatever your homepage is)?

8 Answers 8

is_front_page() is what you want.

I assume, by the fact that is_home() is not working, that your home page is static, according to the settings in wp-admin.

is_home() returns true on your main blog page whereas is_front_page() returns true on which ever page is defined as your front page, feed or not.

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.

It works and doesn’t overcomplicate things, especially as is_front_page() and is_home() don’t always work as you’d expect them to.

Читайте также:  Sql injections in java

Works like a charm. $_SERVER is the one I always use and it always works.

this method worked great for me — is_front_page() and is_home() had no effect as I didnt have a static page set as the home page, just a list of recent posts

if(get_option("page_on_front") == $post->ID) < //do front page stuff here >

In many situations a WordPress site can have is_home and is_frontpage both eval as true on the REAL homepage and also on the main blog page. After building sites in WordPress for about 4 years this still bothers me.

For example if you have a site where you have your latest posts on your homepage with maybe a slider or some other homepage-centric elements, AND have another blog page, then is_frontpage and is_home will both eval as true on BOTH pages. So WordPress does not have a clear conditional function for the true homepage, at least the way most people think of the homepage of a website.

So I agree with Liam that if you get into a confusing situation, something like if ( $_SERVER[«REQUEST_URI»] == ‘/’ )

Источник

Display menu if site is homepage (wordpress)

I would like do display the another menu if site is homepage. In other cases it should be menu from code I pasted. What code may I use?

4 Answers 4

You can combine is_front_page() and is_home() WordPress function.

 // you are on homepage, show your another menu else < ?> 
name; ?>
?>

Looks good, but after use it I don’t see no additional menu. I try with only «test» text too and it is also invisible.

@Pat and in WordPress administration, which site you using as homepage? Standard WP page with posts, or some static page?

If you want to display specific content on wordpress homepage than you can do it using is_front_page()

Since is_front_page() and is_home() for me doesn’t work (WP 5.2.3), I write this condition:

global $wp; $current_url = home_url(add_query_arg(array($_GET), $wp->request)); if ($current_url==get_site_url()) < // code for homepage >

Variant with php — answer by @hardik solanki

This question is in a collective: a subcommunity defined by tags with relevant content and experts.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.27.43548

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

is_home() — проверяет, отображается ли в данный момент главная страница с выводом на ней последних постов

Возвращает true , если в данный момент отображается главная страница сайта с выводом на ней последних записей, это значит, что в настройках чтения должно быть следующее :

настройки чтения WordPress, необходимые для того, чтобы тег is_home() срабатывал на главной странице

Если настройки чтения отличаются от указанных или же текущая страница не является главной — возвращает false .

Кстати говоря, если в настройках установлена «Статическая страница», то на главной будет срабатывать тег is_page().

Если же вам нужно определять главную страницу сайта вне зависимости от настроек, используйте условный тег is_front_page().

Функция не имеет каких-либо параметров.

if( is_home() ){ // какой-нибудь контент или код для главной страницы } else { // какой-нибудь контент или код для остальных страниц }

Вторую часть примера можно записать ещё и вот так:

if( !is_home() ){ // какой-нибудь контент или код для остальных страниц }

Миша

Впервые познакомился с WordPress в 2009 году. Организатор и спикер на конференциях WordCamp. Преподаватель в школе Нетология.

Пишите, если нужна помощь с сайтом или разработка с нуля.

Источник

How to use is_front_page and is_home functions in WordPress

In many cases WordPress developers want to display specific content only on homepage. In other cases they’ll want to display some content only on subpages and not on the homepage. This content can be any kind of HTML elements (text, images…), event template parts such as widget areas (sidebars) or headers and footers (different on homepage and subpages). For that purpouse we’ll use is_front_page and is_home functions.

Difference between is_front_page and is_home functions

WordPress by default has two different homepage layouts. One layout displays a list of latest blog posts and the other shows a static page. To choose between this two layouts click on Settings in the Dashboard left column menu and then choose the Reading section. Homepage layout is defined in the first option of the Reading Settings screen.

Difference between is_home and is_front_page functions

WordPress Reading settings which define the layout of the homepage.

The difference between is_front_page and is_home functions is that each function is used for one of the homepage layouts:

  • is_home function is used when homepage displays the latest blog posts
  • is_front_page function is used when homepage displays a static page

Adding is_home() and is_front_page() functions to WordPress theme files

WordPress functions is_front_page and is_home functions work properly only when added to the template files that generate the home page. Usually the home page is genrated form header.php , footer.php and the file that generates the content section. That file varies from theme to theme and sometimes it requires some intermediate understanding of WordPress file structure to locate it.

For example, in the Twenty Nineteen WordPress theme, the file that generates the content part of the a static page is located in the theme subfolder /theme-parts/content/ and its called content-page.php.

PRO TIP:Pro Tip: Before you start editing your WordPress theme, consider creating a Child theme if you’re not already using one. Editing a Child theme will keep your modifications safe when updating the main theme. We also suggest using a Code highlighter plugin for easier editing of WordPress theme files.

How to show or hide content on the home page and subpages in WordPress

In our example we’ll display some custom content between the page header and content. Firstly we have to locate the header template file inside the WordPress theme editor. Secondly we have to position the cursor inside the template file where we want to add our custom content. In most cases that position will be right after the closing

tag.

If the homepage is set up to display the latest blog posts, insert the following code:

If the homepage is set up to display a static page, insert the following code:

By slight modifications of the code snippets above you can show or hide any HTML element or WordPress template part on your home page or on subpages.

The creation of any WP based website, from simple personal blogs over popular web magazines to sophisticated web shops start .

Источник

Оцените статью