URL重写的自定义帖子类型和自定义分类起作用,但模板不起作用

时间:2014-12-20 作者:user4517

我搜索了很多,但没有找到,只是得到了部分答案,我声明了自定义的post类型“store”,分类法为“city”,我尝试的是根据city列表创建店铺列表,并创建类似于post type=store的URL<posttype slug><taxonomy term>

。com/store/tokyo/my\\u store\\u name

我是如何使用此代码实现它的。Custom Post Type / Taxonomy Slug / Post Title with post type archive但现在如何使用自定义模板,我尝试了“archive-city.php、single-store.php、taxonomy-city.php,以及一些更标准的组合,但都是404。

1 个回复
SO网友:user4517

function my_custom_post_work() {
    $labels = array(
        \'name\'               => _x( \'Store\', \'post type general name\' ),
        \'singular_name\'      => _x( \'Store\', \'post type singular name\' ),
        \'add_new\'            => _x( \'Add New\', \'Store\' ),
        \'add_new_item\'       => __( \'Add New Store\' ),
        \'edit_item\'          => __( \'Edit Store\' ),
        \'new_item\'           => __( \'New Store\' ),
        \'all_items\'          => __( \'All Store\' ),
        \'view_item\'          => __( \'View Store\' ),
        \'search_items\'       => __( \'Search Store\' ),
        \'not_found\'          => __( \'No Store found\' ),
        \'not_found_in_trash\' => __( \'No store found in the Trash\' ), 
        \'parent_item_colon\'  => \'\',
        \'menu_name\'          => \'Our Store\'
    );
$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\' => \'store/%store_location%\')
    );
    register_post_type( \'ss_store\', $args );  

}


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

