我已经创建了一个 wordpress 帖子的 gridview,我试图在鼠标悬停时显示每个 thumbnail(post)
的摘录,但是如果有任何 thumbnail(post)
不'有任何不应显示的摘录内容。
<?php
$args = array('post_type' => 'press');
$counter = 1; //start counter
$grids = 4; //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 12 posts per page and eliminate all sticky posts) */
query_posts ( $args );//query_posts($query_string . '&caller_get_posts=6&posts_per_page=12');
if(have_posts()) : while(have_posts()) : the_post();
?>
<?php
//Show the left hand side column
if($counter >= 1) :
?>
<div class="griditemleft" style="position:relative;">
<div class="postimage">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('category-thumbnail'); ?></a>
<div class="excerpt"><?php if post have excerpt then display .excerpt on hover else display only the thumbnail?>
</div>
<h2 class="press-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
</div>
<h2 class="press-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
</div>
<?php
//Show the right hand side column
elseif($counter == $grids) :
?>
<div class="clear"></div>
<?php
$counter = 0;
endif;
?>
<?php
$counter++;
endwhile;
//Pagination can go here if you want it.
endif;
?>
最佳答案
如果我没理解错的话,您不想显示没有缩略图的帖子。如果我是对的,那么您应该做的是在 if 语句中添加另一个条件 ( if($counter >= 1) )。将此条件 (get_post_thumbnail() !== ") 添加到当前 if 语句,它应该按照您的意愿执行。示例如下:
if(($counter >= 1 && (get_post_thumbnail() !== ""))
编辑: 阅读完您添加的内容后。我建议您使用 get_the_excerpt() function .如果存在摘录,则返回一个值,否则返回空字符串。查看下面的代码示例:
$my_excerpt = get_the_excerpt();
if ( $my_excerpt != '' ) {
// print excerpt
}
使用这段代码和一些 JavaScript 或 CSS,您应该能够达到您想要的效果。
关于php - 如何仅在悬停摘录的图片帖子上显示 wordpress 帖子摘录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19227089/