admin管理员组

文章数量:1278883

Since updating from PHP 7.4 to PHP 8, my error_log is now full of lines like this from a child theme I'm using:

[17-Oct-2021 13:30:38 UTC] PHP Warning: Attempt to read property "term_id" on bool in ./public_html/wp-content/themes/themename-child/functions.php on line 181

Here is the block that is throwing the error. It was used to modify the pagination so posts of a particular type (links) are not navigated.

// modify pagination to exclude post_format of 'link' on news/blog posts
function profound_navigation() {
    $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
    $next     = get_adjacent_post( false, '', false );
    if ( ! $next && ! $previous ) {
        return;
    }
    $link_term = get_term_by('slug' , 'post-format-link' , 'post_format');
    $quote_term = get_term_by('slug' , 'post-format-quote' , 'post-format');

    the_post_navigation(
            array(
                    'next_text' => '<span class="meta-nav">Next Article <i class="flaticon-right-arrow"></i></span><h3 class="title">%title</h3>',
                    'prev_text' => '<span class="meta-nav"><i class="flaticon-left-arrow-1"></i> Previous Article</span><h3 class="title">%title</h3>',
                    // 'taxonomy' => 'post_format',
                    'excluded_terms' => array($link_term->term_id, $quote_term->term_id, 156),
            )
    );
}

Seems like the code still works fine, but how do I modify this code to avoid filling up my error log?

Since updating from PHP 7.4 to PHP 8, my error_log is now full of lines like this from a child theme I'm using:

[17-Oct-2021 13:30:38 UTC] PHP Warning: Attempt to read property "term_id" on bool in ./public_html/wp-content/themes/themename-child/functions.php on line 181

Here is the block that is throwing the error. It was used to modify the pagination so posts of a particular type (links) are not navigated.

// modify pagination to exclude post_format of 'link' on news/blog posts
function profound_navigation() {
    $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
    $next     = get_adjacent_post( false, '', false );
    if ( ! $next && ! $previous ) {
        return;
    }
    $link_term = get_term_by('slug' , 'post-format-link' , 'post_format');
    $quote_term = get_term_by('slug' , 'post-format-quote' , 'post-format');

    the_post_navigation(
            array(
                    'next_text' => '<span class="meta-nav">Next Article <i class="flaticon-right-arrow"></i></span><h3 class="title">%title</h3>',
                    'prev_text' => '<span class="meta-nav"><i class="flaticon-left-arrow-1"></i> Previous Article</span><h3 class="title">%title</h3>',
                    // 'taxonomy' => 'post_format',
                    'excluded_terms' => array($link_term->term_id, $quote_term->term_id, 156),
            )
    );
}

Seems like the code still works fine, but how do I modify this code to avoid filling up my error log?

Share Improve this question edited Oct 17, 2021 at 23:46 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Oct 17, 2021 at 14:28 algalgalgalg 1151 gold badge1 silver badge4 bronze badges 2
  • 2 I noticed you have post_format and post-format when using get_term_by, is this intentional? one with dash symbol and one with underscore. This could be the issue. Do a print_r on $link_term and $quote_term to see what they return, one is 100% empty. – Buttered_Toast Commented Oct 17, 2021 at 14:40
  • That's got it. post-format was empty and should've been post_format. Thanks! – algalg Commented Oct 18, 2021 at 15:29
Add a comment  | 

1 Answer 1

Reset to default 1

Make sure your taxonomy is exist. Which is a correct taxonomy slug between post-format and post_format?

False if $taxonomy does not exist or $term was not found.

Please see: https://developer.wordpress/reference/functions/get_term_by/#return

本文标签: termsPHP Warning Attempt to read property quottermidquot on bool