Wordpress:自定义的文章类型导致404页



我正在管理一个wordpress网站。我们成功地添加了一个名为";合伙人"我创建了一个合作伙伴页面来列出我的合作伙伴,这是正确的。但从那个页面上,当我点击给定合作伙伴的链接,而不是转到合作伙伴页面时,我会收到404错误。同样,如果我在Admin中点击合作伙伴条目的permalink URL,我会得到相同的404。

我已经确保我有一个single-partner.php页面(并且我有一一个single.php可以回退(,以防万一,我还确保我有后partner.php和archive-partnerphp

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 );

同样,这是我页面上的代码,它正在创建每个帖子的永久链接,给我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错误。谢谢

第页。不用说,虽然我在帖子中尽量做到彻底,但如果我遗漏了任何相关细节,请告诉我,我可以把它们包括在内。

请记住,在注册新的帖子类型时,您需要重新保存网站的永久链接结构,以便WordPress能够正确路由请求,因此请转到设置>永久链接,点击保存更改就可以了:(

相关内容

  • 没有找到相关文章

最新更新