未找到Example.com/Customer-Post-name的页面

时间:2016-03-16 作者:terminator

我已经创建了

  1. custom post type: jobs
  2. custom taxonomy : jobs_division
我想要类似url的自定义永久链接结构:/%自定义帖子类型%/%自定义分类法%/%帖子名称%/

我已经使用以下代码使用pluginand注册了自定义帖子类型,我使用的是215主题

除了i

实例com/作业

应该显示所有帖子

但上面说page not found

这是代码

add_action( \'init\', \'boss_cpt\' );

function boss_cpt() {
  $labels = array(
    \'name\'               => \'Jobs\',
    \'singular_name\'      => \'Job\',
    \'menu_name\'          => \'Jobs\',
    \'name_admin_bar\'     => \'Job\',
    \'add_new\'            => \'Add New\', \'Job\',
    \'add_new_item\'       => \'Add New Job\',
    \'new_item\'           => \'New Job\',
    \'edit_item\'          => \'Edit Job\',
    \'view_item\'          => \'View Job\',
    \'all_items\'          => \'All Jobs\',
    \'search_items\'       => \'Search Jobs\',
    \'parent_item_colon\'  => \'Parent Jobs:\',
    \'not_found\'          => \'No Jobs found.\',
    \'not_found_in_trash\' => \'No Jobs found in Trash.\'
  );

  $args = array(
    \'labels\'             => $labels,
        \'description\'        => __( \'Jobs\' ),
    \'public\'             => true,
    \'publicly_queryable\' => true,
    \'show_ui\'            => true,
    \'show_in_menu\'       => true,
    \'query_var\'          => true,
    \'rewrite\' => array(
    \'slug\' => \'jobs/%jobs_division%\',
    \'with_front\' => true
     ),
    \'capability_type\'    => \'post\',
    \'has_archive\'        => true,
    \'hierarchical\'       => true,
    \'menu_position\'      => 20,
    \'supports\'           => array( \'title\', \'editor\', \'excerpt\', \'trackbacks\', \'custom-fields\', \'comments\', \'revisions\', \'thumbnail\', \'author\', \'page-attributes\' ),
    \'taxonomies\'         => array( \'category\' )
  );

  register_post_type( \'jobs\', $args );

}

add_action( \'init\', \'job_taxonomies\', 0 );

function job_taxonomies() {
    register_taxonomy(
        \'jobs_division\',
        \'jobs\',
        array(
            \'labels\'      => array(
                \'name\'      => \'Job Division\',
                \'add_new_item\'  => \'Add New Job Division\',
                \'new_item_name\' => "New Job Division"
            ),
            \'show_ui\'       => true,
            \'show_tagcloud\'   => false,
            \'hierarchical\'    => true,
            \'public\'      => true,
            \'show_ui\'       => true,
            \'query_var\'     => \'true\',
            \'rewrite\' => array(
    \'slug\' => \'jobs\',
    \'with_front\' => true
  ),
            )
        );
}
/**
 * Tell WordPress how to interpret our jobs URL structure
 *
 * @param array $rules Existing rewrite rules
 * @return array
 */
function so23698827_add_rewrite_rules( $rules ) {
  $new = array();
  $new[\'jobss/([^/]+)/(.+)/?$\'] = \'index.php?jobs=$matches[2]\';
  $new[\'jobss/(.+)/?$\'] = \'index.php?jobs_division=$matches[1]\';

  return array_merge( $new, $rules ); // Ensure our rules come first
}
add_filter( \'rewrite_rules_array\', \'so23698827_add_rewrite_rules\' );

/**
 * Handle the \'%jobs_division%\' URL placeholder
 *
 * @param str $link The link to the post
 * @param WP_Post object $post The post object
 * @return str
 */
function so23698827_filter_post_type_link( $link, $post ) {
  if ( $post->post_type == \'jobs\' ) {
    if ( $cats = get_the_terms( $post->ID, \'jobs_division\' ) ) {
      $link = str_replace( \'%jobs_division%\', current( $cats )->slug, $link );
    }
  }
  return $link;
}
add_filter( \'post_type_link\', \'so23698827_filter_post_type_link\', 10, 2 );
如果你能帮助我,我将非常感激

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

这对我有用

\'has_archive\'=> true,
将其替换为

\'has_archive\'=> \'jobs\',
然后冲洗permalinks

相关推荐