我已经创建了名为
天然食品
在我的页面中,天然食物。php
<div class="main container">
<div class="content left">
<?php
$type = \'naturalfoods\';
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$args=array(
\'post_type\' => $type,
\'post_status\' => \'publish\',
\'posts_per_page\' => 2,
\'paged\'=>$paged
);
$my_query = null;
$my_query = new WP_Query($args);
if ( $my_query->have_posts()) :
while ( $my_query->have_posts()) : $my_query->the_post(); ?>
<article class="post">
<h1>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h1>
<?php the_excerpt(); ?>
<div class="readMore right">
<a href="<?php the_permalink(); ?>" class="btnReadMore">Read More»</a>
</div><!-- End readMore -->
<div class="clear"></div>
</article>
<?php endwhile;
my_simone_paging_nav();
else :
echo "<p>No content found</p>";
endif;
wp_reset_query(); // Restore global post data stomped by the_post().
?>
</div><!-- End content -->
</div><!-- End main -->
<?php get_footer(); ?>
我正在索引中使用以下函数。php
在我的功能中。php
if ( !function_exists( \'my_simone_paging_nav\' ) ) {
function my_simone_paging_nav() {
// Don\'t print empty markup if there\'s only one page.
if ( $GLOBALS[\'wp_query\']->max_num_pages < 2 ) {
return;
}
$paged = get_query_var( \'paged\' ) ? intval( get_query_var( \'paged\' ) ) : 1;
$pagenum_link = html_entity_decode( get_pagenum_link() );
$query_args = array();
$url_parts = explode( \'?\', $pagenum_link );
if ( isset( $url_parts[1] ) ) {
wp_parse_str( $url_parts[1], $query_args );
}
$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
$pagenum_link = trailingslashit( $pagenum_link ) . \'%_%\';
$format = $GLOBALS[\'wp_rewrite\']->using_index_permalinks() && ! strpos( $pagenum_link, \'index.php\' ) ? \'index.php/\' : \'\';
$format .= $GLOBALS[\'wp_rewrite\']->using_permalinks() ? user_trailingslashit( \'page/%#%\', \'paged\' ) : \'?paged=%#%\';
// Set up paginated links.
$links = paginate_links( array(
\'base\' => $pagenum_link,
\'format\' => $format,
\'total\' => $GLOBALS[\'wp_query\']->max_num_pages,
\'current\' => $paged,
\'mid_size\' => 2,
\'add_args\' => array_map( \'urlencode\', $query_args ),
\'prev_text\' => __( \'«\', \'my-simone\' ),
\'next_text\' => __( \'»\', \'my-simone\' ),
\'type\' => \'list\',
) );
if ( $links ) :
?>
<nav class="navigation paging-navigation right" role="navigation">
<?php echo $links; ?>
</nav><!-- .navigation -->
<?php
endif;
}
}
但分页不起作用
为创建自定义帖子类型定义的代码
function my_custom_posttypes() {
// Natural Foods Post types
$labels = array(
\'name\' => \'NaturalFoods\',
\'singular_name\' => \'NaturalFoods\',
\'menu_name\' => \'NaturalFoods\',
\'name_admin_bar\' => \'NaturalFoods\',
\'add_new\' => \'Add New\',
\'add_new_item\' => \'Add New NaturalFoods\',
\'new_item\' => \'New NaturalFoods\',
\'edit_item\' => \'Edit NaturalFoods\',
\'view_item\' => \'View NaturalFoods\',
\'all_items\' => \'All NaturalFoods\',
\'search_items\' => \'Search NaturalFoods\',
\'parent_item_colon\' => \'Parent NaturalFoods:\',
\'not_found\' => \'No NaturalFoods found.\',
\'not_found_in_trash\' => \'No NaturalFoods found in Trash.\',
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'menu_icon\' => \'dashicons-id-alt\',
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'naturalfoods\' ),
\'capability_type\' => \'post\',
\'has_archive\' => true,
\'hierarchical\' => false,
\'menu_position\' => 5,
\'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'comments\' ),
\'taxonomies\' => array(\'category\', \'post_tag\' )
);
register_post_type(\'naturalfoods\', $args);
}
add_action(\'init\',\'my_custom_posttypes\');