自定义分类的自定义POST类型日期归档

时间:2018-02-12 作者:Jose

您好,WP StackExchange,

我拿起它,它处理定制的Post-Type归档。

所以我们有一个叫做资源的CPT。在资源下,我们有一个称为资源类型的自定义分类法。对于资源类型,我们有媒体、画廊、博客和常见问题解答。

所以每个人都有自己的permalink:

/资源/媒体/

/资源/博客/

你可能想知道,为什么要把博客文章放在参考资料下,这是一个好问题,基于最近的开发人员笔记,而营销团队想要的是,他们希望博客具有永久链接结构。使用常规贴子类型时,在贴子的永久链接设置中添加基时会出现一些冲突问题。2-3个其他开发人员已经接触到了这一点,我希望我能完成它。这条路线不是我所知道的最好的路线,但它是我正在使用的:/感谢您的帮助。

我想修复的是博客的档案。所以如果我们点击/resources/blog/2012/02,我们会得到一个404。我一直在寻找,但找不到一种方法来实现这一目标,也找不到是否可以为我指明正确的方向。我尝试了一些归档模板,但似乎什么都不适合我。

还有一些快速的背景故事,我们在帖子和资源下有重复的帖子。因此,如果你点击/2012/02,你会从帖子中获得反馈。我们将删除帖子下的帖子,这样资源将是博客的唯一方法。

编辑:CPT和分类的代码

