我有一个要管理的wordpress网站。我们成功添加了一个自定义帖子类型,称为“合作伙伴”。我已经创建了一个“合作伙伴”页面以列出我的合作伙伴,并且此页面可以正常运行。但是从该页面上,当我单击给定合作伙伴的链接时,而不是转到合作伙伴页面时,出现404错误。同样,如果我在Admin中单击永久链接URL作为合作伙伴条目,则会得到相同的404。
我已经确保有一个single-partner.php页面(并且我有一个single.php可以回退),以防万一,我还确保了我拥有post-partner.php和一个存档合作伙伴。 PHP的。这是我创建帖子类型的代码(以及与类别相关的分类法)
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错误而应进行的更改。谢谢!
PS不用说,尽管我在发布过程中尝试了透彻,但是如果我遗漏了任何相关细节,请告诉我,我可以将它们包括在内。
请记住,在注册新的帖子类型时,您需要重新保存站点的永久链接结构以使WordPress能够正确路由请求,因此请转到“设置”>“永久链接”,单击“保存更改”,这应该可以解决问题:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句