0

I have several pages (not posts) on my Wordpress site. They contain excerpts (I forgot which plugin enabled that) and have an article image.

Now I would like to create another page that contains selected links to those pages. I would like to just select the pages and have those links created automatically with the excerpt text and the picture.

I found a plugin which does that, and it looks ok. It's just that the plugin is quite old and has not been updated for two years or so.

Is there another way to achieve this?

2

You have some alernatives.

First alternative is create a taxonomy for pages using register_taxonomy.

Then, assuming this taxonomy is called 'pages-group' create a template file called taxonomy-pages-group.php and use the loop to display your page as you want.

A second alternative is create a shorcode that you have to put into pages or post content.

Shortcode should look like:

[pages ids="12,23,34,45"]

You should register the shorcode with add_shortcode (codex) function, something like

function add_pages_shortcode() {
  add_shortcode( 'pages' , 'show_pages_shortcode' );
}
add_action('wp_loaded','add_pages_shortcode');

And the create your show_pages_shortcode function that display the selected pages, something like:

function show_pages_shortcode( $atts = array() ) {
  if ( isset($atts['ids']) && ! empty($atts['ids']) ) {
    $pages = get_pages( array('include' => $atts['ids'] ));
    if ( ! empty($pages) ) {
      global $post;
      ob_start();
      foreach ( $pages as $post) {
        setup_postdata($post);
        ?>
        <li>
          <a id="page-<?php the_ID(); ?>" href="<?php the_permalink(); ?>">
            <?php the_title(); ?>
            <?php the_post_thumbnail() ?>
          </a>
          <?php the_excerpt() ?>
        </li>
        <?php
      }
      wp_reset_postdata();
      return '<ul class="pages-list">' . ob_get_clean() . '</ul>';
    }
  }
}

Of course you can change the output as you want.

  • I've chosen the second alternative, and it works. Thank you very much! For completeness, the functions for image and excerpt are the_post_thumbnail and the_excerpt. As I said, I'm not that good in Wordpress or PHP. What I don't get is this: even though I placed the shortcode after some text on my page, the title, image and excerpt get displayed at the start of the page, before that text. What is the reason for this? – nokul Sep 2 '13 at 20:35
  • 1
    @nokul there was a bug in my function, I've corrected it. Try now. – gmazzap Sep 2 '13 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.