WordPress

Strona 2 przepisywania niestandardowego typu postu/taksonomii daje 404

  • 30 września, 2014
  • 4 min read
Strona 2 przepisywania niestandardowego typu postu/taksonomii daje 404


Postępowałem zgodnie z kodem opartym na tym i wszystko działa idealnie – Jak utworzyć strukturę bezpośredniego łącza z niestandardowymi taksonomiami i niestandardowymi typami postów, takimi jak nazwa-podstawowa/podatek-nadrzędny/podatek-dziecka/niestandardowa-nazwa typu postu

Dzięki temu mogę przeglądać archiwa taksonomii

my/category
(or)
my/category/subcategory

i wyświetl niestandardowy post powiązany z każdym terminem jako

my/category/subcategory/custompost

Jednak nie mogę obejrzeć

my/category/page/2/
or
my/category/subcategory/page/2/

Daje mi 404.

Oto kod, którego używam.

posttype.my.php

function create_my_posttype() {
    register_post_type(
        'my',
    array(
        'labels' => array(
            'name' => _x( 'Mys', 'post type general name' ),
            'singular_name' => _x( 'My', 'post type singular name' ),
            'add_new' => _x( 'New My', 'add new singular name' ),
            'add_new_item' => __( 'Add New My' ),
            'edit_item' => __( 'Edit My' ),
            'new_item' => __( 'New My' ),
            'view_item' => __( 'View My' ),
            'search_items' => __( 'Search Mys' ),
            'not_found' =>  __( 'No Mys Found' ),
            'not_found_in_trash' => __( 'No Mys found in Trash' ),
            'parent_item_colon' => '-:-'
         ),

        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array(
            'slug' => 'my/%mycategory%',
            'with_front' => true,
            'pages' => true
        ),
        'capability_type' => 'post',
        'hierarchical' => true,
        'show_in_nav_menus' => false,
        'menu_position' => 50,
        'supports' => array(
            'title',
            'editor',
            'comments',
            'shortlink'
        ),
    )
);
}

add_action( 'init', 'create_my_posttype');

taksonomia.my.php

add_action( 'init', 'create_my_taxonomies', 0 );

function create_my_taxonomies() {
    register_taxonomy(
            'mycategory',
            'my',
            array(
                    'labels' => array(
                            'name' => 'Mycategories',
            'singular_name' => 'Mycategory',
            'search_items' => 'Search Mycategories',
            'all_items' => 'All Mycategories',
            'parent_item' => 'Parent Mycategories',
                            'add_new_item' => 'Add New Mycategory',
                            'new_item_name' => "New Mycategory",
            'edit_item' => 'Edit Mycategory',
            'update_item' => 'Update Mycategory',
            'add_new_item' => 'Add New Mycategory',
            'new_item_name' => 'New MyCategory Name',
            'menu_name' => 'Mycategory'
                    ),

        'query_var' => true,
                    'show_ui' => true,
        'has_archive' => true,
                    'show_tagcloud' => false,
                    'hierarchical' => true,
                    'with_front' => true,
                    'rewrite' => array(
                            'slug' => 'my',
                            'with_front' => true,
            'hierarchical' => true,
                    )
            )
    );
}

główny.php

include( 'posttype.my.php' );
include( 'taxonomy.my.php' );


add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules) {
$newRules  = array();
$newRules['my/(.+)/(.+)/(.+)/?$'] = 'index.php?my=$matches[3]';
$newRules['my/(.+)/?$']                = 'index.php?mycategory=$matches[1]';

return array_merge($newRules, $rules);
}

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

if ($cats = get_the_terms($post->ID, 'mycategory'))
{
    $link = str_replace('%mycategory%', get_taxonomy_parents(array_pop($cats)->term_id, 'mycategory', false, " true), $link); // see custom function defined below
}
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);


function get_taxonomy_parents($id, $taxonomy, $link = false, $separator=" $nicename = false, $visited = array()) {
$chain = '';
$parent = &get_term($id, $taxonomy);

if (is_wp_error($parent)) {
    return $parent;
}

if ($nicename)
    $name = $parent -> slug;
else
    $name = $parent -> name;

if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {
    $visited[] = $parent -> parent;
    $chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited);

}

if ($link) {
    // nothing, can't get this working :(
} else
    $chain .= $name . $separator;
return $chain;
}

class main_app {
    function __construct() {
    } // end class main_app


}

Jeśli ktoś może pomóc lub wskazać mi właściwy kierunek, byłoby to niesamowite. Nie chcę uciekać się do oddzielnych adresów URL dla taksonomii i niestandardowych typów postów:\ Czy muszę zmodyfikować regułę przepisywania, aby zawierała coś związanego z „stronicowaniem”?

Warto przeczytać!  Jak łatwo utworzyć wielojęzyczną mapę witryny w WordPress

Dzięki


Źródło