【问题标题】:Using a Wordpress Page as Base for Custom Post Type Permalinks使用 Wordpress 页面作为自定义帖子类型永久链接的基础
【发布时间】:2014-05-14 09:45:59
【问题描述】:

所以我正在尝试在 Wordpress 中设置一个具有以下永久链接结构的网站:

http://www.site.com/trades/trade-type

我希望交易成为一个实际页面,并且我创建了交易类型作为自定义帖子类型。

在我的交易类型自定义帖子类型中,我将 slug 设置为“交易”以使用该永久链接作为我的基本结构。所以我在自定义帖子类型下创建的所有帖子都应该继续 /trades URL。

当我设置这个 slug 时,我能够成功地保存和查看 /trades/trade-type 页面上的页面。但是当我尝试转到 /trades 页面时,它就崩溃了。

当我将自定义帖子类型中的 slug 更改为“trades2”时,我能够成功查看 /trades 页面,但是我创建的所有交易类型现在都在 trades2/trade-type 下。

/trades 页面和自定义帖子类型 slug 之间似乎存在某种冲突。有没有办法可以编辑 functions.php 来适应两者?任何帮助将不胜感激。

下面是我的交易类型自定义帖子类型的functions.php

//Register Custom Trades Post Type
function custom_trades() {

$labels = array(
    'name'                => _x( 'Trades', 'Post Type General Name', 'text_domain' ),
    'singular_name'       => _x( 'Trade', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'           => __( 'Trades', 'text_domain' ),
    'parent_item_colon'   => __( 'Parent Trade:', 'text_domain' ),
    'all_items'           => __( 'All Trades', 'text_domain' ),
    'view_item'           => __( 'View Trade', 'text_domain' ),
    'add_new_item'        => __( 'Add New Trade', 'text_domain' ),
    'add_new'             => __( 'Add New', 'text_domain' ),
    'edit_item'           => __( 'Edit Trade', 'text_domain' ),
    'update_item'         => __( 'Update Trade', 'text_domain' ),
    'search_items'        => __( 'Search Trade', 'text_domain' ),
    'not_found'           => __( 'Not found', 'text_domain' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
);
$rewrite = array(
    'slug'                => 'trades',
    'with_front'          => true,
    'pages'               => true,
    'feeds'               => true,
);
$args = array(
    'label'               => __( 'ds_trades', 'text_domain' ),
    'description'         => __( 'Post Type Description', 'text_domain' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', 'page-attributes', ),
    'taxonomies'          => array( 'category' ),
    'hierarchical'        => true,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'menu_icon'           => 'dashicons-admin-generic',
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'rewrite'             => $rewrite,
    'capability_type'     => 'page',
);
register_post_type( 'ds_trades', $args );

}

// Hook into the 'init' action
add_action( 'init', 'custom_trades', 0 );

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    你在问为什么它吓坏了 domain.com/trades 但是页面 domain.com/trades/trade-article。正在工作。

    我确定 WordPress 此刻正在搜索一个名为 archive-trades.php 在您的主题文件夹中。

    在此处找到详细说明 http://www.wpbeginner.com/wp-tutorials/how-to-create-a-custom-post-types-archive-page-in-wordpress/

    【讨论】:

    • 我没有意识到 has_archieve 需要为 false 才能使其工作。这就是我所需要的。谢谢先生。
    猜你喜欢
    • 2013-09-10
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多