最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Featured or last post with a different WordPress style without plugin

programmeradmin0浏览0评论

How do I get a highlight with only one category but with a different style for each ?

Exemple:

My Code is:

<?php
                      $args=array(
                      'cat' => 2,
                      'orderby' => 'date',
                      'order' => 'DESC',
                      'post_type' => 'post',
                      'post_status' => 'publish',
                      'posts_per_page' => 3,
                      'caller_get_posts'=> 1
                      );
                      $my_query = null;
                      $my_query = new WP_Query($args);

                      if( $my_query->have_posts() ) { ?>
                      <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
                        <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'larger' ); ?>
                        <div class="col-lg-4 col-md-6">
                            <div class="single-services mb-80">
                                <div class="services-thumb mb-35">
                                    <img src="<?php echo $image[0]; ?>" alt="img">
                                </div>
                                <div class="services-content">
                                    <h4><?php the_title(); ?></h4>
                                    <p><?php the_excerpt(); ?></p>
                                </div>
                            </div>
                        </div>
                      <?php  endwhile; ?>
                      <?php } wp_reset_query(); ?>

How do I get a highlight with only one category but with a different style for each ?

Exemple:

My Code is:

<?php
                      $args=array(
                      'cat' => 2,
                      'orderby' => 'date',
                      'order' => 'DESC',
                      'post_type' => 'post',
                      'post_status' => 'publish',
                      'posts_per_page' => 3,
                      'caller_get_posts'=> 1
                      );
                      $my_query = null;
                      $my_query = new WP_Query($args);

                      if( $my_query->have_posts() ) { ?>
                      <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
                        <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'larger' ); ?>
                        <div class="col-lg-4 col-md-6">
                            <div class="single-services mb-80">
                                <div class="services-thumb mb-35">
                                    <img src="<?php echo $image[0]; ?>" alt="img">
                                </div>
                                <div class="services-content">
                                    <h4><?php the_title(); ?></h4>
                                    <p><?php the_excerpt(); ?></p>
                                </div>
                            </div>
                        </div>
                      <?php  endwhile; ?>
                      <?php } wp_reset_query(); ?>
Share Improve this question asked Jan 19, 2021 at 15:28 Everton GouveiaEverton Gouveia 1011 bronze badge 2
  • Can you explain a bit more? I'm not entirely clear on what you're asking. – Tony Djukic Commented Jan 20, 2021 at 15:29
  • @TonyDjukic I would like to show a list of the last posts with only one category but with different layouts like the example: i.sstatic/4gM7Z.png – Everton Gouveia Commented Jan 24, 2021 at 20:13
Add a comment  | 

1 Answer 1

Reset to default 2

The concept is that you have queried WP for a defined number of posts, so you can presume you have a total or 3 posts returned thanks to 'posts_per_page' => 3,-- remember, this might not be true, is there are not enough posts or other parts of the query reduce this number, perhaps because of the wrong category ID.

Here is some pseudo-code to show how you can affect the markup used for each post you render, using the loop ( the rest of your code would still be required ):

// track post loops ##
$loop = 0;

while ($my_query->have_posts()) : $my_query->the_post(); 
    switch( $loop ) {
        case 0: // $loop = 0
?>
    <h4>Loop One:<?php the_title(); ?></h4>     
<?php  
        break;

        case 1: // $loop == 1 etc..
        default: // works for all other values not defined in switch cases..
?>
    <h4>Loop Two:<?php the_title(); ?></h4>     
<?php  
        break;
    }

// iterate post loop count ##
$loop ++;

endwhile;
发布评论

评论列表(0)

  1. 暂无评论