简体   繁体   中英

Related Posts with Numbers Wordpress

I want to add numbers in posts to get the output below:

在此处输入图片说明

 <?php 
                        $args= array(
                            'posts_per_page'=>8,
                            'date_query' => array(
                                array(
        'year' => date( 'Y' ),
        'week' => date( 'W' ),
    ),
),
                            'meta_key' => 'popular_posts',
                            'order' => 'DESC',
                            'orderby' => 'meta_value_num'

                            );
                            $i=0;
                            $funLoop = new WP_Query( $args );
                            while ($funLoop -> have_posts() ) : $funLoop->the_post(); $i++;
                            $cat = get_the_category($post->ID)[0];
                        ?>

 <div class="small-12 medium-6 columns unpadded"> <article class="small-12 med columns articles-<?php echo$cat->slug; ?>"> <a href="<?php the_permalink();?>"> <div class="number"> 1. </div> 

The best method is to wrap your content in <ol> . But if you cant you can make usage of the css counter.

Example: Your html:

<div class="small-12 medium-6 columns unpadded">
<article class="small-12 med columns articles-<?php echo$cat->slug; ?>">
<a href="<?php the_permalink();?>">
<div class="number">

</div>
</a>
<a href="<?php the_permalink();?>">
<div class="number">

</div>
</a>
<a href="<?php the_permalink();?>">
<div class="number">

</div>
</a>
</article>

CSS:

    body {
      counter-reset: mynum;
    }

    .artikulli a .number {
      counter-increment: mynum;
    }
   .artikulli a .number:before {
      content: counter(mynum)".";
    }

Working codepen: https://codepen.io/anon/pen/VBgKrW

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM