WordPress Custom URLs

时间:2014-09-08 作者:James

根据maioman发布的链接,我对以下问题进行了修改:

我很难使用自定义的帖子类型/分类法。我想要一个自定义的帖子类型的剧集,并且自定义帖子类型的分类是包含用户生成的术语的剧集。

我能够

剧集/自定义术语名称/工作。如果你转到这里,它会显示与此术语相关的所有帖子。

然而,如果你在剧集/自定义术语名称/帖子标题上看到一篇帖子,我会看到404页。

function episodes() {

$labels = array(
    \'name\'                => _x( \'Episodes\', \'Post Type General Name\', \'text_domain\' ),
    \'singular_name\'       => _x( \'Episodes\', \'Post Type Singular Name\', \'text_domain\' ),
    \'menu_name\'           => __( \'Episodes\', \'text_domain\' ),
    \'parent_item_colon\'   => __( \'Parent Item:\', \'text_domain\' ),
    \'all_items\'           => __( \'All Episodes\', \'text_domain\' ),
    \'view_item\'           => __( \'View Item\', \'text_domain\' ),
    \'add_new_item\'        => __( \'Add New Item\', \'text_domain\' ),
    \'add_new\'             => __( \'Add new episode\', \'text_domain\' ),
    \'edit_item\'           => __( \'Edit Item\', \'text_domain\' ),
    \'update_item\'         => __( \'Update Item\', \'text_domain\' ),
    \'search_items\'        => __( \'Search Item\', \'text_domain\' ),
    \'not_found\'           => __( \'Not found\', \'text_domain\' ),
    \'not_found_in_trash\'  => __( \'Not found in Trash\', \'text_domain\' )
);
$rewrite = array(
    \'slug\'                => \'episodes/%episode_cat%\',
    \'with_front\'          => false,
    \'pages\'               => true,
    \'feeds\'               => true
);
$args = array(
    \'label\'               => __( \'episodes\', \'text_domain\' ),
    \'description\'         => __( \'All episodes\', \'text_domain\' ),
    \'labels\'              => $labels,
    \'supports\'            => array(\'title\' ),
    \'hierarchical\'        => true,
    \'public\'              => true,
    \'show_ui\'             => true,
    \'show_in_menu\'        => true,
    \'show_in_nav_menus\'   => true,
    \'show_in_admin_bar\'   => true,
    \'menu_position\'       => 5,
    \'can_export\'          => true,
    \'has_archive\'         => true,
    \'exclude_from_search\' => false,
    \'publicly_queryable\'  => true,
    \'rewrite\'             => $rewrite,
    \'capability_type\'     => \'page\',
    \'query_var\'           => true,
    \'_builtin\'            => false
);
register_post_type( \'episodes_listing\', $args );

 }

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

function episodes_taxomony() {

$labels = array(
    \'name\'                       => _x( \'Episodes Categories\', \'Taxonomy General Name\', \'text_domain\' ),
    \'singular_name\'              => _x( \'Episodes\', \'Taxonomy Singular Name\', \'text_domain\' ),
    \'menu_name\'                  => __( \'Episode Categories\', \'text_domain\' ),
    \'all_items\'                  => __( \'All Items\', \'text_domain\' ),
    \'parent_item\'                => __( \'Parent Item\', \'text_domain\' ),
    \'parent_item_colon\'          => __( \'Parent Item:\', \'text_domain\' ),
    \'new_item_name\'              => __( \'New Item Name\', \'text_domain\' ),
    \'add_new_item\'               => __( \'Add new episode\', \'text_domain\' ),
    \'edit_item\'                  => __( \'Edit Item\', \'text_domain\' ),
    \'update_item\'                => __( \'Update Item\', \'text_domain\' ),
    \'separate_items_with_commas\' => __( \'Separate items with commas\', \'text_domain\' ),
    \'search_items\'               => __( \'Search Items\', \'text_domain\' ),
    \'add_or_remove_items\'        => __( \'Add or remove items\', \'text_domain\' ),
    \'choose_from_most_used\'      => __( \'Choose from the most used items\', \'text_domain\' ),
    \'not_found\'                  => __( \'Not Found\', \'text_domain\' )
);
$rewrite = array(
    \'slug\'                       => \'episodes\',
    \'with_front\'                 => false,
    \'hierarchical\'               => true

);
$args = array(
    \'labels\'                     => $labels,
    \'hierarchical\'               => true,
    \'public\'                     => true,
    \'show_ui\'                    => true,
    \'show_admin_column\'          => true,
    \'show_in_nav_menus\'          => true,
    \'show_tagcloud\'              => true,
    \'query_var\'                  => true,
    \'rewrite\'                    => $rewrite
);
register_taxonomy( \'episodes_category\', array(\'episodes_listing\'), $args );

}


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

function filter_post_type_link($link, $post)
{
if ($post->post_type != \'episodes_listing\')
    return $link;

if ($cats = get_the_terms($post->ID, \'episodes_category\')) {

    //echo \'This is the \'. $link;
    $link = str_replace(\'%episode_cat%\', array_pop($cats)->slug, $link);
return $link;
}
}
add_filter(\'post_type_link\', \'filter_post_type_link\', 10, 2);
为了解决上述问题,我加入了

add_filter(\'rewrite_rules_array\', \'mmp_rewrite_rules\');
 function mmp_rewrite_rules($rules) {
  $newRules  = array();
  $newRules[\'episodes/(.+)/(.+)/(.+)/(.+)/?$\'] = \'index.php?custom_post_type_name=$matches[3]\'; // my custom structure will always have the post name as the 5th uri segment
$newRules[\'episodes/(.+)/?$\']                = \'index.php?taxonomy_name=$matches[1]\'; 

return array_merge($newRules, $rules);
}
但现在/剧集/自定义术语名称将我带到主页/剧集/自定义术语名称/帖子标题将我带到主页

所以,什么都不管用。

我已尝试更改$matches数组编号。

注意:在我的WP阅读设置中,我将其设置为静态页面。当我调用mmp\\u rewrite\\u rules函数时,会显示这个静态页面。

2 个回复
SO网友:vancoder

你不能这样做,至少不能以任何非黑客的方式。您基本上希望“剧集”同时表示两个不同的事物,但WP无法知道哪个是针对任何给定路径的。

我想你可以在你的CPT单帖模板的顶部放置一些逻辑,以检查“帖名”是否是第一季、第二季等,如果是,则相应地进行处理。但它很乱。

SO网友:James

这篇文章的答案在这里Custom Taxonomy specific to a Custom Post type

我需要改变

\'has_archive\'=> \'true\'

\'has_archive\' => \'episodes\'
并将capability\\u type设置为post而不是page。

谢谢大家的指点!

结束

相关推荐

Custom permalinks structure

我希望有这样的结构:www.mysite.com/2013 (必须显示2013年的所有职位)www.mysite.com/my-category/2013 (必须显示2013年和“我的类别”类别的所有帖子)www.mysite.com/my-category/my-tag/ (必须显示所有类别为“我的类别”和标记为“我的标记”的帖子)www.mysite.com/my-category/ (必须显示“我的类别”类别的所有帖子)www.mysite.com/my-