我有一个称为服务的自定义帖子类型,它有23个自定义帖子,我还有一个所有类别(5个类别)的自定义分类法。
我正在尝试创建一个所有自定义服务帖子的列表,并且一次只显示5篇我已经完成的帖子。我也在尝试使用wp\\u paginate对它们进行分页,我可以将第1、2和3页转到第1、2和3页,但由于某些原因,我在第4和5页得到了404。
这是我用来分页列表的查询:
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$wp_query = new WP_Query(array(\'post_type\' => \'services\', \'paged\'=>$paged, \'posts_per_page\' => 5, \'order\' => \'DESC\' ));
我的自定义帖子类型代码为:
register_post_type( \'services\',
array(
\'labels\' => array(
\'name\' => __( \'Services\' ),
\'singular_name\' => __( \'Service\' )
),
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => \'services\',
\'capability_type\' => \'page\',
\'has_archive\' => \'our-services\',
\'rewrite\' => array( \'slug\' => \'our-services/%service_categories%\', \'with_front\' => false),
\'taxonomies\' => array( \'service_categories\' ),
\'supports\' => array(
\'title\',
\'editor\',
\'custom-fields\',
\'revisions\',
\'thumbnail\',
\'page-attributes\'
)
)
);
我的自定义分类代码是:
function init_service_categories() {
register_taxonomy(
\'service_categories\',
\'services\',
array(
\'labels\' => array(
\'name\' => \'Services Categories\',
\'add_new_item\' => \'Add New Services Category\',
\'new_item_name\' => "New Services Category"
),
\'show_ui\' => true,
\'show_tagcloud\' => false,
\'hierarchical\' => true,
\'rewrite\' => array( \'slug\' => \'our-services\' ),
)
);
}
add_action(\'init\', \'init_service_categories\', 0);
我也使用这段代码,所以当我转到服务的单个页面时,它的类别位于服务的前面。
add_filter(\'post_type_link\', \'glossary_term_permalink\', 10, 4);
function glossary_term_permalink($post_link, $post, $leavename, $sample)
{
if ( false !== strpos( $post_link, \'%service_categories%\' ) ) {
$glossary_letter = get_the_terms( $post->ID, \'service_categories\' );
$post_link = str_replace( \'%service_categories%\', array_pop( $glossary_letter )->slug, $post_link );
}
return $post_link;
}
我真的很感激在这方面的帮助,因为我太迷茫了,不明白为什么它不起作用。
谢谢