admin管理员组

文章数量:1026989

I have been working on this for quite awhile but I'm stuck at rewriting the permalinks for both the parent and the child.

So I created two custom post types: magazines and magazine-pages both MUST be hierarchical. Also magazine-pages is part of the magazine custom post type:

Here's my code for that:

// Magazines
    $args = array(
        'label' => 'Magazines',
        'labels' => array(
            //my-labels
        ),
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 25,
        'hierarchical' => true,
        'query_var' => true,
        'rewrite' => array( "slug" => "magazines" ),
        'supports' => array('title', 'thumbnail'),
        'menu_icon'   => 'dashicons-book'
    );

    register_post_type('magazines', $args);

    // Magazine pages
    $args = array(
        'label' => 'Magazine pages',
        'labels' => array(
            //my-labels
        ),
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'hierarchical' => true,
        'query_var' => true,
        "rewrite" => array( "slug" => "magazines" ),
        'supports' => array('title', 'thumbnail', 'editor'),
        'show_in_menu' => 'edit.php?post_type=magazines'
    );

    register_post_type('magazine-pages', $args);
}

What I'm trying to achieve:

/magazines/magazine-1/magazine-page-1

Where

/magazine-1 Should be a post from CPT magazine (parent)

and

/magazine-page-1 Should be a post from CPT magazine-pages (child)

I did add the following rewrite rules to make this work, but unfortunately it doesn't. Only the parent page works, the child pages returns a 404 error.

add_action( 'init', function() {
    add_rewrite_rule( '^magazines/([^/]*)/?','index.php?magazines=$matches[1]','top' );
    add_rewrite_rule( '^magazines/([^/]*)/([^/]*)/?','index.php?magazine-pages=$matches[2]','top' ); 
});

Did I do something wrong in the rewrite rules? All the help is welcome!

I have been working on this for quite awhile but I'm stuck at rewriting the permalinks for both the parent and the child.

So I created two custom post types: magazines and magazine-pages both MUST be hierarchical. Also magazine-pages is part of the magazine custom post type:

Here's my code for that:

// Magazines
    $args = array(
        'label' => 'Magazines',
        'labels' => array(
            //my-labels
        ),
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 25,
        'hierarchical' => true,
        'query_var' => true,
        'rewrite' => array( "slug" => "magazines" ),
        'supports' => array('title', 'thumbnail'),
        'menu_icon'   => 'dashicons-book'
    );

    register_post_type('magazines', $args);

    // Magazine pages
    $args = array(
        'label' => 'Magazine pages',
        'labels' => array(
            //my-labels
        ),
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'hierarchical' => true,
        'query_var' => true,
        "rewrite" => array( "slug" => "magazines" ),
        'supports' => array('title', 'thumbnail', 'editor'),
        'show_in_menu' => 'edit.php?post_type=magazines'
    );

    register_post_type('magazine-pages', $args);
}

What I'm trying to achieve:

/magazines/magazine-1/magazine-page-1

Where

/magazine-1 Should be a post from CPT magazine (parent)

and

/magazine-page-1 Should be a post from CPT magazine-pages (child)

I did add the following rewrite rules to make this work, but unfortunately it doesn't. Only the parent page works, the child pages returns a 404 error.

add_action( 'init', function() {
    add_rewrite_rule( '^magazines/([^/]*)/?','index.php?magazines=$matches[1]','top' );
    add_rewrite_rule( '^magazines/([^/]*)/([^/]*)/?','index.php?magazine-pages=$matches[2]','top' ); 
});

Did I do something wrong in the rewrite rules? All the help is welcome!

本文标签: url rewritingCustom Post Type parentchild relationship rewrite rules for permalinks