很抱歉问了这个愚蠢的问题,但我真的迷路了。我想将以下CSS应用于我的foreach的偶数元素。
CSS
.blogArticles a {
float: left;
max-width: 250px;
width: 25%;
color: #333;
}
.blogArticles a:nth-child(even) {
margin: 30px 0 -30px 0;
}
PHP UPDATED (I used snap.svg to render the SVG)<section id="blogArticles" class="blogArticles clearfix">
<?php
$page = (isset($_GET[\'paged\'])) ? $_GET[\'paged\'] : 1;
$args = array(
\'paged\' => $paged,
\'orderby\' => \'id\',
\'order\' => \'DESC\',
\'cat\' => 50,
\'posts_per_page\' => 20,
\'post_status\' => \'publish\',
\'suppress_filters\' => true
);
$blogPosts = query_posts( $args );
if ( have_posts() ) : while ( have_posts() ) : the_post();
$categories = get_the_category();
$count = 0;
if ($categories) {
foreach($categories as $category) {
$class = ($count%2 == 0) ? "even" : "";
?>
<a href="#" class="mix <?php echo $category->name; ?> <?php echo $class; ?>" target="_self" onclick="return false;" data-path-hover="m 0,0 0,100 c 24.580441,3.12569 55.897012,-8.199417 90,-8.199417 34.10299,0 65.41956,11.325107 90,8.199417 L 180,0 z">
<figure>
<?php if ( has_post_thumbnail()) : // Check if Thumbnail exists ?>
<?php the_post_thumbnail(); // Fullsize image for the single post ?>
<?php endif; ?>
<svg viewBox="0 0 180 320" preserveAspectRatio="none"><path d="m 0,0 0,171.14385 c 24.580441,15.47138 55.897012,24.75772 90,24.75772 34.10299,0 65.41956,-9.28634 90,-24.75772 L 180,0 0,0 z"/></svg>
<figcaption>
<h2><?php the_title(); ?></h2>
<p class="label <?php echo $category->name; ?>"><?php echo $category->name; ?></p>
<button>View</button>
</figcaption>
</figure>
</a>
<?php
$count++;
}
}
?>
<?php endwhile; ?>
</section>
<div id="pagination">
<?php html5wp_pagination(); ?>
</div>
function.php
function html5wp_pagination()
{
global $wp_query;
$big = 999999999;
echo paginate_links(array(
\'base\' => str_replace($big, \'%#%\', get_pagenum_link($big)),
\'format\' => \'?paged=%#%\',
\'current\' => max(1, get_query_var(\'paged\')),
\'total\' => $wp_query->max_num_pages
));
}
问题是,在foreach中
$class
是均匀的!有没有关于我错在哪里的线索?提前感谢您!
<小时>
If you have problem, as me, displaying the suggested great pagination function, here is the code fixed for the pagination of @Pieter Goosen
<?php $count = 1; ?>
<?php $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$args = array(\'paged\' => $paged,........);
$blogPosts = new WP_Query( $args );
if ( $blogPosts->have_posts() ) : ....... while ( $blogPosts->have_posts() ) : $blogPosts->the_post();
........
........
........
}
?>
<?php $count = $count+1; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<div class="clear"></div>
<?php custom_pagination($blogPosts->max_num_pages); ?>
最合适的回答,由SO网友:Pieter Goosen 整理而成
首先,你不应该使用query_posts
构造自定义查询。这不仅是我的重点,也是抄本。最大的问题是query_posts
在许多情况下,分页都会失败
Note: 此功能不适用于插件或主题。如后文所述,有更好、性能更好的选项来更改主查询。query\\u posts()是一种过于简单且有问题的方法,通过将页面的主查询替换为新的查询实例来修改它。它效率低下(重新运行SQL查询),并且在某些情况下会彻底失败(尤其是在处理POST分页时)。
值得注意的是next_posts_link
$max_pages
应为自定义查询设置参数,否则将失败
您应该始终使用WP_Query
. 因此,您的自定义查询应该如下所示
$paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;
$args = array(
\'paged\' => $paged,
\'orderby\' => \'ID\',
\'order\' => \'DESC\',
\'cat\' => 50,
\'posts_per_page\' => 20,
\'post_status\' => \'publish\',
\'suppress_filters\' => true
);
$blogPosts = new WP_Query( $args );
if ( $blogPosts->have_posts() ) : while ( $blogPosts->have_posts() ) : $blogPosts->the_post();
<----YOUR LOOP---->
endwhile;
// next_posts_link() usage with max_num_pages
next_posts_link( \'Older Entries\', $blogPosts->max_num_pages );
previous_posts_link( \'Newer Entries\' );
wp_reset_postdata();
endif;
最后,计数器应从循环外部开始,而不是从内部开始
if ( $blogPosts->have_posts() ) :
$count = 0;
while ( $blogPosts->have_posts() ) : $blogPosts->the_post();
EDIT
自定义分页功能。我不知道这个函数的原作者是谁,但如果你能找到这个答案,那就感谢你:-)
function custom_pagination($pages = \'\', $range = 2) {
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == \'\')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
$string = _x( \'Page %1$s of %2$s\' , \'%1$s = current page, %2$s = all pages\' , \'pietergoosen\' );
echo "<div class=\'pagination\'><span>" . sprintf( $string, $paged, $pages ) . "</span>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href=\'".get_pagenum_link(1)."\'>" . __( \'« First\', \'pietergoosen\' ) . "</a>";
if($paged > 1 && $showitems < $pages) echo "<a href=\'".get_pagenum_link($paged - 1)."\'>" . __( \'‹ Previous\', \'pietergoosen\' ) . "</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class=\\"current\\">".$i."</span>":"<a href=\'".get_pagenum_link($i)."\' class=\\"inactive\\">".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href=\'" . get_pagenum_link($paged + 1)."\'>" . __( \'Next ›\', \'pietergoosen\' ) . "</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href=\'".get_pagenum_link($pages)."\'>" . __( \'Last »\', \'pietergoosen\' ) . "</a>";
echo "</div>\\n";
}
} //