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

categories - Cannot use object of type WP_Error as array display category

programmeradmin3浏览0评论

I'm trying to deduce a category for my custom WordPress post, but I get this error. How to fix ?

$events= wp_get_post_terms(get_the_ID(), 'events', array("fields" => "all"));
<?php echo $events[0]->name ?>

add_action( 'init', 'cptui_register_my_cpts' );
function cptui_register_my_cpts() {
  $labels = array(
    "name" => __( 'Events', '' ),
    "singular_name" => __( 'event', '' ),
    );

  $args = array(
    // HERE
    "label" => __( 'Events', '' ),
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => false,
    "rest_base" => "",
    "has_archive" => false,
    "show_in_menu" => true,
    "exclude_from_search" => true,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => false,
    "rewrite" => array( "slug" => "events", "with_front" => false ),
    "query_var" => true,

    "supports" => array( "title", "editor", "thumbnail", "excerpt", "revisions" ),          );
  register_post_type( "events", $args );

// End of cptui_register_my_cpts()
}

I'm trying to deduce a category for my custom WordPress post, but I get this error. How to fix ?

$events= wp_get_post_terms(get_the_ID(), 'events', array("fields" => "all"));
<?php echo $events[0]->name ?>

add_action( 'init', 'cptui_register_my_cpts' );
function cptui_register_my_cpts() {
  $labels = array(
    "name" => __( 'Events', '' ),
    "singular_name" => __( 'event', '' ),
    );

  $args = array(
    // HERE
    "label" => __( 'Events', '' ),
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => false,
    "rest_base" => "",
    "has_archive" => false,
    "show_in_menu" => true,
    "exclude_from_search" => true,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => false,
    "rewrite" => array( "slug" => "events", "with_front" => false ),
    "query_var" => true,

    "supports" => array( "title", "editor", "thumbnail", "excerpt", "revisions" ),          );
  register_post_type( "events", $args );

// End of cptui_register_my_cpts()
}
Share Improve this question edited Jan 29, 2020 at 6:49 kaizer asked Jan 29, 2020 at 6:06 kaizerkaizer 11 bronze badge 1
  • You have not added 'taxonomies' => array('topics', 'category' ), in args array() – Dharmishtha Patel Commented Jan 29, 2020 at 7:24
Add a comment  | 

1 Answer 1

Reset to default 0

wp_get_post_terms() will return a WP_Error object if the taxonomy can't be found. So $events[0] won't exist if that happens.

If you're getting this error it means that there's no taxonomy named events registered on your site. So you'll need to make sure that you've used the correct name for the taxonomy whose terms you want to list.

发布评论

评论列表(0)

  1. 暂无评论