我的代码可以获取自定义字词的所有帖子, 我想为该代码应用分页。
<div> <?php
$text = $_POST['text'];
/* echo ($text); */
$args = array( 'numberposts' => -1 );
$posts = get_posts( $args );
foreach($posts as $post)
{
$custom_terms = get_the_terms($post->ID,'Characters');
foreach($custom_terms as $custom_term)
{
if ( ($custom_term->name) == ($text) )
{
?>
<div class="col-sm-2">
<article class="item">
<div class="article-inner">
<div class="overlay"></div>
<div class="img-wrapper"><img class="img-100p latest-post-image"
src="<?php the_post_thumbnail_url('large'); ?>" alt="img"></div>
<div class="article-info">
<h4 class="entry-title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?><?php
//echo $countA ?><?php //echo $count_posts?><?php //echo $count%$st?></a>
</h4>
</div>
</div>
</article>
</div>
<?php
}
}
}
?>
</div>
我正在使用插件 https://wordpress.org/plugins/wck-custom-fields-and-custom-post-types-creator/ 在后端admin wordpress上创建custom_terms。
并尝试使用此代码但是当转到第2页时没有帖子出现
<div>
<?php
$text = $_POST['text'];
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$count = 0;
$st = 15;
$args = array( 'numberposts' => -1 );
$posts = get_posts( $args );
$stack = array(2005);
$poststatus = array( 'publish' );
$current_category = single_cat_title("", false);
foreach($posts as $post)
{
$custom_terms = get_the_terms($post->ID,'characters');
foreach($custom_terms as $custom_term)
{
if ( (($custom_term->name) == ($text)) )
{
$counter ++ ;
array_push($stack,($post->ID) );
}
}
}
$args1 = array(
'post_type' => 'post',
'post__in' => $stack,
//'category_name' => '',
'post_status' => 'publish',
'posts_per_page' => 18,
//'offset' => 5,
'order_by' => 'date',
'paged' => $paged,
'ignore_sticky_posts' => 5 ,
);
$wp_query = new WP_Query( $args1 );
$count_posts = $wp_query->post_count;
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) :
$wp_query->the_post();
$count++;
if ($count%$st == 4) :
$count = 1;
?>
<?php
endif;
?>
<div class="col-sm-4">
<article class="item">
<div class="article-inner">
<div class="overlay"></div>
<div class="img-wrapper"><img class="img-100p latest-post-image"
src="<?php the_post_thumbnail_url('large'); ?>" alt="img"></div>
<div class="article-info">
<h4 class="entry-title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h4>
</div>
</div>
</article>
</div><!--col-sm-4-->
<?php
endwhile;
//wp_reset_postdata();
//wp_reset_query();
//wp_reset_postdata();
endif;
?>
@ManoharSingh
这是完整的模板文件
<?php
/**
<?php /* Template Name: C1
*
* Used to display archive-type pages if nothing more specific matches a query.
* For example, puts together date-based pages if no date.php file exists.
*
* If you'd like to further customize these archive views, you may create a
* new template file for each one. For example, tag.php (Tag archives),
* category.php (Category archives), author.php (Author archives), etc.
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package Primer
* @since 1.0.0
*/
get_header();
$temp = $wp_query;
// $wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=6&post_type=post_type_name'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div>
<?php
$text = $_POST['text'];
/* echo ($text); */
$args = array( 'numberposts' => -1 );
$posts = get_posts( $args );
foreach($posts as $post)
{
$custom_terms = get_the_terms($post->ID,'characters');
foreach($custom_terms as $custom_term)
{
if ( ($custom_term->name) == ($text) )
{
?>
<div class="col-sm-2">
<article class="item">
<div class="article-inner">
<div class="overlay"></div>
<div class="img-wrapper"><img class="img-100p latest-post-image" src="<?php the_post_thumbnail_url('large'); ?>" alt="img"></div>
<div class="article-info">
<h4 class="entry-title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?><?php //echo $countA ?><?php //echo $count_posts?><?php //echo $count%$st?></a>
</h4>
</div>
</div>
</article>
</div>
<?php
}
}
}
?>
</div>
<?php endwhile; ?>
<section class="all-pagination">
<div class="container">
<div class="col-md-12">
<div class="col-md-12 in-pagination">
<ul class="pagination">
<?php wp_pagenavi( array( 'query' => $wp_query ) ); ?>
<?php //if (function_exists('wp_pagenavi')) { wp_pagenavi( array( 'query' => $wp_query ) ); } ?>
</ul>
</div>
</div>
</div>
</section>
<?php ?>
<?php get_footer(); ?>
答案 0 :(得分:0)
试试这段代码,
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=6&post_type=post_type_name'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<!-- LOOP: Usual Post Template Stuff Here-->
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
您可以尝试使用此插件进行分页'https://wordpress.org/plugins/wp-pagenavi/'