自定义帖子类型的自定义固定链接-工作正常,但分页时损坏

时间:2021-12-23 作者:Rashid

我在尝试获取自定义post类型permalink时遇到分页不起作用的问题。我在这里自定义了帖子类型“songs”和自定义分类“singer”:

example.com/songs/ // 自定义帖子类型

example.com/singer/ // 自定义分类

example.com/songs/article // 后Permalink

我想让它看起来像

example.com/singer/article /// 地点com/自定义分类法/post

这是我的自定义邮件类型代码

add_action( \'init\', \'your_prefix_register_post_type\' );
function your_prefix_register_post_type() {
    $labels = [
        \'name\'                     => esc_html__( \'Songs\', \'your-textdomain\' ),
        \'singular_name\'            => esc_html__( \'Song\', \'your-textdomain\' ),
        \'add_new\'                  => esc_html__( \'Add New\', \'your-textdomain\' ),
        \'add_new_item\'             => esc_html__( \'Add new song\', \'your-textdomain\' ),
        \'edit_item\'                => esc_html__( \'Edit Song\', \'your-textdomain\' ),
        \'new_item\'                 => esc_html__( \'New Song\', \'your-textdomain\' ),
        \'view_item\'                => esc_html__( \'View Song\', \'your-textdomain\' ),
        \'view_items\'               => esc_html__( \'View Songs\', \'your-textdomain\' ),
        \'search_items\'             => esc_html__( \'Search Songs\', \'your-textdomain\' ),
        \'not_found\'                => esc_html__( \'No songs found\', \'your-textdomain\' ),
        \'not_found_in_trash\'       => esc_html__( \'No songs found in Trash\', \'your-textdomain\' ),
        \'parent_item_colon\'        => esc_html__( \'Parent Song:\', \'your-textdomain\' ),
        \'all_items\'                => esc_html__( \'All Songs\', \'your-textdomain\' ),
        \'archives\'                 => esc_html__( \'Song Archives\', \'your-textdomain\' ),
        \'attributes\'               => esc_html__( \'Song Attributes\', \'your-textdomain\' ),
        \'insert_into_item\'         => esc_html__( \'Insert into song\', \'your-textdomain\' ),
        \'uploaded_to_this_item\'    => esc_html__( \'Uploaded to this song\', \'your-textdomain\' ),
        \'featured_image\'           => esc_html__( \'Featured image\', \'your-textdomain\' ),
        \'set_featured_image\'       => esc_html__( \'Set featured image\', \'your-textdomain\' ),
        \'remove_featured_image\'    => esc_html__( \'Remove featured image\', \'your-textdomain\' ),
        \'use_featured_image\'       => esc_html__( \'Use as featured image\', \'your-textdomain\' ),
        \'menu_name\'                => esc_html__( \'Songs\', \'your-textdomain\' ),
        \'filter_items_list\'        => esc_html__( \'Filter songs list\', \'your-textdomain\' ),
        \'filter_by_date\'           => esc_html__( \'\', \'your-textdomain\' ),
        \'items_list_navigation\'    => esc_html__( \'Songs list navigation\', \'your-textdomain\' ),
        \'items_list\'               => esc_html__( \'Songs list\', \'your-textdomain\' ),
        \'item_published\'           => esc_html__( \'Song published\', \'your-textdomain\' ),
        \'item_published_privately\' => esc_html__( \'Song published privately\', \'your-textdomain\' ),
        \'item_reverted_to_draft\'   => esc_html__( \'Song reverted to draft\', \'your-textdomain\' ),
        \'item_scheduled\'           => esc_html__( \'Song scheduled\', \'your-textdomain\' ),
        \'item_updated\'             => esc_html__( \'Song updated\', \'your-textdomain\' ),
    ];
    $args = [
        \'label\'               => esc_html__( \'Songs\', \'your-textdomain\' ),
        \'labels\'              => $labels,
        \'description\'         => \'\',
        \'public\'              => true,
        \'hierarchical\'        => false,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'show_ui\'             => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'show_in_rest\'        => true,
        \'query_var\'           => true,
        \'can_export\'          => true,
        \'delete_with_user\'    => true,
        \'has_archive\'         => true,
        \'rest_base\'           => \'\',
        \'show_in_menu\'        => true,
        \'menu_position\'       => \'\',
        \'menu_icon\'           => \'dashicons-format-audio\',
        \'capability_type\'     => \'post\',
        \'supports\'            => [\'title\', \'editor\', \'thumbnail\'],
        \'taxonomies\'          => [\'singer\', \'rapper\', \'category\', \'movie\', \'album\', \'composer\', \'lyricist\', \'by-year\', \'mood\', \'file-link\', \'post-id\', \'yt_id\', \'artist\'],
        \'rewrite\'             => array(\'slug\' => \'songs\'),
    ];

    register_post_type( \'songs\', $args );
}
和用于为post permalink重写slug的代码

function wpa_course_post_link( $post_link, $id = 0 ){
    $post = get_post($id);  
    if ( is_object( $post ) ){
        $terms = wp_get_object_terms( $post->ID, \'singer\' );
        if( $terms ){
            return str_replace( \'/songs/\' , \'/\' . $terms[0]->slug . \'/\' , $post_link );
        }
    }
    return $post_link;
}
add_filter( \'post_type_link\', \'wpa_course_post_link\', 1, 3 );

function custom_rewrite_rules() {
    add_rewrite_rule(
        \'^(.*)/(.*)/?$\',
        \'index.php?post_type=songs&name=$matches[2]\',
        \'top\'
    );
}
add_action( \'init\', \'custom_rewrite_rules\' );
感谢您的帮助。

1 个回复
SO网友:MR. shaikh__92

// change in post type argument
register_post_type(
             "rewrite" => [ "slug" => "songs/%singer%",
 //change in taxonomy argument                         "with_front" => true ]);
 register_taxonomy(
                    \'rewrite\' =>false\',
                    \'with_front\' => true,
                    \'query_var\' => true,
                    \'Custom Rewrite Slug\' => \'\');
// for add rewrite rule
function so23698827_add_rewrite_rules( $rules ) {
  $new = array();
  $new[\'songs/([^/]+)/(.+)/?$\'] = \'index.php?songs=$matches[2]\';
  $new[\'songs/(.+)/?$\'] = \'index.php?singer=$matches[1]\';

  return array_merge( $new, $rules ); // Ensure our rules come first
}
add_filter( \'rewrite_rules_array\', \'so23698827_add_rewrite_rules\' );

// for permalinks
function wpa_show_permalinks( $post_link, $post ){
    if ( is_object( $post ) && $post->post_type == \'songs\' ){
        $terms = wp_get_object_terms( $post->ID, \'singer\' );
        if( $terms ){
            return str_replace( \'%singer%\' , $terms[0]->slug , $post_link );
        }
        else{
           return str_replace( \'%singer%/\' , \'\' , $post_link ); 
        }
    }
    return $post_link;
}
add_filter( \'post_type_link\', \'wpa_show_permalinks\', 1, 3 );
希望这对您有帮助:)

相关推荐

如何在插件类中使用Apply_Filters()?

我希望主题能够改变插件类中的默认数据数组。目前的设置如下:namespace Example; class Example_Class { private $stuff; public function __construct() { $this->stuff = $this->set_stuff(); } public function