URL末尾的Wordpress CPT额外参数 - > 404

时间:2016-08-12 18:11:17

标签: php wordpress url-rewriting routing custom-post-type

我创建了一个自定义帖子类型。收藏见下面的代码。

a, b
a, ab*, b
a, aab*, ab, b
a, aab, ab, abb*, b
a, aab, ab, abab*, abb, b
a, aaab*, aab, ab, abab, abb, b

像魅力一样工作。 当我转到此页面时:http://www.example.com/collections/slug

它通过一个钩子向我展示了模板:

`$labels = array(
        'name' => __( 'Collections', 'pcs' ),
        'singular_name' => __( 'Collection', 'pcs' ),
        'add_new' => __( 'New Collection', 'pcs' ),
        'add_new_item' => __( 'New Collection', 'pcs' ),
        'edit_item' => __( 'Edit Collection', 'pcs' ),
        'new_item' => __( 'New Collection', 'pcs' ),
        'view_item' => __( 'View Collection', 'pcs' ),
        'search_items' => __( 'Search Collections', 'pcs' ),
        'not_found' => __( 'No Collection Found', 'pcs' ),
        'not_found_in_trash' => __( 'No Collection Found in Trash', 'pcs' ),
        'parent_item_colon' => __( 'Parent Collection', 'pcs' ),
        'menu_name' => __( 'All Collections', 'pcs' ),
        'filter_items_list' => __( 'Filter collections list', 'pcs' ),
        'items_list_navigation' => __( 'Collections list navigation', 'pcs' ),
        'items_list' => __( 'Collections list', 'pcs' )
    );`

    $args = array(
        'labels' => $labels,
        'hierarchical' => false,
        'supports' => array( 'title' ),
        'public' => true,
        'show_ui' => true,
        'show_in_nav_menus' => false,
        'show_in_menu'  => 'pcs',
        'menu_position' => 1,
        'publicly_queryable' => true,
        'exclude_from_search' => true,
        'has_archive' => false,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => array(
            'slug' => 'collections'),
            'with_front' => false
        ),
        'capability_type' => 'post'
    );

问题是,我想通过这样的网址传递一个额外的变量:http://www.example.com/collection/slug/email_md5

因此,我可以使用email_hash作为查看集合的验证。我知道我必须制作一个网址重写规则,但我无法使其正常工作。

function xload_template( $single_template ) {

    global $post;

    if ( $post->post_type == 'my_collection' ) {
        if ( file_exists( PATH . 'frontend/template.php' ) ) {
            $single_template = PATH . 'frontend/template.php';
        }
    }

    return $single_template;
}

add_filter( 'single_template', 'xload_template' );

FIX:由于我对Wordpress插件中的自定义帖子类型很新,我发现如果你知道Wordpress在做什么,那么创建重写要容易得多。更改您的wordpress设置 - >永久链接 - >平原。 这样您就可以看到如何调用url以及如何在自己的重写中使用它。下面的答案为我解决了。感谢

1 个答案:

答案 0 :(得分:1)

我认为你要找的是add_rewrite_rule。您可以尝试以下内容。

  function custom_rewrite_rule() {
    add_rewrite_rule('collections/([^/]+)/([^/]+)/?$','index.php?collections=$matches[1]&email=$matches[2]','top');
  }
  add_action('init', 'custom_rewrite_rule', 10, 0);