由于样式问题,这种方法变得有点像tameletjie,让我们对所有内容进行返工,看看不同的方法
我认为我们应该做的是$posts
阵列通过usort()
按照我们想要的顺序对帖子进行排序,然后我们可以正常运行循环
创建我们的查询:
<?php
$args = array(
\'tax_query\' => array(
array(
\'taxonomy\' => \'post-status\',
\'field\' => \'slug\',
\'terms\' => array (\'post-status-published\')
)
)
);
$query = new WP_Query( $args );
现在我们已经运行了查询,我们需要获取
$posts
属性来自
$query
并对其进行排序。因此,这一部分将位于我们的查询下方:(
NOTE: 这完全没有经过测试
if ( $query->have_posts() ) :
// Now we will use usort to sort the posts array
@usort( $query->posts, function ( $a, $b ) // @ hides the bug before PHP 7
{
// Assign sortable values for categories to sort posts by
$array = [\'a\' => $a, \'b\' => $b];
$sort_order_a = \'\';
$sort_order_b = \'\';
foreach ( $array as $key=>$p ) {
if ( in_category( \'First\', $p ) ) {
${\'sort_order_\' . $key} = 1;
} elseif ( in_category( \'Second\', $p ) ) {
${\'sort_order_\' . $key} = 2;
} else {
${\'sort_order_\' . $key} = 3;
}
}
// Now that we have a custom sorting order, lets sort the posts
if ( $sort_order_a !== $sort_order_b ) {
// Make sure about <, change to > to change sort order
return $sort_order_a < $sort_order_b;
} else {
/**
* Sort by date if sorting order is the same,
* again, to change sort order, change > to <
*/
return $a->post_date > $b->post_date;
}
});
// Our posts is now sorted, run the loop normally
// Define our counter to set our columns
$counter = 0;
$midpoint = ceil( $query->post_count / 2 );
while ( $query->have_posts() ) :
$query->the_post();
// set our left div on first post
if ( 0 == $counter ) :
?>
<div id="left">
<?php
endif;
// Close our left div and open the right one on midpoint
if ( ( $counter + 1 ) == $midpoint ) :
<?php
</div>
<div id="right">
<?php
endif;
// Lets output the posts with different styling
if ( in_category( \'First\', get_the_ID() ) {
// Apply styling for category First and display title
the_title();
} elseif ( in_category( \'First\', get_the_ID() ) {
// Apply styling for category Second and display title
the_title();
} else {
// Apply styling for other posts and display title
the_title();
}
/**
* Lets close the div and bail if we have only one post,
* or if we are on the last post
*/
if ( 1 == $post_count
|| ( $counter + 1 ) == $post_count
) :
?>
</div>
<?php
endif;
// Update our counter
$counter++;
endwhile;
wp_reset_postdata();
endif;
原始答案您的第二段代码不起作用,超出了您之前问题的全部原因。请注意,您不应该使用
$wp_query
作为局部变量,它是一个保留的全局变量,专门用于WordPress中保存主查询对象。打破这一全球格局将打破许多其他东西。你在做什么
query_posts()
是的,这也是为什么您永远不应该使用查询帖子。
你的问题是你的计数器被错误地更新了,因此它错误地添加了你的div。我们需要做的是只在有条件的声明中更新我们的意见,以防止职位被重复计算,从而破坏布局。
我们将只使用第一个循环并相应地修改它。唯一棘手的部分是何时打开和关闭div
编辑
由于代码有点技术性,我放弃了我原来的想法。我们仍将使用第一块中的代码,除了像以前一样循环浏览帖子并创建一个新数组,最后循环浏览该数组并显示帖子会容易得多。
<?php
$args = array(
\'tax_query\' => array(
array(
\'taxonomy\' => \'post-status\',
\'field\' => \'slug\',
\'terms\' => array (\'post-status-published\')
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
/**
* Get the amount of posts in the loop, this will be used
* to calculate when to open and close our divs
*/
$post_count = $query->post_count;
/**
* Get a midpoint to break the div into left and right
*/
$midpoint = ceil( $post_count / 2 );
$duplicates = [];
$post_titles = [];
while ( $query->have_posts() ) :
$query->the_post();
if ( in_category( \'First\' ) ) :
$post_titles[] = apply_filters( \'the_title\', get_the_title() );
$duplicates[] = get_the_ID();
endif;
endwhile;
$query->rewind_posts();
while ( $query->have_posts() ) :
$query->the_post();
if ( in_category( \'Second\' ) ) :
if ( in_array( get_the_ID(), $duplicates ) )
continue;
$post_titles[] = apply_filters( \'the_title\', get_the_title() );
$duplicates[] = get_the_ID();
endif;
endwhile;
$query->rewind_posts();
while ( $query->have_posts() ) :
$query->the_post();
if ( in_array( get_the_ID(), $duplicates ) )
continue;
$post_titles[] = apply_filters( \'the_title\', get_the_title() );
endwhile;
wp_reset_postdata();
// Now that we have an array of post titles sorted, lets display them
foreach ( $post_titles as $key=>$post_title ) :
// Open our left div
if ( 0 == $key ) :
?>
<div id="left">
<?php
endif;
// Close our left div and open the right one on midpoint
if ( ( $key + 1 ) == $midpoint ) :
<?php
</div>
<div id="right">
<?php
endif;
// Display the post title
echo $post_title . \'</br>\';
/**
* Lets close the div and bail if we have only one post,
* or if we are on the last post
*/
if ( 1 == $post_count
|| ( $key + 1 ) == $post_count
) :
?>
</div>
<?php
break;
endif;
endforeach;
endif;
?>
重要注意事项:Al代码未经测试,可以根据需要进行改进。由于未经测试,可能会有轻微的语法错误或小错误