lundi 14 mars 2016

the_permalink in loop gives the same results

I need to show an archive list post by post. I created plugin with this code:

function posts_by_year() {
  // array to use for results
  $years = array();

  // get posts from WP
  $posts = get_posts(array(
    'numberposts' => -1,
    'orderby' => 'post_date',
    'order' => 'ASC',
    'post_type' => 'post',
    'post_status' => 'publish'
  ));

  // loop through posts, populating $years arrays
  foreach($posts as $post) {
    $years[date('Y', strtotime($post->post_date))][] = $post;
  }

  // reverse sort by year
  krsort($years);

  return $years;
}

?>


<?php function wyswietl_archiwum (){ ob_start(); ?>

<?php foreach(posts_by_year() as $year => $posts) : ?>
  <h2><?php echo $year; ?></h2>

  <ul>
    <?php foreach($posts as $post) : setup_postdata($post); ?>
      <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
      </li>
    <?php endforeach; ?>
  </ul>
<?php endforeach; ?>
<?php
    return ob_get_clean();
} ?>

<?php add_shortcode( 'archiwum', 'wyswietl_archiwum' );

When I put shortcode [archiwum] on any page, I get the list but every one item displays current page title and links to itself.

screenshot

Any idea how to make it proper?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire