我使用以下代码在我的主页上以网格布局显示特定类别的帖子。它的工作方式正是我想要的,但我一直在读,我不应该使用query\\u帖子。如何在不使用query\\u帖子的情况下获得相同的结果?
此外,我最终需要在主页上显示来自十个不同类别的帖子——使用完全相同的网格布局。如果我为每个类别复制了下面的所有代码,会不会导致问题,或者有没有更有效的方法来做到这一点?
如果您有任何建议,我们将不胜感激,因为您可能会从我的代码和问题中了解到,我对WordPress开发还比较陌生:)
<?php
$counter = 1; //start counter
$grids = 3; //Grids per row
global $query_string; //Need this to make pagination work
/*Setting up our custom query (In here we are setting it to show 3 posts per page and eliminate all sticky posts) */
query_posts( array(\'posts_per_page\'=>3, \'category_name\'=>\'Mobile\') );
if(have_posts()) : while(have_posts()) : the_post();
?>
<?php
//Show the left hand side column
if($counter == 1 || $counter == 2) :
?>
<div class="col-cat3">
<div class="entry-featured"><?php x_featured_image(); ?></div>
<div class="col-cat-pic"><?php echo get_avatar( get_the_author_meta(\'ID\'), 40); ?></div>
<div class="hero-info">
<h3><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<p class="p-meta"><?php the_author_posts_link(); ?> / <?php the_time(\'m.d.y\'); ?></p>
</div>
</div>
<?php
//Show the right hand side column
elseif($counter == $grids) :
?>
<div class="col-cat3-last">
<div class="entry-featured"><?php x_featured_image(); ?></div>
<div class="col-cat-pic"><?php echo get_avatar( get_the_author_meta(\'ID\'), 40); ?></div>
<div class="hero-info">
<h3><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<p class="p-meta"><?php the_author_posts_link(); ?> / <?php the_time(\'m.d.y\'); ?></p>
</div>
</div>
<?php
$counter = 0;
endif;
?>
<?php
$counter++;
endwhile;
//Pagination can go here if you want it.
endif;
?>