【问题标题】:Wp_query for reset_count用于 reset_count 的 wp_query
【发布时间】:2019-09-18 20:30:48
【问题描述】:

我想在类别页面中使用 wp_query 循环并使用下面的代码,但它得到一个错误:“该站点遇到技术问题。” .那么如何改进我的代码呢?感谢您的帮助! [![在此处输入图片描述][1]][1]

<?php if(have_posts()) :  while(have_posts()) :  the_post(); 
?>
<?php
if($counter == 1) :
?>
<div class="col-<?php echo counter; ?>"> //this col have width 50%, height 300px.
  <div class="postimage">
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('project-thumb'); ?></a>
  </div>
  <div class="proj-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
</div>
<?php
elseif($counter == 2) :
?>
<div class="col-<?php echo counter; ?>"> //this col have width 50%, height 150px.
  <div class="postimage">
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('project-thumb'); ?></a>
  </div>
  <div class="proj-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
</div>
<div class="clear"></div>
<?php
elseif($counter == 3) :
?>
<div class="col-<?php echo counter; ?>"> //this col have width 50%, height 150px.
  <div class="postimage">
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('project-thumb'); ?></a>
  </div>
  <div class="proj-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
</div>          
<?php endif; ?>


  [1]: https://i.stack.imgur.com/jgolS.png

【问题讨论】:

  • 你遇到了什么错误,你想要的结果是什么?
  • 这是 html css 模板:codepen.io/tu1988/pen/YzKJbKb ,我想回显 number 来计算 class col-{number} 。我把这段代码放到我的类别模板中,并希望所有的帖子循环都是 mansory

标签: wordpress gridview masonry


【解决方案1】:

我认为代码不错,但需要稍作修改。 您只需要一个砌体库,否则很难使用多个 div 包装器和语句将其保持在所需的形式

https://masonry.desandro.com/

设置完成后,您需要在 css 中为所需的高度定义 1-5 类。

.col-1,.col-2,.col-3,.col-4,.col-5 {width:50%;}
.col-1,.col-2 {height:300;}
.col-3,.col-4,.col-5{height:150px;}

masonry 库为您完成对齐工作

<?php 
$counter = 1;
if(have_posts()) :  while(have_posts()) :  the_post(); 
if($counter == 1) :
?>
<div class="col-<?php echo counter; ?>"> //this col have width 50%, height 300px.
  <div class="postimage">
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('project-thumb'); ?></a>
  </div>
  <div class="proj-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
</div>
<?php
elseif($counter == 2) :
?>
<div class="col-<?php echo counter; ?>"> //this col have width 50%, height 150px.
  <div class="postimage">
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('project-thumb'); ?></a>
  </div>
  <div class="proj-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
</div>
<div class="clear"></div>
<?php
elseif($counter == 3) :
?>
<div class="col-<?php echo counter; ?>"> //this col have width 50%, height 150px.
  <div class="postimage">
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('project-thumb'); ?></a>
  </div>
  <div class="proj-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
</div>          
<?php endif; 
$counter++;
if( $counter == 5 ): $counter = 1; endif;
endwhile; endif;
?>

【讨论】:

  • 是的,它工作,谢谢,但它只显示 5post,那么如何循环继续行?我有 5 列,希望从 6 开始的帖子将应用 col-1、7 用于 col-2 ... 等等。
猜你喜欢
  • 2013-01-20
  • 2020-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-16
  • 2017-04-12
  • 2017-09-07
相关资源
最近更新 更多