web-dev-qa-db-ja.com

カスタム投稿タイプを使用してURLに投稿者を含む投稿を表示する

私はこれで本当に難しい時間を過ごしています。私はマルチユーザーのギャラリーサイトを持っています。各作者には、作者の投稿を一覧表示する独自のページがあります(これはカスタム投稿タイプです)。

著者のURL(author.php)がwww.example.com/firstname-lastname/になるようにしました。しかし、訪問者が著者の投稿の1つをクリックすると、URLはwww.example.com/custom-post-type-name/postname/に変わりますが、www.example.com/firstname-lastname/postname/にします。 。どうやってこれを達成できますか? .htaccessとは?または投稿タイプを登録するときに書き換えルールを変更しますか?どうやって?

これを使用して、自分の投稿タイプをfunctions.phpファイルに登録します。

// Make custom post type Add media

function galleryRegister()
{
$labels = array(
'name' => _x('Add gallery', 'post type general name'),
'singular_name' => _x('Add gallery', 'post type singular name'),
);

$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'thumbnail'),
'rewrite' => false,
'show_in_nav_menus' => true,
);

register_post_type('gallery' , $args);
}

add_action('init', 'galleryRegister');

手伝ってくれてありがとう。

編集:私は私が手動でwww.example.com/firstname-lastname/postname/を入力できることを見つけました、そしてそれは正しい投稿を示しています、しかしパーマリンクはまだ古いwww.example.com/custom-post-type-name/ポストネーム/至る所に

解決策: すばらしい答えをくれてありがとうStephen!これは私が使ったコードで、うまくいきました。

// ****************************************************
// Make a custom post type "Add gallery"
// ****************************************************
function galleryRegister()
{
$labels = array(
'name' => _x('Add gallery', 'post type general name'),
'singular_name' => _x('Add gallery', 'post type singular name'),
'add_new' => _x('Add gallery ', 'portfolio item'),
'all_items' => __( 'Manage your galleries' ),
'add_new_item' => __('Add gallery'),
'edit_item' => __('Edit your galleries'),
'new_item' => __('New gallery'),
'view_item' => __('View gallery on site'),
'search_items' => __('Search galleries'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);

$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'thumbnail'),
'rewrite' =>  array( 'slug' => _x( '%author%', 'URL slug') ),
'show_in_nav_menus' => true,
);

register_post_type('gallery' , $args);
}

add_action('init', 'galleryRegister');

// ****************************************************
// Flush rewrite rules. Delete this 
// ****************************************************
function my_rewrite_flush() {
    my_cpt_init();

    // ATTENTION: This is *only* done during plugin activation hook in this example!
    // You should *NEVER EVER* do this on every page load!!
    flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'my_rewrite_flush' );

// ****************************************************
// Make author as slug for posts
// ****************************************************
add_filter('post_type_link', 'wpse73228_author_tag',10,4);
function wpse73228_author_tag($post_link, $post, $leavename, $sample){

    if( 'gallery' != get_post_type($post) )
        return $post_link;

    $authordata = get_userdata($post->post_author);
$author = $authordata->user_nicename;

$post_link = str_replace('%author%', $author, $post_link);

return $post_link;
}
5
hlotvonen

register_post_type()rewriteプロパティで%author%タグを使用できます。ただし、書き換え規則は追加されていますが(フラッシュされた後) - 投稿タイプのパーマリンクを生成するときに、WordPressはタグを適切な値に置き換えません。例えば、あなたはパーマリンクで終わることになります/ - www.example.com/%author%/gallery-name

以下は%author%を適切な値に置き換えます。

add_filter('post_type_link', 'wpse73228_author_tag',10,4);
function wpse73228_author_tag($post_link, $post, $leavename, $sample){

    if( 'gallery' != get_post_type($post) )
        return $post_link;

    $authordata = get_userdata($post->post_author);
    $author = $authordata->user_nicename;

    $post_link = str_replace('%author%', $author, $post_link);

    return $post_link;
}
4
Stephen Harris