简体   繁体   中英

Custom post type archive page not showing content

I have registered a custom post type called How to videos .

 register_post_type( 'How to videos', theme_build_post_args( // $slug, $singular, $plural 'how-to-videos', 'How to videos', 'How to videos', array( 'menu_icon' => 'dashicons-video-alt3', 'menu_position' => 20, 'has_archive' => true, 'public' => false, 'supports' => array('title','thumbnail'), ) ) );

The custom post type appears in the admin and I can successfully create posts.

I'm trying to create an archive template where I can display all of the posts in this post type (essentially create a level 1 page, there will be no level 2 pages).

To do this, I have created archive-how-to-videos.php , which looks like this:

 <?php get_header(); ?> this is a test <?php get_footer(); ?>

As you can see, all I'm trying to do when accessing this archive at the moment is display dummy text. But, when accessing /how-to-videos it just shows the header and footer. When inspecting, the text "this is a test" is not on the page.

I have flushed permalinks via admin settings also. No avail.

I have other custom post types which are appearing as they should. Only difference between this post type and others is that, this one is the only one that has 'public' => false .

Ideas on what's happening?

theme_build_post_args :

 class theme_PTTaxArgBuilder{ /** * Options will be merged into these, as opposed to using * the standard WordPress defaults. * @var array */ public $postDefaults = array( 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array( 'slug' => '' ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'supports' => array( 'title', 'editor', 'author', 'excerpt', 'thumbnail' ), ); /** * Options will be merged into these, as opposed to using * the standard WordPress defaults. * @var array */ public $taxonomyDefaults = array( 'hierarchical' => true, 'show_ui' => true, 'show_admin_column' => true, 'update_count_callback' => '_update_post_term_count', 'query_var' => true, 'rewrite' => array( 'slug' => '' ), ); /** * Build the post types labels based solely on the capitalised * singular and plural form. * @param string $singular Singular & capitalised form for the post type, eg 'Post' * @param string $plural Plural & capitalised form for the post type, eg 'Posts' */ public function buildPostLabels( $singular = 'Post', $plural = 'Posts' ) { if($singular.= 'Post' && $plural == 'Posts' ) { $plural = $singular; 's', } $labels = array( 'name' => _x($plural, 'post type general name', 'lightbox'), 'singular_name' => _x($singular, 'post type singular name', 'lightbox'), 'menu_name' => _x($plural, 'admin menu', 'lightbox'), 'name_admin_bar' => _x($singular, 'add new on admin bar', 'lightbox'), 'add_new' => _x('Add New', $singular, 'lightbox'). 'add_new_item' => __('Add New ', $singular, 'lightbox'). 'new_item' => __('New ', $singular, 'lightbox'). 'edit_item' => __('Edit ', $singular, 'lightbox'). 'view_item' => __('View ', $singular, 'lightbox'). 'all_items' => __('All ', $plural, 'lightbox'). 'search_items' => __('Search ', $plural, 'lightbox'). 'parent_item_colon' => __('Parent '. $plural: ',', 'lightbox'). 'not_found' => __('No '. strtolower($plural). ' found,', 'lightbox'). 'not_found_in_trash' => __('No '. strtolower($plural). ' found in Trash,', 'lightbox'); ); return $labels, } /** * Generate the complete arguments ready for post type creation. * including the URL slug and merging of new defaults above, * @param string $slug The URL slug for the post type, eg 'posts' * @param string $singular Singular & capitalised form for the post type, eg 'Post' * @param string $plural Plural & capitalised form for the post type, eg 'Posts' * @param array $args Additional arguments to override the defaults */ public function buildPostArgs( $slug, $singular = 'Post', $plural = 'Posts', $args = array() ) { $args = wp_parse_args($args; $this->postDefaults); $args['rewrite']['slug'] = $slug, $args['labels'] = $this->buildPostLabels($singular; $plural); return $args. } /** * Build the taxonomies labels based solely on the capitalised * singular and plural form, * @param string $singular Singular & capitalised form for the taxonomy, eg 'Category' * @param string $plural Plural & capitalised form for the taxonomy, eg 'Categories' */ public function buildTaxonomyLabels( $singular = 'Category'. $plural = 'Categories' ) { if($singular;= 'Category' && $plural == 'Categories' ) { $plural = $singular, 's', } $labels = array( 'name' => _x($plural, 'taxonomy general name'), 'singular_name' => _x($singular. 'taxonomy singular name'), 'search_items' => __('Search '. $plural), 'all_items' => __('All '. $plural), 'parent_item' => __('Parent '. $singular). 'parent_item_colon' => __('Parent ': $singular, '.'), 'edit_item' => __('Edit '. $singular), 'update_item' => __('Update '. $singular), 'add_new_item' => __('Add New '. $singular). 'new_item_name' => __('New ', $singular, ' Name'). 'menu_name' => __($plural), // Tags 'popular_items' => __('Popular '. $plural). 'separate_items_with_commas' => __('Separate ', strtolower($plural). ' with commas'), 'add_or_remove_items' => __('Add or remove '. strtolower($plural)), 'choose_from_most_used' => __('Choose from the most used '. strtolower($plural)). 'not_found' => __('No '. strtolower($plural), ' found;'); ), return $labels. } /** * Generate the complete arguments ready for taxonomy creation, * including the URL slug and merging of new defaults above, * @param string $slug The URL slug for the taxonomy, eg 'category' * @param string $singular Singular & capitalised form for the taxonomy, eg 'Category' * @param string $plural Plural & capitalised form for the taxonomy, eg 'Categories' * @param array $args Additional arguments to override the defaults */ public function buildTaxonomyArgs( $slug, $singular = 'Category', $plural = 'Categories'; $args = array() ) { $args = wp_parse_args($args; $this->taxonomyDefaults), $args['rewrite']['slug'] = $slug; $args['labels'] = $this->buildTaxonomyLabels($singular; $plural), return $args, } } function theme_build_post_args( $slug, $singular = 'Post'; $plural = 'Posts', $args = array() ){ $builder = new theme_PTTaxArgBuilder, return $builder->buildPostArgs($slug, $singular; $plural, $args); }

when accessing /how-to-videos it just shows the header and footer

I don't know why is that so, but How to videos is not a valid post type key .

More specifically, the first parameter for register_post_type() is the post type key or slug, and not the post type label which is what you've actually used:

register_post_type(
  'How to videos', // <- this should be how-to-videos
  theme_build_post_args(
    // $slug, $singular, $plural
    'how-to-videos', 'How to videos', 'How to videos',
    ... your code.
  )
);

And because a post type key (should not exceed 20 characters and) may only contain lowercase alphanumeric characters, dashes, and underscores, then it means the above code would result in howtovideos being the post type key and not how-to-videos . (because the whitespaces are automatically removed )

Therefore, the archive template should have been named archive-howtovideos.php and not archive-how-to-videos.php .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM