我正在尝试构建自定义博客home.php模板,该页面上有2个查询。目标是将总共8个最新职位(降序)分为2组(4个职位和4个职位)。
我已经尝试了两种方法来实现此目的,但是没有一种方法可以100%起作用。以下代码是我认为最接近的尝试(基于我作为示例找到的资源-参见下文)。
这是我的搜索结果:
-
经过几次失败的尝试,我从头开始,并基于以下示例创建了代码:https://code.tutsplus.com/tutorials/use-two-loops-to-output-your-first-blog-post-differently--cms-23656
-
这是模板文件中的代码:
<?php
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<header>
<h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
</header>
<!--START first loop-->
<?php
$args = array(
'posts_per_page' => '4',
'paged'=>$paged,
);
$query = new WP_query ( $args );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); /* start the loop */ ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'first-post' ); ?>>
<?php the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'medium', array(
'class' => 'left',
'alt' => trim(strip_tags( $wp_postmeta->_wp_attachment_image_alt ))
) ); ?>
</a>
<?php } ?>
<section class="entry-content">
<?php the_content(); ?>
</section><!-- .entry-content -->
</article><!-- #post-## -->
<?php // End the loop.
endwhile;
rewind_posts();
} ?>
<!--END first loop-->
<hr/>
<p>I'm dividing text</p>
<hr/>
<!--START second loop-->
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'medium', array(
'class' => 'left',
'alt' => trim(strip_tags( $wp_postmeta->_wp_attachment_image_alt ))
) ); ?>
</a>
<?php } ?>
<section class="entry-summary">
<?php the_excerpt(); ?><a href="<?php the_permalink(); ?>">Further information</a>.
</section><!-- .entry-summary -->
</article><!-- #post-## -->
<?php // End the loop.
endwhile; ?>
<!--END second loop-->
<?php // Previous/next page navigation.
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'twentyfifteen' ),
'next_text' => __( 'Next page', 'twentyfifteen' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>',
) ); ?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>
这是功能文件中的代码:
// offset the main query on the home page and correct pagination
function tutsplus_offset_main_query ( $query ) {
//the offset
$offset = 4;
$ppp = 8; //posts per page
if ( $query->is_home() && $query->is_main_query() && !$query->is_paged() ) {
// Manually determine page query offset (offset + current page (minus one) x posts per page)
$page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
// Apply adjust page offset
$query->set('offset', $page_offset );
}
}
add_action( 'pre_get_posts', 'tutsplus_offset_main_query' );
提前谢谢!