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

Adding custom post types to archive

programmeradmin0浏览0评论

I am wanting to dispaly custom post types on my archive pages using code from here:


but this now gives WP error message of:

Warning: Invalid argument supplied for foreach() in /wp-includes/class-wp-post-type.php on line 613

add_action('pre_get_posts', 'query_post_type');

function query_post_type($query) {
  if($query->is_main_query()
    && ( is_category() || is_tag() )) {
        $query->set( 'post_type', array('post','cpt') );
  }
}

Please let me know what changes are needed to not return errors.

I am wanting to dispaly custom post types on my archive pages using code from here:

https://wordpress.stackexchange/questions/179023/adding-custom-post-types-to-archive-php

but this now gives WP error message of:

Warning: Invalid argument supplied for foreach() in /wp-includes/class-wp-post-type.php on line 613

add_action('pre_get_posts', 'query_post_type');

function query_post_type($query) {
  if($query->is_main_query()
    && ( is_category() || is_tag() )) {
        $query->set( 'post_type', array('post','cpt') );
  }
}

Please let me know what changes are needed to not return errors.

Share Improve this question edited Mar 25, 2020 at 2:45 WordPress Speed 2,2833 gold badges19 silver badges34 bronze badges asked Jan 4, 2020 at 15:54 Timothy BurginTimothy Burgin 132 bronze badges 3
  • what are the names of your custom post types? are you trying to include more than one custom post type into the archive? – Michael Commented Jan 5, 2020 at 5:50
  • No, I just want to show 1 cpt in the archive. The specific name is "asana" and if I change this nothing shows in the archive. – Timothy Burgin Commented Jan 5, 2020 at 12:25
  • You shouldn't need any code. As long as has_archive is true when registering the post type you will get an archive automatically at /asana/ – Jacob Peattie Commented Jan 6, 2020 at 0:57
Add a comment  | 

1 Answer 1

Reset to default 0

The problem could be related to how you register the custom post type.

Did you add the "taxonomies" argument in the register_post_type? If yes, is it a string? Try to convert it in an array of strings.

See the the example below...

    register_post_type(
        'custom_post_type_slug',
        array(
            'labels'               => array ( /* ... */ ),
            'public'               => true,
            /* ... */
            'taxonomies'           => array( 'custom_post_taxonomy_slug' ),
        )
    );  

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论