自定义帖子类型未显示在主循环中

时间:2018-05-17 作者:Jason

我已经创建了一个自定义的post类型“courses”,但当我对get\\u post\\u types()进行var\\u转储时,它没有显示出来。这是我的密码。

add_action( \'init\', \'create_post_types\' );
function create_post_types() {
    $courseargs = array(
            \'labels\' => array(
                        \'name\' => \'Courses\',
                        \'singular_name\' => \'Course\',
                       ),
            \'public\'             => true,
            \'publicly_queryable\' => true,
            \'show_ui\'            => true,
            \'show_in_menu\'       => true,
            \'query_var\'          => true,
            \'rewrite\'            => array( \'slug\' => \'courses\' ),
            \'capability_type\'    => \'post\',
            \'taxonomies\'         => array( \'category\' ),
            \'has_archive\'        => true,
            \'hierarchical\'       => false,
            \'menu_position\'      => null,
            \'menu_icon\'          => \'dashicons-welcome-learn-more\',
            \'supports\'           => array( \'title\', \'editor\', \'thumbnail\' )
    );
register_post_type( \'courses\', $courseargs );
}

// add the featured image thumbnail to the admin courses column
add_filter( \'manage_courses_posts_columns\', \'add_img_column\' );
add_action( \'manage_courses_posts_custom_column\' , \'manage_img_column\', 10, 2 );

// size the thumbnail
add_image_size( \'admin-list-thumb\', 80, 80, false );

function add_img_column($columns) {
    $columns = array(
        \'cb\' => \'<input type="checkbox" />\',
        \'title\' => \'Title\',
        \'author\' => \'Author\',
        \'categories\' => \'Categories\',
        \'tags\' => \'Tags\',
        \'comments\' => \'<span class="vers"><div title="Comments" class="comment-grey-bubble"></div></span>\',
        \'featured_thumb\' => \'Thumbnail\',
        \'date\' => \'Date\'
    );
    return $columns;
}

function manage_img_column( $column, $post_id ) {
    switch ( $column ) {
    case \'featured_thumb\':
        echo \'<a href="\' . get_edit_post_link() . \'">\';
        echo the_post_thumbnail( \'admin-list-thumb\' );
        echo \'</a>\';
        break;
    }
}

// Show posts of \'post\', \'page\' and \'courses\' post types on home page
function add_my_post_types_to_query( $query ) {
  if ( is_home() && $query->is_main_query() )
    $query->set( \'post_type\', array( \'post\', \'page\', \'courses\' ) );
  return $query;
}
add_action( \'pre_get_posts\', \'add_my_post_types_to_query\' );

var_dump(get_post_types());
这就是var\\u转储给我的。

array(8) {
  ["post"]=>
  string(4) "post"
  ["page"]=>
  string(4) "page"
  ["attachment"]=>
  string(10) "attachment"
  ["revision"]=>
  string(8) "revision"
  ["nav_menu_item"]=>
  string(13) "nav_menu_item"
  ["custom_css"]=>
  string(10) "custom_css"
  ["customize_changeset"]=>
  string(19) "customize_changeset"
  ["oembed_cache"]=>
  string(12) "oembed_cache"
}
任何帮助都将不胜感激。

1 个回复
最合适的回答,由SO网友:SeventhSteel 整理而成

关于var_dump, “课程”没有出现,因为var_dump 正在任何操作之外运行。因此,它在“init”操作运行之前被触发,这是post类型注册的地方。

关于主循环,我将您的代码粘贴到默认的2017主题函数中。php文件,并通过仪表板添加了一门课程,它显示在我的主页上。

结束