您好,我正在使用下面的代码从特定帖子类型下的特定类别调用帖子,但我如何设置它的样式,以便使第一篇帖子与其他帖子有所不同。
<?php
$queryObject = new Wp_Query( array(
\'showposts\' => 4,
\'post_type\' => array(\'pretty-little-liars\', \'revenge\', \'once-upon-a-time\'),
\'category_name\' => celebrity,
\'orderby\' => 1,
));
// The Loop!
if ($queryObject->have_posts()) {
?>
<?php
while ($queryObject->have_posts()){
$queryObject->the_post();
?>
<a href="<?php the_permalink(); ?>" title="<?php printf(__( \'Read %s\', \'wpbx\' ), wp_specialchars(get_the_title(), 1)) ?>">
<?php the_post_thumbnail(\'video-post\'); ?>
</a>
<?php echo human_time_diff( get_the_time(\'U\'), current_time(\'timestamp\') ) . \' ago\'; ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
}
?>
<?php wp_reset_postdata();
}
?>
SO网友:Richard Sweeney
如果您不介意添加一点额外的标记,可以将第一篇文章包装在div中,并使用其类以不同的方式设置这篇文章的样式。
<?php
$queryObject = new Wp_Query( array(
\'showposts\' => 4,
\'post_type\' => array(\'pretty-little-liars\', \'revenge\', \'once-upon-a-time\'),
\'category_name\' => celebrity,
\'orderby\' => 1,
));
// The Loop
if ( $queryObject->have_posts() ) :
$i = 0;
while ( $queryObject->have_posts() ) :
$queryObject->the_post();
?>
<?php if ( $i == 0 ) : ?>
<div class="first-post">
<?php endif; ?>
<a href="<?php the_permalink(); ?>" title="<?php printf(__( \'Read %s\', \'wpbx\' ), wp_specialchars(get_the_title(), 1)) ?>">
<?php the_post_thumbnail(\'video-post\'); ?>
</a>
<?php echo human_time_diff( get_the_time(\'U\'), current_time(\'timestamp\') ) . \' ago\'; ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php if ( $i == 0 ) : ?>
</div>
<?php endif; ?>
<?php $i++; ?>
<?php endwhile; ?>
<?php endif; ?>
SO网友:Rohit Pande
您可以这样做:
<?php
$queryObject = new Wp_Query( array(
\'showposts\' => 4,
\'post_type\' => array(\'pretty-little-liars\', \'revenge\', \'once-upon-a-time\'),
\'category_name\' => celebrity,
\'orderby\' => 1,
));
// Initiate some counter to point the first post
$post_counter = 0;
// The Loop!
if ($queryObject->have_posts()) {
while ($queryObject->have_posts()){
$queryObject->the_post();
if(!$post_counter)
{
?>
/* Custom styling for the first post */
<?php
}
else
{
/* common styling for the rest of the posts */
}
$post_counter+=1;
}
wp_reset_postdata();
}
?>