【问题标题】:Wordpress Custom Post Type Archive Not LoadingWordPress 自定义帖子类型存档未加载
【发布时间】:2013-12-28 09:28:09
【问题描述】:

我在 WordPress 3.7.1 中设置了自定义帖子类型“游戏”。一切运行良好,我已经创建了这个帖子类型的几个项目,并为它们创建/分配了类别。显示此 CPT 帖子的查询正常工作,我的自定义单页 (single-game.php) 也可以正常工作。

但是,当我创建archive-game.php 时,WordPress 会加载默认的archive.php 文件并且分页不起作用。我已经搜索了几个小时,并尝试了一些解决方案无济于事。我正在尝试获取我在此 CPT 下创建的每个类别的存档,我做得对吗?最新版本的 WordPress 中是否发生了可能影响这一点的变化?

这是我创建自定义帖子类型的代码:

public function gv_game_setup_post_types() {
    $game_labels = array(
        'name'                => 'Games',
        'singular_name'       => 'Game',
        'add_new'             => __('Add New', 'game'),
        'add_new_item'        => __('Add New Game', 'game'),
        'edit_item'           => __('Edit Game', 'game'),
        'new_item'            => __('New Game', 'game'),
        'all_items'           => __('All Games', 'game'),
        'view_item'           => __('View Game', 'game'),
        'search_items'        => __('Search Games', 'game'),
        'not_found'           => __('No Games found', 'game'),
        'not_found_in_trash'  => __('No Games found in Trash', 'game'),
        'parent_item_colon'   => '',
        'menu_name'           => __('Games', 'game'),
        'exclude_from_search' => false
    );

    $game_args = array(
        'labels'              => $game_labels,
        'public'              => true,
        'has_archive'         => true,
        'publicly_queryable'  => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'query_var'           => true,
        'capability_type'     => 'post',
        'hierarchical'        => false,
        'supports'            => array('editor', 'title', 'thumbnail', 'custom-fields'),
        'taxonomies'          => array('category', 'post_tag'),
        'rewrite'             => array('slug' => 'game', 'with_front' => false)
    );

    register_post_type('game', $game_args);
}

此函数是我为此目的制作的自定义插件的一部分,并在“init”钩子的构造函数中被调用。

感谢任何帮助。

谢谢!

【问题讨论】:

  • 当您说“分页不起作用”时,请解释它是如何不起作用的。
  • 存档页面上的分页返回 404 错误。

标签: php wordpress archive custom-post-type


【解决方案1】:

我使用这个插件来创建我的自定义帖子类型,一旦它启动并工作,有一个选项可以导出 php 代码,然后将其添加到函数文件中,然后我禁用插件。对我来说,这是确保它正常工作的最安全方法。当然,在进行任何更改后,您应该始终访问管理中的永久链接页面。

http://wordpress.org/plugins/custom-post-type-ui/

如果您正确创建它,您可以创建一个自定义帖子类型存档文件archive-games.php(这将是您的自定义帖子类型名称,例如,在您的函数文件中更像这样的名称,

add_action('init', 'cptui_register_my_cpt_game');
function cptui_register_my_cpt_game() {
register_post_type('game', array(
'label' => 'Games',
'description' => '',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'game', 'with_front' => true),
'query_var' => true,
'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes','post-formats'),
'taxonomies' => array('category','post_tag'),
'labels' => array (
'name' => 'Games',
'singular_name' => '',
'menu_name' => 'Games',
'add_new' => 'Add Game',
'add_new_item' => 'Add New Game',
'edit' => 'Edit',
'edit_item' => 'Edit Game',
'new_item' => 'New Game',
'view' => 'View Game',
'view_item' => 'View Game',
'search_items' => 'Search Game',
'not_found' => 'No Game Found',
'not_found_in_trash' => 'No Game Found in Trash',
'parent' => 'Parent Game',
)
) ); }

当然,请确保您没有任何现有页面与 slug 游戏

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 2013-06-02
    相关资源
    最近更新 更多