自定义发布类型/分类插件/包含发布类型归档的发布标题

时间:2013-04-17 作者:Joshua Richards

我尝试了插件,通读了栈,花了几个小时在这上面,但我似乎找不到一种方法来实现我想要的。

我可以实现以下permalink结构:

custom-post-type/taxonomy-term/post-title

例如our-work/interactive/some-project-title

这也允许our-work/interactive 正确显示我们所有带有分类术语interactive的工作帖子,但我无法再获得归档页面our-work 为了工作,下面是我用来实现上述目标的代码

function my_custom_post_work() {
    $labels = array(
        \'name\'               => _x( \'Work\', \'post type general name\' ),
        \'singular_name\'      => _x( \'Work\', \'post type singular name\' ),
        \'add_new\'            => _x( \'Add New\', \'book\' ),
        \'add_new_item\'       => __( \'Add New Work\' ),
        \'edit_item\'          => __( \'Edit Work\' ),
        \'new_item\'           => __( \'New Work\' ),
        \'all_items\'          => __( \'All Work\' ),
        \'view_item\'          => __( \'View Work\' ),
        \'search_items\'       => __( \'Search Work\' ),
        \'not_found\'          => __( \'No work found\' ),
        \'not_found_in_trash\' => __( \'No work found in the Trash\' ), 
        \'parent_item_colon\'  => \'\',
        \'menu_name\'          => \'Our Work\'
    );
    $args = array(
        \'labels\'        => $labels,
        \'description\'   => \'Holds our works and work specific data\',
        \'public\'        => true,
        \'menu_position\' => 5,
        \'supports\'      => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\' ),
        \'has_archive\'   => true,
        \'rewrite\'       => array( \'slug\' => \'our-work/%work_category%\')
    );
    register_post_type( \'work\', $args );    
}
add_action( \'init\', \'my_custom_post_work\' );

function mav_taxonomies_work() {
    $labels = array(
        \'name\'              => _x( \'Work Categories\', \'taxonomy general name\' ),
        \'singular_name\'     => _x( \'Work Category\', \'taxonomy singular name\' ),
        \'search_items\'      => __( \'Search Work Categories\' ),
        \'all_items\'         => __( \'All Work Categories\' ),
        \'parent_item\'       => __( \'Parent Work Category\' ),
        \'parent_item_colon\' => __( \'Parent Work Category:\' ),
        \'edit_item\'         => __( \'Edit Work Category\' ), 
        \'update_item\'       => __( \'Update Work Category\' ),
        \'add_new_item\'      => __( \'Add New Work Category\' ),
        \'new_item_name\'     => __( \'New Work Category\' ),
        \'menu_name\'         => __( \'Work Categories\' ),
    );
    $args = array(
        \'labels\' => $labels,
        \'hierarchical\' => true,
        \'has_archive\'   => true,
    );
    register_taxonomy( \'work_category\', \'work\', $args );
}
add_action( \'init\', \'mav_taxonomies_work\', 0 );

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

    if ($cats = get_the_terms($post->ID, \'work_category\'))
        $link = str_replace(\'%work_category%\', array_pop($cats)->slug, $link);

    return $link;
}
add_filter(\'post_type_link\', \'filter_post_type_link\', 10, 2);
理想情况下,我希望以下方面能够发挥作用:

  • /our-work/ => 显示所有自定义帖子/our-work/taxonomy-term/ => 显示选中该术语的所有帖子/our-work/taxonomy-term/post-title/ => 单击特定帖子时的永久链接
1 个回复
最合适的回答,由SO网友:dipali 整理而成

将代码替换为以下内容
我对post_type_link 过滤器的回调函数。

function my_custom_post_work() {
    $labels = array(
        \'name\'               => _x( \'Work\', \'post type general name\' ),
        \'singular_name\'      => _x( \'Work\', \'post type singular name\' ),
        \'add_new\'            => _x( \'Add New\', \'book\' ),
        \'add_new_item\'       => __( \'Add New Work\' ),
        \'edit_item\'          => __( \'Edit Work\' ),
        \'new_item\'           => __( \'New Work\' ),
        \'all_items\'          => __( \'All Work\' ),
        \'view_item\'          => __( \'View Work\' ),
        \'search_items\'       => __( \'Search Work\' ),
        \'not_found\'          => __( \'No work found\' ),
        \'not_found_in_trash\' => __( \'No work found in the Trash\' ), 
        \'parent_item_colon\'  => \'\',
        \'menu_name\'          => \'Our Work\'
    );
$args = array(
        \'labels\'        => $labels,
        \'description\'   => \'Holds our works and work specific data\',
        \'public\'        => true,
        \'menu_position\' => 5,
        \'supports\'      => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\' ),
        \'has_archive\'   => true,
        \'rewrite\'       => array( \'slug\' => \'our-work/%work_category%\')
    );
    register_post_type( \'work\', $args );    
}
add_action( \'init\', \'my_custom_post_work\' );

function mav_taxonomies_work() {
    $labels = array(
        \'name\'              => _x( \'Work Categories\', \'taxonomy general name\' ),
        \'singular_name\'     => _x( \'Work Category\', \'taxonomy singular name\' ),
        \'search_items\'      => __( \'Search Work Categories\' ),
        \'all_items\'         => __( \'All Work Categories\' ),
        \'parent_item\'       => __( \'Parent Work Category\' ),
        \'parent_item_colon\' => __( \'Parent Work Category:\' ),
        \'edit_item\'         => __( \'Edit Work Category\' ), 
        \'update_item\'       => __( \'Update Work Category\' ),
        \'add_new_item\'      => __( \'Add New Work Category\' ),
        \'new_item_name\'     => __( \'New Work Category\' ),
        \'menu_name\'         => __( \'Work Categories\' ),
    );
    $args = array(
        \'labels\' => $labels,
        \'hierarchical\' => true,
        \'has_archive\'   => true,
    );
    register_taxonomy( \'work_category\', \'work\', $args );
}
add_action( \'init\', \'mav_taxonomies_work\', 0 );
add_filter(\'post_type_link\', \'filter_post_type_link\', 10, 2);    
function filter_post_type_link( $post_link, $id = 0, $leavename = FALSE ) {
    if ( strpos(\'%work_category%\', $post_link) === \'FALSE\' ) {
      return $post_link;
    }
    $post = get_post($id);
    if ( !is_object($post) || $post->post_type != \'work\' ) {
      return $post_link;
    }
    $terms = wp_get_object_terms($post->ID, \'work_category\');
    if ( !$terms ) {
      return str_replace(\'our-work/%work_category%/\', \'\', $post_link);
    }
    return str_replace(\'%work_category%\', $terms[0]->slug, $post_link);
}
此外,您还必须为自定义帖子类型编写重写规则,以使建议的URI结构正常工作。

将以下代码添加到主题functions.php 文件:

add_filter( \'rewrite_rules_array\',\'my_insert_rewrite_rules\' );
add_action( \'wp_loaded\',\'my_flush_rules\' );    
function my_flush_rules(){
    $rules = get_option( \'rewrite_rules\' );
            global $wp_rewrite;
    $wp_rewrite->flush_rules();
} 

// Adding a new rule    
function my_insert_rewrite_rules( $rules )    
{
    $newrules = array();
    $newrules[\'our-work/?$\'] = \'index.php?post_type=work\';
    $newrules[\'our-work/page/?([0-9]{1,})/?$\'] = \'index.php?post_type=work&paged=$matches[1]\';
    $newrules[\'our-work/(.+?)/page/?([0-9]{1,})/?$\'] = \'index.php?post_type=work&work_category=$matches[1]&paged=$matches[2]\';
    //print_r($rules);
    return $newrules + $rules;
}

结束

相关推荐

自定义发布类型/分类插件/包含发布类型归档的发布标题 - 小码农CODE - 行之有效找到问题解决它

自定义发布类型/分类插件/包含发布类型归档的发布标题

时间:2013-04-17 作者:Joshua Richards

我尝试了插件,通读了栈,花了几个小时在这上面,但我似乎找不到一种方法来实现我想要的。

我可以实现以下permalink结构:

custom-post-type/taxonomy-term/post-title

例如our-work/interactive/some-project-title

这也允许our-work/interactive 正确显示我们所有带有分类术语interactive的工作帖子,但我无法再获得归档页面our-work 为了工作,下面是我用来实现上述目标的代码

function my_custom_post_work() {
    $labels = array(
        \'name\'               => _x( \'Work\', \'post type general name\' ),
        \'singular_name\'      => _x( \'Work\', \'post type singular name\' ),
        \'add_new\'            => _x( \'Add New\', \'book\' ),
        \'add_new_item\'       => __( \'Add New Work\' ),
        \'edit_item\'          => __( \'Edit Work\' ),
        \'new_item\'           => __( \'New Work\' ),
        \'all_items\'          => __( \'All Work\' ),
        \'view_item\'          => __( \'View Work\' ),
        \'search_items\'       => __( \'Search Work\' ),
        \'not_found\'          => __( \'No work found\' ),
        \'not_found_in_trash\' => __( \'No work found in the Trash\' ), 
        \'parent_item_colon\'  => \'\',
        \'menu_name\'          => \'Our Work\'
    );
    $args = array(
        \'labels\'        => $labels,
        \'description\'   => \'Holds our works and work specific data\',
        \'public\'        => true,
        \'menu_position\' => 5,
        \'supports\'      => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\' ),
        \'has_archive\'   => true,
        \'rewrite\'       => array( \'slug\' => \'our-work/%work_category%\')
    );
    register_post_type( \'work\', $args );    
}
add_action( \'init\', \'my_custom_post_work\' );

function mav_taxonomies_work() {
    $labels = array(
        \'name\'              => _x( \'Work Categories\', \'taxonomy general name\' ),
        \'singular_name\'     => _x( \'Work Category\', \'taxonomy singular name\' ),
        \'search_items\'      => __( \'Search Work Categories\' ),
        \'all_items\'         => __( \'All Work Categories\' ),
        \'parent_item\'       => __( \'Parent Work Category\' ),
        \'parent_item_colon\' => __( \'Parent Work Category:\' ),
        \'edit_item\'         => __( \'Edit Work Category\' ), 
        \'update_item\'       => __( \'Update Work Category\' ),
        \'add_new_item\'      => __( \'Add New Work Category\' ),
        \'new_item_name\'     => __( \'New Work Category\' ),
        \'menu_name\'         => __( \'Work Categories\' ),
    );
    $args = array(
        \'labels\' => $labels,
        \'hierarchical\' => true,
        \'has_archive\'   => true,
    );
    register_taxonomy( \'work_category\', \'work\', $args );
}
add_action( \'init\', \'mav_taxonomies_work\', 0 );

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

    if ($cats = get_the_terms($post->ID, \'work_category\'))
        $link = str_replace(\'%work_category%\', array_pop($cats)->slug, $link);

    return $link;
}
add_filter(\'post_type_link\', \'filter_post_type_link\', 10, 2);
理想情况下,我希望以下方面能够发挥作用:

  • /our-work/ => 显示所有自定义帖子/our-work/taxonomy-term/ => 显示选中该术语的所有帖子/our-work/taxonomy-term/post-title/ => 单击特定帖子时的永久链接
1 个回复
最合适的回答,由SO网友:dipali 整理而成

将代码替换为以下内容
我对post_type_link 过滤器的回调函数。

function my_custom_post_work() {
    $labels = array(
        \'name\'               => _x( \'Work\', \'post type general name\' ),
        \'singular_name\'      => _x( \'Work\', \'post type singular name\' ),
        \'add_new\'            => _x( \'Add New\', \'book\' ),
        \'add_new_item\'       => __( \'Add New Work\' ),
        \'edit_item\'          => __( \'Edit Work\' ),
        \'new_item\'           => __( \'New Work\' ),
        \'all_items\'          => __( \'All Work\' ),
        \'view_item\'          => __( \'View Work\' ),
        \'search_items\'       => __( \'Search Work\' ),
        \'not_found\'          => __( \'No work found\' ),
        \'not_found_in_trash\' => __( \'No work found in the Trash\' ), 
        \'parent_item_colon\'  => \'\',
        \'menu_name\'          => \'Our Work\'
    );
$args = array(
        \'labels\'        => $labels,
        \'description\'   => \'Holds our works and work specific data\',
        \'public\'        => true,
        \'menu_position\' => 5,
        \'supports\'      => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\' ),
        \'has_archive\'   => true,
        \'rewrite\'       => array( \'slug\' => \'our-work/%work_category%\')
    );
    register_post_type( \'work\', $args );    
}
add_action( \'init\', \'my_custom_post_work\' );

function mav_taxonomies_work() {
    $labels = array(
        \'name\'              => _x( \'Work Categories\', \'taxonomy general name\' ),
        \'singular_name\'     => _x( \'Work Category\', \'taxonomy singular name\' ),
        \'search_items\'      => __( \'Search Work Categories\' ),
        \'all_items\'         => __( \'All Work Categories\' ),
        \'parent_item\'       => __( \'Parent Work Category\' ),
        \'parent_item_colon\' => __( \'Parent Work Category:\' ),
        \'edit_item\'         => __( \'Edit Work Category\' ), 
        \'update_item\'       => __( \'Update Work Category\' ),
        \'add_new_item\'      => __( \'Add New Work Category\' ),
        \'new_item_name\'     => __( \'New Work Category\' ),
        \'menu_name\'         => __( \'Work Categories\' ),
    );
    $args = array(
        \'labels\' => $labels,
        \'hierarchical\' => true,
        \'has_archive\'   => true,
    );
    register_taxonomy( \'work_category\', \'work\', $args );
}
add_action( \'init\', \'mav_taxonomies_work\', 0 );
add_filter(\'post_type_link\', \'filter_post_type_link\', 10, 2);    
function filter_post_type_link( $post_link, $id = 0, $leavename = FALSE ) {
    if ( strpos(\'%work_category%\', $post_link) === \'FALSE\' ) {
      return $post_link;
    }
    $post = get_post($id);
    if ( !is_object($post) || $post->post_type != \'work\' ) {
      return $post_link;
    }
    $terms = wp_get_object_terms($post->ID, \'work_category\');
    if ( !$terms ) {
      return str_replace(\'our-work/%work_category%/\', \'\', $post_link);
    }
    return str_replace(\'%work_category%\', $terms[0]->slug, $post_link);
}
此外,您还必须为自定义帖子类型编写重写规则,以使建议的URI结构正常工作。

将以下代码添加到主题functions.php 文件:

add_filter( \'rewrite_rules_array\',\'my_insert_rewrite_rules\' );
add_action( \'wp_loaded\',\'my_flush_rules\' );    
function my_flush_rules(){
    $rules = get_option( \'rewrite_rules\' );
            global $wp_rewrite;
    $wp_rewrite->flush_rules();
} 

// Adding a new rule    
function my_insert_rewrite_rules( $rules )    
{
    $newrules = array();
    $newrules[\'our-work/?$\'] = \'index.php?post_type=work\';
    $newrules[\'our-work/page/?([0-9]{1,})/?$\'] = \'index.php?post_type=work&paged=$matches[1]\';
    $newrules[\'our-work/(.+?)/page/?([0-9]{1,})/?$\'] = \'index.php?post_type=work&work_category=$matches[1]&paged=$matches[2]\';
    //print_r($rules);
    return $newrules + $rules;
}

相关推荐