I use the Frontpage.php to display three posts from specific categories in a WP_Query The articles in the query are displayed randomly.
But now I have thought about it, if I write an important and new post in one of these categories and this post should become a sticky post for a while, the whole query doesn't work anymore. It would be perfect if the sticky post could be seen at the top and the other two posts could still be displayed randomly. Do you have any idea how I can do that. I know that the sticky-posts in category archives can't be displayed that easily. But maybe one of you has an idea...
<?php
$args = array(
'cat'=>'11, 12,20,24',
'posts_per_page'=> '3',
'orderby'=> 'rand'
);
$knowledgebase_fp_Posts = new WP_Query($args);
if ( $knowledgebase_fp_Posts-> have_posts() ) :
$counter=0;
else :
$counter=1;
endif;
while ( $knowledgebase_fp_Posts-> have_posts() ) : $knowledgebase_fp_Posts->the_post(); ?>
<?php if ($counter==0): ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('container single-news vertical mb-4 nopadding-lr' ); ?>>
<div class="mb-2 vertical-img ">
<div class="embed-responsive embed-responsive-4by3">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php if(has_post_thumbnail()) { the_post_thumbnail($size, array('class' => 'img-fluid embed-responsive-item m-auto') );
} else {
echo '<img src="'.get_template_directory_uri() .'/img/placeholder.jpg" class="img-fluid embed-responsive-item m-auto" alt=""/>';
} ?>
</a>
</div>
</div>
<div class="featured-card-block">
<?php the_title( '<h1 class="featured-lead-title" ><a aria-label="Titel" class="text-dark" href="' . esc_url( get_permalink() ) . '" rel="bookmark" >', '</a></h1>' );?>
<div class="entry-meta text-muted">
<ul class="list-inline text-left list-unstyled font-small">
<li class="list-inline-item "><?php ekoo_posted_on();?></li>
<li class="list-inline-item"><?php ekoo_posted_by();?></li>
<li class="list-inline-item"><?php ekoo_comment(); ?></li>
</ul>
</div>
<div class="mt-0">
<p class="featured-lead-summary desktop"><?php echo excerpt(50); ?><span class="screen-reader-text"><?php the_title(); ?></span></p>
<p class="featured-lead-summary tablet"><?php echo excerpt(60); ?><span class="screen-reader-text"><?php the_title(); ?></span></p>
<p class="featured-lead-summary phone"><?php echo excerpt(60); ?><span class="screen-reader-text"><?php the_title(); ?></span></p>
</div>
</div>
</article>
<?php endif;
if ($counter > 0): ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('container single-news horizontal'); ?>>
<div class="row ">
<div class="col-md-4 horizontal-img nopadding">
<div class="mb-2">
<div class="embed-responsive embed-responsive-4by3">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php if(has_post_thumbnail()) { the_post_thumbnail($size, array('class' => 'img-fluid embed-responsive-item m-auto') );
} else {
echo '<img src="'.get_template_directory_uri() .'/img/placeholder.jpg" class="img-fluid embed-responsive-item m-auto" alt=""/>';
} ?>
</a>
</div>
</div>
</div>
<div class="col-md-8 small-news nopadding">
<div class="card-block">
<?php the_title( '<h1 class="featured-title "><a href="' . esc_url( get_permalink() ) . '" rel="bookmark" class="text-dark">', '</a></h1>' );?>
<div class="entry-meta text-muted">
<ul class="list-inline text-left list-unstyled font-small">
<li class="list-inline-item "><?php ekoo_posted_on(); ?></li>
<li class="list-inline-item"><?php ekoo_posted_by(); ?></li>
<li class="list-inline-item"><?php ekoo_comment(); ?></li>
</ul>
</div>
<p class="featured-summary tablet"><?php echo excerpt(30); ?><span class="screen-reader-text"><?php the_title(); ?></span></p>
<p class="featured-summary phone"><?php echo excerpt(60); ?><span class="screen-reader-text"><?php the_title(); ?></span></p>
</div>
</div>
</div>
</article>
<?php endif; ?>
<?php
$counter++;
endwhile; ?>
<?php
wp_reset_query();
wp_reset_postdata();
?>
Aucun commentaire:
Enregistrer un commentaire