$labels = array(
        \'name\'                       => \'Cities\',
        \'singular_name\'              => \'City\',
        \'menu_name\'                  => \'City\',
        \'all_items\'                  => \'All Cities\',
        \'parent_item\'                => \'Parent City\',
        \'parent_item_colon\'          => \'Parent City:\',
        \'new_item_name\'              => \'New City Name\',
        \'add_new_item\'               => \'Add New City\',
        \'edit_item\'                  => \'Edit City\',
        \'update_item\'                => \'Update City\',
        \'separate_items_with_commas\' => \'Separate City with commas\',
        \'search_items\'               => \'Search Cities\',
        \'add_or_remove_items\'        => \'Add or remove Cities\',
        \'choose_from_most_used\'      => \'Choose from the most used Cities\',
    );

    $args = array(
        \'labels\'                     => $labels,
        //\'hierarchical\'               => false,
        \'public\'                     => true,
        \'show_ui\'                    => true,
        \'query_var\'                  => true,
        \'show_admin_column\'          => true,
        \'show_in_nav_menus\'          => true,
        \'show_tagcloud\'              => false,
        \'has_archive\'                => true

    );


    register_taxonomy( \'city\', \'ss_store\', $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(\'%store_location%\', $post_link) === \'FALSE\' ) {
      return $post_link;
    }
    $post = get_post($id);
    if ( !is_object($post) || $post->post_type != \'ss_store\' ) {
      return $post_link;
    }
    $terms = wp_get_object_terms($post->ID, \'store_location\');
    if ( !$terms ) {
      return str_replace(\'store/%store_location%/\', \'\', $post_link);
    }
    return str_replace(\'%store_location%\', $terms[0]->slug, $post_link);
}


add_filter( \'rewrite_rules_array\',\'my_insert_rewrite_rules\' );
add_action( \'wp_loaded\',\'my_flush_rules\' );  

/// flush  
function my_flush_rules(){
    $rules = get_option( \'rewrite_rules\' );
            global $wp_rewrite;
    $wp_rewrite->flush_rules();
} 

// Also, you will have to write rewrite rules for your custom post type for your proposed URI structure to work.

// Add the below code to your theme\'s functions.php file:


 `
// Adding a new rule    
function my_insert_rewrite_rules( $rules )    
{
    $newrules = array();
    $newrules[\'store/?$\'] = \'index.php?post_type=ss_store\';
    $newrules[\'store/page/?([0-9]{1,})/?$\'] = \'index.php?post_type=ss_work&paged=$matches[1]\';
    $newrules[\'store/(.+?)/page/?([0-9]{1,})/?$\'] = \'index.php?post_type=ss_store&city=$matches[1]&paged=$matches[2]\';
    //print_r($rules);
    return $newrules + $rules;
这是我使用的代码。。。永久链接已按需要格式化,但现在无法加载模板。。。404

结束

相关推荐

Wacky taxonomy in wordpress

我有我的主要网站,www.whatsthatbug。com和一个测试站点test。什么是臭虫。com。据我所知,它们是相同的——相同版本的wordpress,相同版本的相同插件,相同主题,相同修改。然而。www上的category小部件显示父类别的计数,但不包括计数中的子类别,而在测试中(以及它直到最近在www上的工作方式),类别计数包括子类别的小计。和在wp管理/编辑标签中。php?分类法=www上的类别,我没有看到任何类别的子类别。这让一些事情变得困难。这意味着有些东西坏了。在测试中,子类别按其应有的

URL重写的自定义帖子类型和自定义分类起作用,但模板不起作用 - 小码农CODE - 行之有效找到问题解决它

URL重写的自定义帖子类型和自定义分类起作用,但模板不起作用

时间:2014-12-20 作者:user4517

我搜索了很多,但没有找到,只是得到了部分答案,我声明了自定义的post类型“store”,分类法为“city”,我尝试的是根据city列表创建店铺列表,并创建类似于post type=store的URL<posttype slug><taxonomy term>

。com/store/tokyo/my\\u store\\u name

我是如何使用此代码实现它的。Custom Post Type / Taxonomy Slug / Post Title with post type archive但现在如何使用自定义模板,我尝试了“archive-city.php、single-store.php、taxonomy-city.php,以及一些更标准的组合,但都是404。

1 个回复
SO网友:user4517

function my_custom_post_work() {
    $labels = array(
        \'name\'               => _x( \'Store\', \'post type general name\' ),
        \'singular_name\'      => _x( \'Store\', \'post type singular name\' ),
        \'add_new\'            => _x( \'Add New\', \'Store\' ),
        \'add_new_item\'       => __( \'Add New Store\' ),
        \'edit_item\'          => __( \'Edit Store\' ),
        \'new_item\'           => __( \'New Store\' ),
        \'all_items\'          => __( \'All Store\' ),
        \'view_item\'          => __( \'View Store\' ),
        \'search_items\'       => __( \'Search Store\' ),
        \'not_found\'          => __( \'No Store found\' ),
        \'not_found_in_trash\' => __( \'No store found in the Trash\' ), 
        \'parent_item_colon\'  => \'\',
        \'menu_name\'          => \'Our Store\'
    );
$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\' => \'store/%store_location%\')
    );
    register_post_type( \'ss_store\', $args );  

}


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

$labels = array(
        \'name\'                       => \'Cities\',
        \'singular_name\'              => \'City\',
        \'menu_name\'                  => \'City\',
        \'all_items\'                  => \'All Cities\',
        \'parent_item\'                => \'Parent City\',
        \'parent_item_colon\'          => \'Parent City:\',
        \'new_item_name\'              => \'New City Name\',
        \'add_new_item\'               => \'Add New City\',
        \'edit_item\'                  => \'Edit City\',
        \'update_item\'                => \'Update City\',
        \'separate_items_with_commas\' => \'Separate City with commas\',
        \'search_items\'               => \'Search Cities\',
        \'add_or_remove_items\'        => \'Add or remove Cities\',
        \'choose_from_most_used\'      => \'Choose from the most used Cities\',
    );

    $args = array(
        \'labels\'                     => $labels,
        //\'hierarchical\'               => false,
        \'public\'                     => true,
        \'show_ui\'                    => true,
        \'query_var\'                  => true,
        \'show_admin_column\'          => true,
        \'show_in_nav_menus\'          => true,
        \'show_tagcloud\'              => false,
        \'has_archive\'                => true

    );


    register_taxonomy( \'city\', \'ss_store\', $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(\'%store_location%\', $post_link) === \'FALSE\' ) {
      return $post_link;
    }
    $post = get_post($id);
    if ( !is_object($post) || $post->post_type != \'ss_store\' ) {
      return $post_link;
    }
    $terms = wp_get_object_terms($post->ID, \'store_location\');
    if ( !$terms ) {
      return str_replace(\'store/%store_location%/\', \'\', $post_link);
    }
    return str_replace(\'%store_location%\', $terms[0]->slug, $post_link);
}


add_filter( \'rewrite_rules_array\',\'my_insert_rewrite_rules\' );
add_action( \'wp_loaded\',\'my_flush_rules\' );  

/// flush  
function my_flush_rules(){
    $rules = get_option( \'rewrite_rules\' );
            global $wp_rewrite;
    $wp_rewrite->flush_rules();
} 

// Also, you will have to write rewrite rules for your custom post type for your proposed URI structure to work.

// Add the below code to your theme\'s functions.php file:


 `
// Adding a new rule    
function my_insert_rewrite_rules( $rules )    
{
    $newrules = array();
    $newrules[\'store/?$\'] = \'index.php?post_type=ss_store\';
    $newrules[\'store/page/?([0-9]{1,})/?$\'] = \'index.php?post_type=ss_work&paged=$matches[1]\';
    $newrules[\'store/(.+?)/page/?([0-9]{1,})/?$\'] = \'index.php?post_type=ss_store&city=$matches[1]&paged=$matches[2]\';
    //print_r($rules);
    return $newrules + $rules;
这是我使用的代码。。。永久链接已按需要格式化,但现在无法加载模板。。。404

相关推荐

如何控制根据Taxonomy术语显示什么模板?

我正在建立一个商业目录,并将给出一些背景,然后在最后有2个问题。The development URL is: http://svcta.lainternet.biz/The website I am rebuilding is: https://www.visitsimivalley.com/当前网站要求每个分类法类型具有唯一的业务概要文件。例如,如果您是一家酒店,并且您也有会议室和婚礼场地,那么您最终会得到3个列表,一个用于酒店,一个用于会议,一个用于婚礼。我希望有一个主配置文件,其中包含我们将显示的