最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

themes - Apache errors when retrieving taxonomies

programmeradmin4浏览0评论

I'm regularly seeing Apache errors when my category-single-page.php is pulling in the categories related to a post:

PHP Warning: array_values() expects parameter 1 to be array, bool given in /wp-content/themes/my-theme/inc/category-single-page.php on line 4

This is the first few lines from the php file (lines numbered for reference only NOT in the actual file)

<?php while ( have_posts() ) : the_post(); ?>
<?php
    $thiscategory = get_the_terms( $post->ID, $taxonomy_name );
    $thiscategory = array_values( $thiscategory );  // This is line 4
    $section = get_post_type( $post->ID );
    $thispost = $post->ID;
?>

The php file doesn't seem to generate an error all of the time it is called - so I think my code is OK (I could be wrong!) so any thoughts of what could be causing Apache to log the error on occasion, or is it my code at fault?

I'm regularly seeing Apache errors when my category-single-page.php is pulling in the categories related to a post:

PHP Warning: array_values() expects parameter 1 to be array, bool given in /wp-content/themes/my-theme/inc/category-single-page.php on line 4

This is the first few lines from the php file (lines numbered for reference only NOT in the actual file)

<?php while ( have_posts() ) : the_post(); ?>
<?php
    $thiscategory = get_the_terms( $post->ID, $taxonomy_name );
    $thiscategory = array_values( $thiscategory );  // This is line 4
    $section = get_post_type( $post->ID );
    $thispost = $post->ID;
?>

The php file doesn't seem to generate an error all of the time it is called - so I think my code is OK (I could be wrong!) so any thoughts of what could be causing Apache to log the error on occasion, or is it my code at fault?

Share Improve this question edited Oct 9, 2020 at 19:51 Howdy_McGee 20.9k24 gold badges91 silver badges177 bronze badges asked Oct 9, 2020 at 19:37 SNCookeSNCooke 111 bronze badge 1
  • Just before line 4 can you add var_dump( $thiscategory ); and check if it is indeed an array? – Tony Djukic Commented Oct 13, 2020 at 20:56
Add a comment  | 

1 Answer 1

Reset to default 0

It's a PHP error, not an Apache error.

What's happening is that get_the_terms() returns an array of terms, or false if there are no terms or the post doesn't exist.

You can fix this like this:

$thiscategory = get_the_terms( $post->ID, $taxonomy_name );
if ( is_array( $thiscategory ) ) {
    // Only runs if $thiscategory is an array.
    $thiscategory = array_values( $thiscategory );  // This is line 4
}
$section = get_post_type( $post->ID );
$thispost = $post->ID;
发布评论

评论列表(0)

  1. 暂无评论