自定义帖子类型的WordPress自定义固定链接和存档链接

时间:2016-10-04 作者:Wimal Weerawansa

我有自定义的帖子类型venue 以及自定义分类法“设施”和“类型”,可以使用当前的posts分类法slug为单个post和归档生成自定义永久链接。示例:


单岗位任期facility slug是“餐厅”

http://mywebsite.com/restaurant/restaurant-name


存档页面

http://mywebsite.com/restaurants


我的自定义帖子声明

$args = array(
  \'label\'                 => __( \'Venue\', \'weera\' ),
  \'description\'           => __( \'Dubai day Venue listings\', \'weera\' ),
  \'labels\'                => $labels,
  \'supports\'              => array( \'title\', \'excerpt\', \'author\', \'thumbnail\', \'revisions\'),
  \'hierarchical\'          => false,
  \'public\'                => true,
  \'show_ui\'               => true,
  \'show_in_menu\'          => true,
  \'menu_position\'         => 5,
  \'menu_icon\'             => \'dashicons-palmtree\',
  \'show_in_admin_bar\'     => true,
  \'show_in_nav_menus\'     => true,
  \'can_export\'            => true,
  \'has_archive\'           => true,
  \'exclude_from_search\'   => false,
  \'publicly_queryable\'    => true,
  \'capability_type\'       => \'page\',
  \'rewrite\' => array( \'slug\' => \'restaurant\'),
  \'has_archive\' => true,
);
register_post_type( \'venue\', $args );
我的分类“设施”声明

$args = array(
  \'labels\'                     => $labels,
  \'hierarchical\'               => true,
  \'public\'                     => true,
  \'show_ui\'                    => true,
  \'show_admin_column\'          => true,
  \'show_in_nav_menus\'          => true,
  \'show_tagcloud\'              => true,
  \'rewrite\' => array( \'slug\' => \'venue\' ),
);

register_taxonomy( \'facility\', array( \'venue\' ), $args );
但是http://mywebsite.com/restaurantshttp://mywebsite.com/restaurant/restaurant-name 两者都有404页。

1 个回复
SO网友:sMyles

你要么进入Settings -> Permalinks 然后单击“保存”

或者,如果以编程方式执行此操作,则需要使用flush_rewrite_rules(), 但请确保仅在激活/停用插件时使用此选项,不要在运行的代码中设置此选项(如在init中,或在每次加载页面时):

https://developer.wordpress.org/reference/functions/flush_rewrite_rules/

register_deactivation_hook( __FILE__, \'flush_rewrite_rules\' );
register_activation_hook( __FILE__, \'wdocs_flush_rewrites\' );


/**
 * Flush rewrite rules on activation
 */
function wpdocs_flush_rewrites() {
    // call your CPT registration function here (it should also be hooked into \'init\')
    wpdocs_custom_post_types_registration();
    flush_rewrite_rules();
}

相关推荐

Force pretty permalinks?

我正在构建一个插件,该插件将用于单个站点,并依赖于add_rewrite_rule 要工作,需要打开永久链接。打开它们并不困难,因为它只是一个站点,但我担心其中一个管理员可能会在不知道自己在做什么的情况下关闭它,并破坏该站点。如何以编程方式强制保持漂亮的永久链接?