// Resource
function cpt_pt_resources() {

$labels = array(
    \'name\'                  => _x( \'Resources\', \'Post Type General Name\', \'text_domain\' ),
    \'singular_name\'         => _x( \'Resource\', \'Post Type Singular Name\', \'text_domain\' ),
    \'menu_name\'             => __( \'Resources\', \'text_domain\' ),
    \'name_admin_bar\'        => __( \'Resources\', \'text_domain\' ),
    \'archives\'              => __( \'Resource Archives\', \'text_domain\' ),
    \'attributes\'            => __( \'Resource Attributes\', \'text_domain\' ),
    \'parent_item_colon\'     => __( \'Parent Item:\', \'text_domain\' ),
    \'all_items\'             => __( \'All Items\', \'text_domain\' ),
    \'add_new_item\'          => __( \'Add New Resource\', \'text_domain\' ),
    \'add_new\'               => __( \'Add Resource\', \'text_domain\' ),
    \'new_item\'              => __( \'New Item\', \'text_domain\' ),
    \'edit_item\'             => __( \'Edit Item\', \'text_domain\' ),
    \'update_item\'           => __( \'Update Item\', \'text_domain\' ),
    \'view_item\'             => __( \'View Item\', \'text_domain\' ),
    \'view_items\'            => __( \'View Items\', \'text_domain\' ),
    \'search_items\'          => __( \'Search Item\', \'text_domain\' ),
    \'not_found\'             => __( \'Not found\', \'text_domain\' ),
    \'not_found_in_trash\'    => __( \'Not found in Trash\', \'text_domain\' ),
    \'featured_image\'        => __( \'Featured Image\', \'text_domain\' ),
    \'set_featured_image\'    => __( \'Set featured image\', \'text_domain\' ),
    \'remove_featured_image\' => __( \'Remove featured image\', \'text_domain\' ),
    \'use_featured_image\'    => __( \'Use as featured image\', \'text_domain\' ),
    \'insert_into_item\'      => __( \'Insert into item\', \'text_domain\' ),
    \'uploaded_to_this_item\' => __( \'Uploaded to this item\', \'text_domain\' ),
    \'items_list\'            => __( \'Items list\', \'text_domain\' ),
    \'items_list_navigation\' => __( \'Items list navigation\', \'text_domain\' ),
    \'filter_items_list\'     => __( \'Filter items list\', \'text_domain\' ),
);
$args = array(
    \'label\'                 => __( \'Resource\', \'text_domain\' ),
    \'description\'           => __( \'Resources\', \'text_domain\' ),
    \'labels\'                => $labels,
    \'supports\'              => array( \'title\', \'editor\', \'excerpt\', \'thumbnail\', \'revisions\', \'page-attributes\', \'post-formats\', \'custom-fields\', ),
    \'taxonomies\'            => array( \'category\', \'post_tag\', ),
    \'hierarchical\'          => true,
    \'public\'                => true,
    \'show_ui\'               => true,
    \'show_in_menu\'          => true,
    \'menu_position\'         => 5,
    \'menu_icon\'             => \'dashicons-index-card\',
    \'show_in_admin_bar\'     => false,
    \'show_in_nav_menus\'     => true,
    \'can_export\'            => true,
    \'has_archive\'           => true,        
    \'exclude_from_search\'   => false,
    \'publicly_queryable\'    => true,
    \'capability_type\'       => \'page\',
    \'rewrite\'               => array( \'slug\' => \'resources/%resource-type%\', \'with_front\' => false ),
);
register_post_type( \'resource\', $args );

}
add_action( \'init\', \'cpt_pt_resources\', 0 );

// Resource URL rewrite
function res_post_link( $post_link, $id = 0 ){
$post = get_post($id);  
if ( is_object( $post ) ){
    $terms = wp_get_object_terms( $post->ID, \'resource-type\' );
    if( $terms ){
        return str_replace( \'%resource-type%\' , $terms[0]->slug , $post_link );
    }
}
return $post_link;  
}
add_filter( \'post_type_link\', \'res_post_link\', 1, 3 );

// Custom taxonomy Resource Type
function cpt_pt_resource_tax() {

$labels = array(
    \'name\'              => _x( \'Resource Types\', \'taxonomy general name\', \'textdomain\' ),
    \'singular_name\'     => _x( \'Resource Type\', \'taxonomy singular name\', \'textdomain\' ),
    \'menu_name\'         => __( \'Resource Types\', \'textdomain\' ),
    \'search_items\'      => __( \'Search Resource Types\', \'textdomain\' ),
    \'all_items\'         => __( \'All Resource Types\', \'textdomain\' ),
    \'parent_item\'       => __( \'Parent Resource Type\', \'textdomain\' ),
    \'parent_item_colon\' => __( \'Parent Resource Type:\', \'textdomain\' ),
    \'edit_item\'         => __( \'Edit Resource Type\', \'textdomain\' ),
    \'update_item\'       => __( \'Update Resource Type\', \'textdomain\' ),
    \'add_new_item\'      => __( \'Add New Resource Type\', \'textdomain\' ),
    \'new_item_name\'     => __( \'New Resource Type Name\', \'textdomain\' ),    
);

$args = array(
    \'hierarchical\'      => true,
    \'labels\'            => $labels,
    \'show_ui\'           => true,
    \'show_admin_column\' => true,
    \'query_var\'         => true,
    \'menu_position\'     => 0,
    \'rewrite\'           => array( \'slug\' => \'resources\', \'with_front\' => false ),
);
register_taxonomy( \'resource-type\', array( \'resource\' ),  $args );

}
add_action( \'init\', \'cpt_pt_resource_tax\', 0 );

// Custom URL for FAQs
function faq_post_link( $post_link, $id = 0 ){
$post = get_post($id);  
if ( is_tax( \'resource-type\', \'faq\') ){
    $terms = wp_get_object_terms( $post->ID, \'faqs\' );
    if( $terms ){
        return str_replace( \'%resource-type%\' , $terms[0]->slug , $post_link );
    }
}
    return $post_link;  
}
add_filter( \'post_type_link\', \'faq_post_link\', 1, 3 );

1 个回复
SO网友:Milo

关于当前代码的几件事-

您可能会发现分类术语页面上的分页当前不起作用。在这里注册分类法和post类型的顺序很重要,因为规则非常相似,需要它们以特定的方式级联。您可以通过将稍后的优先级赋予post类型注册来颠倒它们发生的顺序add_action( \'init\', \'cpt_pt_resources\', 1 );

faq_post_link 连接到的函数post_type_link 最后是多余的。这个res_post_link 连接到同一过滤器的函数已经执行了相同的操作。

启用日期存档非常简单。您需要添加一组重写规则,这些规则是正则表达式形式的URL模式,加上捕获的值放入的相应查询变量。

function cpt_pt_resource_tax_date_archives() {

    add_rewrite_rule(
        \'resources/([^/]+)/([0-9]{4})/?$\',
        \'index.php?resource-type=$matches[1]&year=$matches[2]\',
        \'top\'
    );
    add_rewrite_rule(
        \'resources/([^/]+)/([0-9]{4})/page/([0-9]{1,})/?$\',
        \'index.php?resource-type=$matches[1]&year=$matches[2]&paged=$matches[3]\',
        \'top\'
    );

    add_rewrite_rule(
        \'resources/([^/]+)/([0-9]{4})/([0-9]{2})/?$\',
        \'index.php?resource-type=$matches[1]&year=$matches[2]&monthnum=$matches[3]\',
        \'top\'
    );
    add_rewrite_rule(
        \'resources/([^/]+)/([0-9]{4})/([0-9]{2})/page/([0-9]{1,})/?$\',
        \'index.php?resource-type=$matches[1]&year=$matches[2]&monthnum=$matches[3]&paged=$matches[4]\',
        \'top\'
    );

}
add_action( \'init\', \'cpt_pt_resource_tax_date_archives\', 2 );
这里有两套规则-一套用于仅年份存档,加上分页,另一套用于年/月存档,加上分页。

不要忘记在任何更改后刷新重写规则。您可以通过访问Settings > Permalinks 管理页面,该页面调用flush_rewrite_rules 作用

如果您想添加/更改规则,或者只是想更好地了解它们的工作方式,我建议 Monkeyman Rewrite Analyzer 插件。

结束

相关推荐

自定义分类的自定义POST类型日期归档 - 小码农CODE - 行之有效找到问题解决它

自定义分类的自定义POST类型日期归档

时间:2018-02-12 作者:Jose

您好,WP StackExchange,

我拿起它,它处理定制的Post-Type归档。

所以我们有一个叫做资源的CPT。在资源下,我们有一个称为资源类型的自定义分类法。对于资源类型,我们有媒体、画廊、博客和常见问题解答。

所以每个人都有自己的permalink:

/资源/媒体/

/资源/博客/

你可能想知道,为什么要把博客文章放在参考资料下,这是一个好问题,基于最近的开发人员笔记,而营销团队想要的是,他们希望博客具有永久链接结构。使用常规贴子类型时,在贴子的永久链接设置中添加基时会出现一些冲突问题。2-3个其他开发人员已经接触到了这一点,我希望我能完成它。这条路线不是我所知道的最好的路线,但它是我正在使用的:/感谢您的帮助。

我想修复的是博客的档案。所以如果我们点击/resources/blog/2012/02,我们会得到一个404。我一直在寻找,但找不到一种方法来实现这一目标,也找不到是否可以为我指明正确的方向。我尝试了一些归档模板,但似乎什么都不适合我。

还有一些快速的背景故事,我们在帖子和资源下有重复的帖子。因此,如果你点击/2012/02,你会从帖子中获得反馈。我们将删除帖子下的帖子,这样资源将是博客的唯一方法。

编辑:CPT和分类的代码

// Resource
function cpt_pt_resources() {

$labels = array(
    \'name\'                  => _x( \'Resources\', \'Post Type General Name\', \'text_domain\' ),
    \'singular_name\'         => _x( \'Resource\', \'Post Type Singular Name\', \'text_domain\' ),
    \'menu_name\'             => __( \'Resources\', \'text_domain\' ),
    \'name_admin_bar\'        => __( \'Resources\', \'text_domain\' ),
    \'archives\'              => __( \'Resource Archives\', \'text_domain\' ),
    \'attributes\'            => __( \'Resource Attributes\', \'text_domain\' ),
    \'parent_item_colon\'     => __( \'Parent Item:\', \'text_domain\' ),
    \'all_items\'             => __( \'All Items\', \'text_domain\' ),
    \'add_new_item\'          => __( \'Add New Resource\', \'text_domain\' ),
    \'add_new\'               => __( \'Add Resource\', \'text_domain\' ),
    \'new_item\'              => __( \'New Item\', \'text_domain\' ),
    \'edit_item\'             => __( \'Edit Item\', \'text_domain\' ),
    \'update_item\'           => __( \'Update Item\', \'text_domain\' ),
    \'view_item\'             => __( \'View Item\', \'text_domain\' ),
    \'view_items\'            => __( \'View Items\', \'text_domain\' ),
    \'search_items\'          => __( \'Search Item\', \'text_domain\' ),
    \'not_found\'             => __( \'Not found\', \'text_domain\' ),
    \'not_found_in_trash\'    => __( \'Not found in Trash\', \'text_domain\' ),
    \'featured_image\'        => __( \'Featured Image\', \'text_domain\' ),
    \'set_featured_image\'    => __( \'Set featured image\', \'text_domain\' ),
    \'remove_featured_image\' => __( \'Remove featured image\', \'text_domain\' ),
    \'use_featured_image\'    => __( \'Use as featured image\', \'text_domain\' ),
    \'insert_into_item\'      => __( \'Insert into item\', \'text_domain\' ),
    \'uploaded_to_this_item\' => __( \'Uploaded to this item\', \'text_domain\' ),
    \'items_list\'            => __( \'Items list\', \'text_domain\' ),
    \'items_list_navigation\' => __( \'Items list navigation\', \'text_domain\' ),
    \'filter_items_list\'     => __( \'Filter items list\', \'text_domain\' ),
);
$args = array(
    \'label\'                 => __( \'Resource\', \'text_domain\' ),
    \'description\'           => __( \'Resources\', \'text_domain\' ),
    \'labels\'                => $labels,
    \'supports\'              => array( \'title\', \'editor\', \'excerpt\', \'thumbnail\', \'revisions\', \'page-attributes\', \'post-formats\', \'custom-fields\', ),
    \'taxonomies\'            => array( \'category\', \'post_tag\', ),
    \'hierarchical\'          => true,
    \'public\'                => true,
    \'show_ui\'               => true,
    \'show_in_menu\'          => true,
    \'menu_position\'         => 5,
    \'menu_icon\'             => \'dashicons-index-card\',
    \'show_in_admin_bar\'     => false,
    \'show_in_nav_menus\'     => true,
    \'can_export\'            => true,
    \'has_archive\'           => true,        
    \'exclude_from_search\'   => false,
    \'publicly_queryable\'    => true,
    \'capability_type\'       => \'page\',
    \'rewrite\'               => array( \'slug\' => \'resources/%resource-type%\', \'with_front\' => false ),
);
register_post_type( \'resource\', $args );

}
add_action( \'init\', \'cpt_pt_resources\', 0 );

// Resource URL rewrite
function res_post_link( $post_link, $id = 0 ){
$post = get_post($id);  
if ( is_object( $post ) ){
    $terms = wp_get_object_terms( $post->ID, \'resource-type\' );
    if( $terms ){
        return str_replace( \'%resource-type%\' , $terms[0]->slug , $post_link );
    }
}
return $post_link;  
}
add_filter( \'post_type_link\', \'res_post_link\', 1, 3 );

// Custom taxonomy Resource Type
function cpt_pt_resource_tax() {

$labels = array(
    \'name\'              => _x( \'Resource Types\', \'taxonomy general name\', \'textdomain\' ),
    \'singular_name\'     => _x( \'Resource Type\', \'taxonomy singular name\', \'textdomain\' ),
    \'menu_name\'         => __( \'Resource Types\', \'textdomain\' ),
    \'search_items\'      => __( \'Search Resource Types\', \'textdomain\' ),
    \'all_items\'         => __( \'All Resource Types\', \'textdomain\' ),
    \'parent_item\'       => __( \'Parent Resource Type\', \'textdomain\' ),
    \'parent_item_colon\' => __( \'Parent Resource Type:\', \'textdomain\' ),
    \'edit_item\'         => __( \'Edit Resource Type\', \'textdomain\' ),
    \'update_item\'       => __( \'Update Resource Type\', \'textdomain\' ),
    \'add_new_item\'      => __( \'Add New Resource Type\', \'textdomain\' ),
    \'new_item_name\'     => __( \'New Resource Type Name\', \'textdomain\' ),    
);

$args = array(
    \'hierarchical\'      => true,
    \'labels\'            => $labels,
    \'show_ui\'           => true,
    \'show_admin_column\' => true,
    \'query_var\'         => true,
    \'menu_position\'     => 0,
    \'rewrite\'           => array( \'slug\' => \'resources\', \'with_front\' => false ),
);
register_taxonomy( \'resource-type\', array( \'resource\' ),  $args );

}
add_action( \'init\', \'cpt_pt_resource_tax\', 0 );

// Custom URL for FAQs
function faq_post_link( $post_link, $id = 0 ){
$post = get_post($id);  
if ( is_tax( \'resource-type\', \'faq\') ){
    $terms = wp_get_object_terms( $post->ID, \'faqs\' );
    if( $terms ){
        return str_replace( \'%resource-type%\' , $terms[0]->slug , $post_link );
    }
}
    return $post_link;  
}
add_filter( \'post_type_link\', \'faq_post_link\', 1, 3 );

1 个回复
SO网友:Milo

关于当前代码的几件事-

您可能会发现分类术语页面上的分页当前不起作用。在这里注册分类法和post类型的顺序很重要,因为规则非常相似,需要它们以特定的方式级联。您可以通过将稍后的优先级赋予post类型注册来颠倒它们发生的顺序add_action( \'init\', \'cpt_pt_resources\', 1 );

faq_post_link 连接到的函数post_type_link 最后是多余的。这个res_post_link 连接到同一过滤器的函数已经执行了相同的操作。

启用日期存档非常简单。您需要添加一组重写规则,这些规则是正则表达式形式的URL模式,加上捕获的值放入的相应查询变量。

function cpt_pt_resource_tax_date_archives() {

    add_rewrite_rule(
        \'resources/([^/]+)/([0-9]{4})/?$\',
        \'index.php?resource-type=$matches[1]&year=$matches[2]\',
        \'top\'
    );
    add_rewrite_rule(
        \'resources/([^/]+)/([0-9]{4})/page/([0-9]{1,})/?$\',
        \'index.php?resource-type=$matches[1]&year=$matches[2]&paged=$matches[3]\',
        \'top\'
    );

    add_rewrite_rule(
        \'resources/([^/]+)/([0-9]{4})/([0-9]{2})/?$\',
        \'index.php?resource-type=$matches[1]&year=$matches[2]&monthnum=$matches[3]\',
        \'top\'
    );
    add_rewrite_rule(
        \'resources/([^/]+)/([0-9]{4})/([0-9]{2})/page/([0-9]{1,})/?$\',
        \'index.php?resource-type=$matches[1]&year=$matches[2]&monthnum=$matches[3]&paged=$matches[4]\',
        \'top\'
    );

}
add_action( \'init\', \'cpt_pt_resource_tax_date_archives\', 2 );
这里有两套规则-一套用于仅年份存档,加上分页,另一套用于年/月存档,加上分页。

不要忘记在任何更改后刷新重写规则。您可以通过访问Settings > Permalinks 管理页面,该页面调用flush_rewrite_rules 作用

如果您想添加/更改规则,或者只是想更好地了解它们的工作方式,我建议 Monkeyman Rewrite Analyzer 插件。

相关推荐