下面的粘贴中是运行CPT存档页的模板的代码http://www.m.angiemeekerdesigns.com/totd
除了分页之外,页面的所有内容都正常工作。当点击“下一页”时,第2页和转发会显示CPT存档中的下一篇文章,这些文章在第一页的两个循环中没有显示。
因此,我不希望第一个循环在后续页面上重复,也不希望后续页面有第一个帖子偏移量,而且由于此归档文件的第1页上总共有11篇帖子,我希望下一页不显示前11篇帖子。
现在发生的事情是,它没有显示第一个循环(好),也没有抵消第一篇帖子(好),但它显示了第一页的所有11篇帖子——第2页和之后的每一页。
呼!
是否可以按我的意愿更正此存档以分页?我已经阅读了许多其他帖子和关于循环和多个循环分页的抄本(这就是我如何获得我所拥有的),但唉,我所拥有的并没有起作用。
<?php
/**
*
* Display the Tip of the Day Custom Post Type archive custom fields using
* ACF.
*
* @author Angie Meeker
* @uses Advanced Custom Fields
*/
add_action(\'genesis_entry_content\',\'genesis_do_post_title\', 2);
//* Add Tip of the Day body class to the head
add_filter( \'body_class\', \'add_tiparchives_body_class\' );
function add_tiparchives_body_class( $classes ) {
$classes[] = \'tiparchives-post-type-archive-{tipoftheday}\';
return $classes;
}
// Return Category and Tip of the Day on Single Posts
add_action ( \'genesis_before_content\', \'show_totd\', 9 );
function show_totd() {
echo \'<div class="totd-cats-title">Tip of the Day</div>\';
}
// Remove Post Author
add_filter( \'genesis_post_info\', \'remove_post_author_totd_posts\' );
function remove_post_author_totd_posts($post_info) {
$post_info = \'[post_date]\';
return $post_info;
}
/** Replace the standard loop with our custom loop */
remove_action( \'genesis_loop\', \'genesis_do_loop\' );
add_action( \'genesis_loop\', \'totd_do_loop\');
add_action( \'genesis_loop\', \'totd_teasers_do_loop\');
add_action(\'genesis_loop\', \'totd_do_loop\');
function totd_do_loop() {
if ( !is_paged() ) {
global $wp_query;
// Set up your query here
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
$query = array(
\'post_type\' => \'tipoftheday\',
\'orderby\' => \'date\',
\'posts_per_page\' => \'1\',
);
$wp_query = new WP_Query( $query );
if( $wp_query->have_posts() ):
while( $wp_query->have_posts() ): $wp_query->the_post();
// Get the custom fields
// Store the pre tips data
$tips_data_pre = array(
\'totd_tags\' => get_field( \'totd_tags\' ),
\'tip_article_headline\' => get_field( \'tip_article_headline\' ),
\'article_author\' => get_field( \'article_author\' ),
\'article_author_link\' => get_field( \'article_author_link\' ),
);
// Only output if we have tips data
if ($tips_data_pre[\'totd_tags\'] != \'\' ||
$tips_data_pre[\'tip_article_headline\'] != \'\' ||
$tips_data_pre[\'article_author\'] != \'\' ||
$tips_data_pre[\'article_author_link\'] != \'\') {
echo \'<span class="date time published" title="%1$s">\' , do_shortcode(\'[post_date]\'),\'</span>\' ;
echo \'<div class="tip-excerpt"><p><div class="entry-content">\';
echo \'<div class=h1 entry-title"><a href="\'.get_permalink().\'"title="\'.get_the_title().\'">\' . get_the_title() . \'</a></div>\';
echo the_excerpt();
echo \'<div class="entry-terms">\' , do_shortcode(\'[post_terms taxonomy="totd_tags" before="See More Tips For: " taxonomy="totd_tags"] \'),\'</div>\' ;
echo \'<div class="entry-terms">
<div class="share">Share This Tip:</div>
<div class="addthis_toolbox addthis_default_style">
<a class="addthis_button_preferred_1"></a>
<a class="addthis_button_preferred_2"></a>
<a class="addthis_button_preferred_3"></a>
<a class="addthis_button_preferred_4"></a>
<a class="addthis_button_compact"></a>
<a class="addthis_counter addthis_bubble_style"></a>
</div>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=manta"></script>
</div>
</div></div>\';
echo \'</p>\';
}
endwhile;
endif;
// Reset the query
wp_reset_query();
}
}
add_action(\'genesis_loop\', \'totd_teasers_do_loop\');
function totd_teasers_do_loop() {
global $wp_query;
// Set up your query here
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 0;
$query = array(
\'post_type\' => \'tipoftheday\',
\'orderby\' => \'date\',
\'posts_per_page\' => \'10\',
\'offset\' => \'1\',
\'paged\' => $paged,
);
$wp_query = new WP_Query( $query );
if( $wp_query->have_posts() ):
while( $wp_query->have_posts() ): $wp_query->the_post();
// Get the custom fields
// Store the pre tips data
$tips_data_pre = array(
\'totd_tags\' => get_field( \'totd_tags\' ),
\'tip_article_headline\' => get_field( \'tip_article_headline\' ),
\'article_author\' => get_field( \'article_author\' ),
\'article_author_link\' => get_field( \'article_author_link\' ),
);
// Only output if we have tips data
if ($tips_data_pre[\'totd_tags\'] != \'\' ||
$tips_data_pre[\'tip_article_headline\'] != \'\' ||
$tips_data_pre[\'article_author\'] != \'\' ||
$tips_data_pre[\'article_author_link\'] != \'\') {
echo \'<div class="time-teaser">
<span class="day">\',get_the_time( \'l\' ),\'</span>
<span class="month">\',get_the_time( \'m/d/Y\' ),\'</span> </div>\';
echo \'<div class="tip-excerpt"><p><div class="entry-content">\';
echo \'<div class=h2 entry-title"><a href="\'.get_permalink().\'"title="\'.get_the_title().\'">\' . get_the_title() . \'</a></div>\';
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt,25); echo \'<a class="moretag" href="\'. get_permalink($post->ID) . \'"> Continue Reading→</a>\';
echo \'<div class="entry-terms">\' , do_shortcode(\'[post_terms taxonomy="totd_tags" before="See More Tips For: " taxonomy="totd_tags"] \'),\'</div>\' ;
echo \'<div class="entry-terms"></div>
</div></div>\';
echo \'</p>\';
}
endwhile;
// Pagination! Woo!
genesis_posts_nav();
endif;
// Reset the query
wp_reset_query();
}
genesis();
已编辑
我回来是为了展示我的进度,它使用了下面@mneil提供的大部分相同代码(有人在WP.org论坛中也提到了)。分页确实工作正常,第一个post在第一个循环中显示正确,但现在第二个循环没有正确偏移(没有跳过最新的post-与第一个循环中显示的相同)。除此之外,这确实有效。你有没有想过为什么第二个循环现在会跳过偏移柱?
/** Replace the standard loop with our custom loop */
remove_action( \'genesis_loop\', \'genesis_do_loop\' );
add_action( \'genesis_loop\', \'totd_do_loop\');
add_action( \'genesis_loop\', \'totd_teasers_do_loop\');
add_action(\'pre_get_posts\', \'myprefix_query_offset\', 1 );
function myprefix_query_offset(&$query) {
//First, define your desired offset...
$offset = 0;
//Next, determine how many posts per page you want (we\'ll use WordPress\'s settings)
$ppp = 11;
//Next, detect and handle pagination...
if ( $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 );
}
else {
//This is the first page. Just use the offset...
$query->set(\'offset\',$offset);
}
}
add_filter(\'found_posts\', \'myprefix_adjust_offset_pagination\', 1, 2 );
function myprefix_adjust_offset_pagination($found_posts, $query) {
//Define our offset again...
$offset = 0;
//Ensure we\'re modifying the right query object...
//Reduce WordPress\'s found_posts count by the offset...
return $found_posts - $offset;
}
add_action(\'genesis_loop\', \'totd_do_loop\');
function totd_do_loop() {
if ( !is_paged() ) {
global $wp_query;
// Set up your query here
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
$query = array(
\'post_type\' => \'tipoftheday\',
\'orderby\' => \'date\',
\'posts_per_page\' => \'1\',
);
$wp_query = new WP_Query( $query );
if( $wp_query->have_posts() ):
while( $wp_query->have_posts() ): $wp_query->the_post();
$do_not_duplicate = $post->ID;
// Get the custom fields
// Store the pre tips data
$tips_data_pre = array(
\'totd_tags\' => get_field( \'totd_tags\' ),
\'tip_article_headline\' => get_field( \'tip_article_headline\' ),
\'article_author\' => get_field( \'article_author\' ),
\'article_author_link\' => get_field( \'article_author_link\' ),
);
// Only output if we have tips data
if ($tips_data_pre[\'totd_tags\'] != \'\' ||
$tips_data_pre[\'tip_article_headline\'] != \'\' ||
$tips_data_pre[\'article_author\'] != \'\' ||
$tips_data_pre[\'article_author_link\'] != \'\') {
echo \'<span class="date time published" title="%1$s">\' , do_shortcode(\'[post_date]\'),\'</span>\' ;
echo \'<div class="tip-excerpt"><p><div class="entry-content">\';
echo \'<div class=h1 entry-title"><a href="\'.get_permalink().\'"title="\'.get_the_title().\'">\' . get_the_title() . \'</a></div>\';
echo the_excerpt();
echo \'<div class="entry-terms">\' , do_shortcode(\'[post_terms taxonomy="totd_tags" before="See More Tips For: " taxonomy="totd_tags"] \'),\'</div>\' ;
echo \'<div class="entry-terms">
<div class="share">Share This Tip:</div>
<div class="addthis_toolbox addthis_default_style">
<a class="addthis_button_preferred_1"></a>
<a class="addthis_button_preferred_2"></a>
<a class="addthis_button_preferred_3"></a>
<a class="addthis_button_preferred_4"></a>
<a class="addthis_button_compact"></a>
<a class="addthis_counter addthis_bubble_style"></a>
</div>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=manta"></script>
</div>
</div></div>\';
echo \'</p>\';
}
endwhile;
endif;
// Reset the query
wp_reset_query();
}
}
add_action(\'genesis_loop\', \'totd_teasers_do_loop\');
function totd_teasers_do_loop() {
global $wp_query;
// Set up your query here
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 0;
$query = array(
\'post_type\' => \'tipoftheday\',
\'orderby\' => \'date\',
\'posts_per_page\' => \'10\',
\'offset\' => \'1\',
\'paged\' => $paged,
);
$wp_query = new WP_Query( $query );
if( $wp_query->have_posts() ):
while( $wp_query->have_posts() ): $wp_query->the_post();
// Get the custom fields
// Store the pre tips data
$tips_data_pre = array(
\'totd_tags\' => get_field( \'totd_tags\' ),
\'tip_article_headline\' => get_field( \'tip_article_headline\' ),
\'article_author\' => get_field( \'article_author\' ),
\'article_author_link\' => get_field( \'article_author_link\' ),
);
// Only output if we have tips data
if ($tips_data_pre[\'totd_tags\'] != \'\' ||
$tips_data_pre[\'tip_article_headline\'] != \'\' ||
$tips_data_pre[\'article_author\'] != \'\' ||
$tips_data_pre[\'article_author_link\'] != \'\') {
echo \'<div class="time-teaser">
<span class="day">\',get_the_time( \'l\' ),\'</span>
<span class="month">\',get_the_time( \'m/d/Y\' ),\'</span> </div>\';
echo \'<div class="tip-excerpt"><p><div class="entry-content">\';
echo \'<div class=h2 entry-title"><a href="\'.get_permalink().\'"title="\'.get_the_title().\'">\' . get_the_title() . \'</a></div>\';
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt,25); echo \'<a class="moretag" href="\'. get_permalink($post->ID) . \'"> Continue Reading→</a>\';
echo \'<div class="entry-terms">\' , do_shortcode(\'[post_terms taxonomy="totd_tags" before="See More Tips For: " taxonomy="totd_tags"] \'),\'</div>\' ;
echo \'<div class="entry-terms"></div>
</div></div>\';
echo \'</p>\';
}
endwhile;
// Pagination! Woo!
genesis_posts_nav();
endif;
// Reset the query
wp_reset_query();
}
SO网友:mneil
首先,在我看来,第一个循环只会获取当天帖子类型tip的第一篇帖子,然后第二个循环会获取剩下的减去第一篇的帖子,因此,除非我误解了你的循环的功能,否则我只会执行一个循环,检查它是否是第一次迭代,如果是的话,为第一篇帖子获取单独的数据,然后在所有其他迭代中获取你需要的其余数据。此外,作为一种双重安全措施,在检查是否是循环的第一次迭代时,还要检查以确保它没有分页,即它位于结果的第一页,而不是第2、3页等。这应该只是简单地检查$wp\\u query->is\\u paged
免责声明:在基本上写完这个长答案后,我意识到它几乎不起作用,因为过滤到pre\\u get\\u posts会影响包括get\\u posts()函数在内的所有查询。我决定留下完整的答案,因为其中的一些信息可能会对你有所帮助,如果稍加编辑,可能会解决你的问题。然而,我相信我上面写的是最简单的解决方案,除非我完全误解了你的意图。
然而,我会假设您需要两个独立的循环,因为其他原因,我的答案也是如此。我没有对此进行任何测试,所以您必须对其进行测试,看看它是否有效,但问题是您正在使用offest,这将破坏分页。Wordpress建议您必须手动处理分页。http://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
另外,我将假设第二个循环中每页只有帖子=1,即您只希望归档文件中的第一篇帖子是那种类型的,等等。
首先让我们按照wordpress建议(为您修改)使用偏移量解决分页问题。
add_action(\'pre_get_posts\', \'myprefix_query_offset\', 1 );
function myprefix_query_offset(&$query) {
// Before anything else, make sure this is the right page...
// Assuming this is a page. if its an archive, etc. choose the correct conditional
// tag
if ( ! is_page(\'totd\') ) {
return;
}
//First, define your desired offset...
$offset = 1;
//Next, determine how many posts per page you want
$ppp = 10;
//Next, detect and handle pagination...
if ( $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 );
}
else {
//This is the first page. Just use the offset...
$query->set(\'offset\',$offset);
}
}
下一个wordpress告诉我们修改found posts变量,因为它不考虑您的偏移量
add_filter(\'found_posts\', \'myprefix_adjust_offset_pagination\', 1, 2 );
function myprefix_adjust_offset_pagination($found_posts, $query) {
//Define our offset again...
$offset = 1;
// make sure this is the right page...
// Assuming this is a page. if its an archive, etc. choose the correct conditional
// tag
if ( is_page(\'totd\') ) {
//Reduce WordPress\'s found_posts count by the offset...
return $found_posts - $offset;
}
}
现在我很确定,因为pre\\u get\\u posts会为每个posts查询触发,并且我们正在检查它是否是totd页面,它会为两个you\'s循环触发,所以我现在要做的是过滤到\\u posts中,然后将该post插入$posts数组的前面。
function myprefix_add_first_post($posts){
global $wp_query;
// make sure this is the right page...
// Assuming this is a page. if its an archive, etc. choose the correct conditional
// tag, as well as make sure we are not on page 2, 3 etc of pagination
if(!is_admin() && is_page(\'totd\') && !($wp_query->is_paged)){
$query_args = array(
\'post_type\' => \'tipoftheday\',
\'orderby\' => \'date\',
\'posts_per_page\' => \'1\',
);
$first_post = get_posts($query_args);
$all_posts = array_unshift($posts, $first_post[0])
return $all_posts;
}
else{
return $posts;
}
}
add_filter(\'the_posts\', \'myprefix_add_first_post\', 1);
这应该抓住第一个帖子,把它扔进去。您仍然需要检查循环的第一次迭代,并相应地设置其样式。(警告:正如顶部的免责声明所述,pre\\u get\\u posts仍会过滤get\\u posts(),因此如果不进行进一步编辑,上述内容可能无法正常工作。)
SO网友:angiemeeker
以下是最终有效的代码:
/** Replace the standard loop with our custom loop */
remove_action( \'genesis_loop\', \'genesis_do_loop\' );
add_action( \'genesis_loop\', \'totd_do_loop\');
add_action( \'genesis_loop\', \'totd_teasers_do_loop\');
add_action(\'pre_get_posts\', \'myprefix_query_offset\', 1 );
function myprefix_query_offset(&$query) {
//First, define your desired offset...
$offset = $query->get(\'offset\');
//Next, determine how many posts per page you want (we\'ll use WordPress\'s settings)
$ppp = 10;
//Next, detect and handle pagination...
if ( $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 );
}
else {
//This is the first page. Just use the offset...
$query->set(\'offset\',$offset);
}
}
add_filter(\'found_posts\', \'myprefix_adjust_offset_pagination\', 1, 2 );
function myprefix_adjust_offset_pagination($found_posts, $query) {
//Define our offset again...
$offset = $query->get(\'offset\');
//Reduce WordPress\'s found_posts count by the offset...
return $found_posts - $offset;
}
add_action(\'genesis_loop\', \'totd_do_loop\');
function totd_do_loop() {
if ( !is_paged() ) {
global $wp_query;
// Set up your query here
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
$query = array(
\'post_type\' => \'tipoftheday\',
\'orderby\' => \'date\',
\'posts_per_page\' => \'1\',
);
$wp_query = new WP_Query( $query );
if( $wp_query->have_posts() ):
while( $wp_query->have_posts() ): $wp_query->the_post();
// Get the custom fields
// Store the pre tips data
$tips_data_pre = array(
\'totd_tags\' => get_field( \'totd_tags\' ),
\'tip_article_headline\' => get_field( \'tip_article_headline\' ),
\'article_author\' => get_field( \'article_author\' ),
\'article_author_link\' => get_field( \'article_author_link\' ),
);
// Only output if we have tips data
if ($tips_data_pre[\'totd_tags\'] != \'\' ||
$tips_data_pre[\'tip_article_headline\'] != \'\' ||
$tips_data_pre[\'article_author\'] != \'\' ||
$tips_data_pre[\'article_author_link\'] != \'\') {
echo \'<span class="date time published" title="%1$s">\' , do_shortcode(\'[post_date]\'),\'</span>\' ;
echo \'<div class="tip-excerpt"><p><div class="entry-content">\';
echo \'<div class=h1 entry-title"><a href="\'.get_permalink().\'"title="\'.get_the_title().\'">\' . get_the_title() . \'</a></div>\';
echo the_excerpt();
echo \'<div class="entry-terms">\' , do_shortcode(\'[post_terms taxonomy="totd_tags" before="See More Tips For: " taxonomy="totd_tags"] \'),\'</div>\' ;
echo \'<div class="entry-terms">
<div class="share">Share This Tip:</div>
<div class="addthis_toolbox addthis_default_style">
<a class="addthis_button_preferred_1"></a>
<a class="addthis_button_preferred_2"></a>
<a class="addthis_button_preferred_3"></a>
<a class="addthis_button_preferred_4"></a>
<a class="addthis_button_compact"></a>
<a class="addthis_counter addthis_bubble_style"></a>
</div>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=manta"></script>
</div>
</div></div>\';
echo \'</p>\';
}
endwhile;
endif;
// Reset the query
wp_reset_query();
}
}
add_action(\'genesis_loop\', \'totd_teasers_do_loop\');
function totd_teasers_do_loop() {
global $wp_query;
// Set up your query here
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 0;
$query = array(
\'post_type\' => \'tipoftheday\',
\'orderby\' => \'date\',
\'posts_per_page\' => \'10\',
\'offset\' => \'1\',
\'paged\' => $paged,
);
$wp_query = new WP_Query( $query );
if( $wp_query->have_posts() ):
while( $wp_query->have_posts() ): $wp_query->the_post();
// Get the custom fields
// Store the pre tips data
$tips_data_pre = array(
\'totd_tags\' => get_field( \'totd_tags\' ),
\'tip_article_headline\' => get_field( \'tip_article_headline\' ),
\'article_author\' => get_field( \'article_author\' ),
\'article_author_link\' => get_field( \'article_author_link\' ),
);
// Only output if we have tips data
if ($tips_data_pre[\'totd_tags\'] != \'\' ||
$tips_data_pre[\'tip_article_headline\'] != \'\' ||
$tips_data_pre[\'article_author\'] != \'\' ||
$tips_data_pre[\'article_author_link\'] != \'\') {
echo \'<div class="time-teaser">
<span class="day">\',get_the_time( \'l\' ),\'</span>
<span class="month">\',get_the_time( \'m/d/Y\' ),\'</span> </div>\';
echo \'<div class="tip-excerpt"><p><div class="entry-content">\';
echo \'<div class=h2 entry-title"><a href="\'.get_permalink().\'"title="\'.get_the_title().\'">\' . get_the_title() . \'</a></div>\';
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt,25); echo \'<a class="moretag" href="\'. get_permalink($post->ID) . \'"> Continue Reading→</a>\';
echo \'<div class="entry-terms">\' , do_shortcode(\'[post_terms taxonomy="totd_tags" before="See More Tips For: " taxonomy="totd_tags"] \'),\'</div>\' ;
echo \'<div class="entry-terms"></div>
</div></div>\';
echo \'</p>\';
}
endwhile;
// Pagination! Woo!
genesis_posts_nav();
endif;
// Reset the query
wp_reset_query();
}