Wordpress:自定义 Post 类型导致 404 页面

Wordpress: Custom Post type leads to 404 Page

我管理着一个 wordpress 网站。我们成功添加了一个名为“合作伙伴”的自定义 post 类型。我已经创建了一个合作伙伴页面来列出我的合作伙伴,并且它工作正常。但是在该页面上,当我单击给定合作伙伴的 link,而不是转到合作伙伴页面时,我收到了 404 错误。同样,如果我在合作伙伴条目的管理员中单击 permalink URL,我会得到相同的 404.

我已经确保我有一个 partner.php 页面(并且我有一个 single.php 可以返回)并且以防万一我也确保我有一个 post-partner.php 和一个存档-partner.php。这是我创建 post 类型(以及类别的相关分类法)

的代码
class Xoo_Custom_Post_Type{

    public $postSlug = 'partner';

    public $taxSlug = 'partner_category';
public function __construct(){
    $this->hooks();
}


public function hooks(){
    add_action( 'init', array( $this, 'registerPostType' ) );
    add_shortcode( 'xoo_partners_ui', array( $this, 'shortcode' ) );
    add_shortcode( 'xoo_partners_categories', array( $this, 'short_shortcode' ) );
}

public function registerPostType(){

    $partnerlabels = array(
        'name'              => _x( 'Partners', 'taxonomy general name' ),
        'singular_name'     => _x( 'Partner', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Partners' ),
        'all_items'         => __( 'All Partners' ),
        'parent_item'       => __( 'Parent Partner' ),
        'parent_item_colon' => __( 'Parent Partner:' ),
        'edit_item'         => __( 'Edit Partner' ),
        'update_item'       => __( 'Update Partner' ),
        'add_new_item'      => __( 'Add New Partner' ),
        'new_item_name'     => __( 'New Partner Name' ),
    );

    $partnerargs = array(
        'labels'            => $partnerlabels,
        'public'            => true,
        'show_ui'           => true, // UI in admin panel
        '_builtin'          => false, // It's a custom post type, not built in!
        '_edit_link'        => 'post.php?post=%d',
        'show_in_menu'      => true,
        'show_in_nav_menus' => true,
        'capability_type'   => 'post',
        'hierarchical'      => false,
        'supports'          => array('title', 'editor', 'excerpt', 'custom-fields', 'thumbnail'),
        'has_archive'       => 'partners'
    );

    register_post_type( $this->postSlug, $partnerargs );


    //Register Category Taxonomy
    $labels = array(
        'name'              => _x( 'Categories', 'taxonomy general name', 'textdomain' ),
        'singular_name'     => _x( 'Category', 'taxonomy singular name', 'textdomain' ),
        'search_items'      => __( 'Search Categories', 'textdomain' ),
        'all_items'         => __( 'All Categories', 'textdomain' ),
        'parent_item'       => __( 'Parent Category', 'textdomain' ),
        'parent_item_colon' => __( 'Parent Category:', 'textdomain' ),
        'edit_item'         => __( 'Edit Category', 'textdomain' ),
        'update_item'       => __( 'Update Category', 'textdomain' ),
        'add_new_item'      => __( 'Add New Category', 'textdomain' ),
        'new_item_name'     => __( 'New Category Name', 'textdomain' ),
        'menu_name'         => __( 'Categories', 'textdomain' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
    );

    register_taxonomy( $this->taxSlug, array( $this->postSlug ), $args );

}


public function getCategories(){

    $args = array(
        'taxonomy'      => $this->taxSlug,
        'orderby'       => 'name',
        'order'         => 'ASC',
        'hide_empty'    => false,
    );

    return get_categories($args);

}

public function getAllPartners() {
    
    return get_posts( array(
        'post_type'     => $this->postSlug,
        'numberposts'   => -1,
        'tax_query'     => array(
            array(
                'taxonomy'  => $this->taxSlug,
                'field'     => 'term_id', 
                
            )
        )
    ) );
}


public function getPartnersByCategory( $category_id ){

    return get_posts( array(
        'post_type'     => $this->postSlug,
        'numberposts'   => -1,
        'tax_query'     => array(
            array(
                'taxonomy'  => $this->taxSlug,
                'field'     => 'term_id', 
                'terms'     => $category_id,
            )
        )
    ) );

}



public function short_shortcode() {
    
    $categories = $this->getCategories();
    
    if( empty( $categories ) ){
        return __( '' );
    }
    
    ob_start();
    
    foreach ( $categories as $index => $category ) {
        echo '<li><a id="' . str_ireplace(' ','_',$category->name) . '">' . $category ->name . '</a></li>';
    }
    
    return ob_get_clean();
}

public function shortcode(){

    $categories = $this->getCategories();
    
    if( empty( $categories ) ){
        return __( 'No categories found' );
    }

    ob_start();

    echo '<ul>';

    foreach ( $categories as $index => $category ) {

        echo '<li>';

        esc_html_e( $category->name );

        $partners = $this->getPartnersByCategory( $category->term_id );

        echo '<ul>';

        foreach ( $partners as $partner ) {
            printf( '<li><a href="%1$s">%2$s</a></li>', esc_url( get_permalink( $partner->ID ) ), esc_html( $partner->post_title ) );
        }
        
        echo '</ul>';

        echo '</li>';
        }

        echo '</ul>';

        return ob_get_clean();
    }

}

add_action( 'init', function(){
    new Xoo_Custom_Post_Type();
}, 0 );

同样,这是我页面上的代码,它根据 post 创建 permalinks,给我 404 错误:

<?php 
                
                    $args = array(
                        'taxonomy'      => 'partner_category',
                        'orderby'       => 'name',
                        'order'         => 'ASC',
                        'hide_empty'    => false,
                    );

                    $loopa = get_categories($args);
                    if (!empty($loopa)) { 
                        foreach( $loopa as $category) { 
                            $posts = get_posts( array(
                                                        'post_type'     => 'partner',
                                                        'numberposts'   => -1,
                                                        'orderby'       => 'title',
                                                        'order'         => 'ASC',
                                                        'tax_query'     => array(
                                                            array(
                                                                'taxonomy'  => 'partner_category',
                                                                'field'     => 'term_id', 
                                                                'terms'     => $category->term_id,
                                                            )
                                                        )
                                                ) 
                                    );
                            //print_r($posts);
                        ?>
                        <section id="all-trade-paperback" class="all-comics bottom-content-wrap <?php echo str_ireplace(' ','_',$category->name) ?>">
                            <h1 class="upper-82 standard-case blue"><?php echo tCase( $category->name ) ?></h1>
                            
                            <div class="letter-content-wrap float-wrap white series-comics">
                                        <ul class="col-2-layout standard-40-bold upper float-left col-91">
                                            <?php foreach( $posts as $post ) {  ?>
                                                    <li data-title="<?php echo $post->post_title?>">
                                                        <a class="white" href="<?php get_permalink($post->ID); ?>"><?php echo $post->post_title; ?></a>
                                                    </li>
                                            <?php } ?>
                                        </ul>
                            </div>
                        </section>
                        <?php }
                    }
                    
                ?>

我希望,通过在这里分享我的代码,更精通 WordPress 的人可以指出我的愚蠢之处,以及我应该改变什么来解决 404 错误。谢谢!

P.S。不用说,虽然我在 posting 中尝试做到全面,但如果我遗漏了任何相关细节,请告诉我,我可以将它们包括在内。

请记住,在注册新的 post 类型时,您需要 re-save 您网站的永久链接结构,以便 WordPress 能够正确路由请求,因此请转至 设置 >固定链接,点击保存更改,这应该可以解决问题:)