0

I have a page marked as the Posts page that lists most recent blog posts.

It is at path /blog.

I want to know when the user is on this page so that I can display a list of categories of blog posts at the beginning of the page. I already did this in singular.php and in the rest of the cases in index.php.

For me is_home() returns true when on the landing page. And I use its behavior in index.php.

I tried a little to use this but it does not work:

get_page_by_path('/blog') == get_post()

So my current code is:

if (!is_home() || get_page_by_path('/blog') == get_post()) {
    get_template_part('template-parts/blog-categories');
}

and in the /blog page the blog categories are not shown. I am sure it is because of this condition in the if above.

Update 1

This condition does not work:

$_SERVER['PHP_SELF'] === '/blog'

because it is not /blog, but index.php (behavior build through .htaccess I guess).

Update 2

screenshot

Translated: Blog - Articles Page

This is not considered front page.

Update 3

screenshot

From this screenshot I think that home is Start (LP) and Blog is Posts page. Because of this I do not get why the is_front_page call is there:

if (!is_front_page() && is_home() || is_archive()) {
    get_template_part('template-parts/blog-categories');
}
1
  • "why the is_front_page call is there" - so you could do just if ( is_home() || is_archive() ) because is_home() would only be true on the Posts page (your "Blog" Page); however there's no harm checking for is_front_page() which would ensure the current page is not the "Start" Page which is the static site homepage.
    – Sally CJ
    Mar 26, 2021 at 8:09

1 Answer 1

2

You could try this instead

if (!is_front_page() && is_home()) {
    get_template_part('template-parts/blog-categories');
}

Explanation for the difference between front_page an home is here: https://wordpress.stackexchange.com/a/239838 if you set a page as a blog-page it is "home", in your case the landing-page is the so called "front_page". This is WordPress specific.

7
  • In my case the home page is the landing page and the /blog page is not the front page. Mar 23, 2021 at 0:31
  • I have updated the question with more information. Mar 23, 2021 at 0:36
  • @silviubogan, have you actually tried the code in this answer?
    – Sally CJ
    Mar 23, 2021 at 2:06
  • 1
    Try using the exact condition as in this answer: !is_front_page() && is_home() - and I believe that should work if you want to check whether the current page is the Posts page at /blog.
    – Sally CJ
    Mar 23, 2021 at 2:22
  • 1
    It works! Thanks! The final condition I use is !is_front_page() && is_home() || is_archive(). I still have to focus more to understand the role of is_front_page call in it. Mar 23, 2021 at 2:38

Your Answer

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

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