admin管理员组

文章数量:1291130

Regarding to the performances, and the performances only (and not from the point of view of the readability of the code), is it a best practice to check if the post has a thumbnail before to check if we are on the front-page, the home page, a single page, etc. or to do the opposite ?

Will my website load faster if I write :

<?php

if ( has_post_thumbnail() ) {

  if ( is_front_page() ) {
    // Do something…
  } elseif ( is_archive() ) {
    // Do something…
  } elseif ( is_page() ) {
    // Do something…
  } elseif ( is_single() ) {
    // Do someting…
    // And so on
  }
} else {
  // Do something…
}
?>

OR if I write :


if ( is_front_page() ) {
  if ( has_post_thumbnail() ) {
    // Do something…
  } else {
    // Do something…
  }
}

if ( is_archive() ) {
  if ( has_post_thumbnail() ) {
    // Do something…
  } else {
    // Do something…
  }
}

if ( is_page() ) {
  if ( has_post_thumbnail() ) {
    // Do something…
  } else {
    // Do something…
  }
}

if ( is_single() ) {
  if ( has_post_thumbnail() ) {
    // Do something…
  } else {
    // Do something…
  }
}

?>```

Regarding to the performances, and the performances only (and not from the point of view of the readability of the code), is it a best practice to check if the post has a thumbnail before to check if we are on the front-page, the home page, a single page, etc. or to do the opposite ?

Will my website load faster if I write :

<?php

if ( has_post_thumbnail() ) {

  if ( is_front_page() ) {
    // Do something…
  } elseif ( is_archive() ) {
    // Do something…
  } elseif ( is_page() ) {
    // Do something…
  } elseif ( is_single() ) {
    // Do someting…
    // And so on
  }
} else {
  // Do something…
}
?>

OR if I write :


if ( is_front_page() ) {
  if ( has_post_thumbnail() ) {
    // Do something…
  } else {
    // Do something…
  }
}

if ( is_archive() ) {
  if ( has_post_thumbnail() ) {
    // Do something…
  } else {
    // Do something…
  }
}

if ( is_page() ) {
  if ( has_post_thumbnail() ) {
    // Do something…
  } else {
    // Do something…
  }
}

if ( is_single() ) {
  if ( has_post_thumbnail() ) {
    // Do something…
  } else {
    // Do something…
  }
}

?>```

Share Improve this question asked Sep 29, 2022 at 15:36 PhpDoePhpDoe 2992 silver badges11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Neither, they are just as fast as each other, both are using information that has already been fetched in advance. This is micro-optimising.

Functions such as is_front_page or is_singular etc etc are just looking up variables already set in the main query object.

Likewise functions such as has_post_thumbnail etc are looking up data that has already been fetched. WordPress will try to fetch things in advance, and most functions you call are already cached, or rely on pre-fetched data. For example when WordPress fetches a post, it also fetches its post meta and terms in bulk to save time and improve performance. This is why get_post_meta calls don't add database queries on most pages (because the data is already fetched and stored in WP_Cache).

If you want to make your site faster, you should not be looking at things like this, and you should be measuring performance timings. If you took measurements you would see no measurable difference. Unless you have proof this is slowing down your site, performance isn't a consideration for this code.

Normally the differences for code like this are so tiny when contributing to performance that they'll be drowned out by mundane things such as the temperature of the RAM and other minute things that aren't worth thinking about.

Tom's answer is better and more true, but I'm leaving this here because the logic might be helpful to see.

In theory, all else being equal, you'd want to minimize the number of checks. The first approach is more performant because if the post doesn't have a post thumbnail, it only does one check.

Then after that it minimized the number of checks because as soon as there's a match (is_front_page()) it stops checking the rest of them.

The second approach does a minimum of four checks, and a maximum of five. The first approach does a minimum of one check, and a maximum of five.

That said, not all checks are equal. All of the is_ functions here just check a value stored in the global wp_query object, so I believe there's no call to the database, and effectively no difference for each of these checks. You'd have to run them thousands of times to make a difference.

has_post_thumbnail() uses get_post() and get_post_meta(), so there might be one or two database calls depending on what caching is going on. So this would be the function to limit use of, which means that your second approach could actually be more performant in cases where none of the is_ functions are true.

You can actually test this yourself using the microtime() function, which measures how long the server is taking to process a request. Run it before and after the code, then take the difference between the two. (Run it several times to get an average.)

本文标签: