admin管理员组

文章数量:1298409

I've made a child theme of Twenty Twenty theme and want to display the posts/ category archive pages in flexbox grid.

However, none of the conditional tags I'm hoping to check outside the loop (is_home, is_archive, is_category) are returning true on posts page or category archive pages:

* @since Twenty Twenty 1.0
*/
get_header();
?>

<?php if ( is_archive() ): ?>
    <h1>ARCHIVE</h1>
<?php endif; ?>
<?php if ( is_category() ): ?>
    <h1>CATEGORY</h1>
<?php endif; ?>
<?php if ( is_home() ): ?>
    <h1>HOME</h1>
<?php endif; ?>

<main id="site-content maybe-flex-directive-class-here">

    <?php

    if ( have_posts() ) {

        while ( have_posts() ) {
            the_post();

I expect either I'm missing something obvious or going about it the wrong way.

I've made a child theme of Twenty Twenty theme and want to display the posts/ category archive pages in flexbox grid.

However, none of the conditional tags I'm hoping to check outside the loop (is_home, is_archive, is_category) are returning true on posts page or category archive pages:

* @since Twenty Twenty 1.0
*/
get_header();
?>

<?php if ( is_archive() ): ?>
    <h1>ARCHIVE</h1>
<?php endif; ?>
<?php if ( is_category() ): ?>
    <h1>CATEGORY</h1>
<?php endif; ?>
<?php if ( is_home() ): ?>
    <h1>HOME</h1>
<?php endif; ?>

<main id="site-content maybe-flex-directive-class-here">

    <?php

    if ( have_posts() ) {

        while ( have_posts() ) {
            the_post();

I expect either I'm missing something obvious or going about it the wrong way.

Share Improve this question asked Aug 11, 2022 at 1:26 MikeiLLMikeiLL 5791 gold badge8 silver badges21 bronze badges 5
  • Which template file does this code reside in? – bosco Commented Aug 11, 2022 at 1:29
  • it's in singular.php – MikeiLL Commented Aug 11, 2022 at 1:41
  • which makes the answer really obvious! I wasn't seeing that the theme includes <main id="site-content"> on index.php as well as singular.php. I think I will delete this post unless you feel like posting an answer. Please lmk. – MikeiLL Commented Aug 11, 2022 at 1:46
  • Naw that's cool - you can delete :P. Always keep our friendly template hierarchy close at hand! – bosco Commented Aug 11, 2022 at 1:47
  • Thanks and peace man – MikeiLL Commented Aug 11, 2022 at 1:56
Add a comment  | 

1 Answer 1

Reset to default 0

I was going to (and actually did) delete this post, but after sleeping on it, think it might end up being of some use.

What I was missing, above, is [Template Hierarchy][1]. I was looking at the singular.php template file, which will only ever be loaded when is_archive(), is_home(), etc, aren't true.

What got me is that the HTML <main> element exists on both the index.php and singular.php documents in this theme.

So I was able to update the child theme's copy of index.php:

<main id="site-content" class="flex-container">

With CSS:

.flex-container {
  display: flex;
  flex-direction: column;
}
/* Responsive layout - make multi-column on larger screens */
@media (min-width: 800px) {
  .flex-container {
    flex-direction: row;
  }
}

Thanks for the solution in the comment, @bosco.



  [1]: https://developer.wordpress.org/themes/basics/template-hierarchy/

本文标签: conditional tagsGetting false for ishome() on Posts page