Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to create a template for selecting 5 specific posts to be displayed on a WordPress page.

Essentially I must be able to query 5 exact posts to be displayed on a Wordpress Page using a WP page template and a custom filed to choose the exact 5 post ids.

EDIT: I have this code thanks to this site. But now I need to be able to add the page id array via custom field. Can this be done?

EDIT 2: Actually the code is not displaying the posts in the array, but rather the latest posts on the homepage. Hmmm.

<?php
$post_id = array(483,454,376);
 $posts = get_posts( $post_id);
 foreach( $posts as $post ) :
  setup_postdata($post);  ?> 
     // you can call use post data inside here like
    <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>

                         <a class="thumb" href="<?php the_permalink(); ?>"><?php dp_attachment_image($post->ID, 'thumbnail', 'alt="' . $post->post_title . '" class="hthumb"'); ?></a><?php the_excerpt(45); ?><a href="<?php the_permalink(); ?>" class="more">Read More..</a><div style="clear:both;"></div>
<?php endforeach; ?>
share|improve this question
add comment

4 Answers

Do it like this:

global $post; //this is the post object for the wordpress page

/** Assuming your meta key is called "posts_to_show" */
$post_ids_from_meta = get_post_meta( $post->ID, 'posts_to_show', true );

/** $post_ids_from_meta will contain string of comma delimited post IDs 
    entered from a meta box somewhere in the edit page.
**/

$args = array( 
    'include' => $post_ids_from_meta,
    );

$the_posts = get_posts( $args );

foreach( $the_posts as $post ):
    setup_postdata($post);
    /** DO YOUR STUFF HERE **/
endforeach;
share|improve this answer
    
Please explain why that could solve the problem. –  toscho Jan 16 '13 at 14:40
add comment

in single.php I get two loops, one is to fetch another page elements, and the next loop is the normal loop to fetch a single post:

<?php get_template_part( 'loop', 'panel-news' ); ?>
<?php get_template_part( 'loop', 'single' ); ?>

then in loop-panel-news.php (I use some custom fields -- Advanced Custom Fields plugin):

<?php $pgn = new WP_Query("page_id=31"); while($pgn->have_posts()) : $pgn->the_post();?>
            <div id="panel" role="single-news">
                <div class="featimg"><?php the_post_thumbnail(); ?>
                </div>
                <div class="feattxt">
                    <?php $value = get_field('panel_text');
                    if ($value[0]) { ?>
                    <div class="panelsub"><?php the_field('panel_text'); ?></div>
                    <?php } else { ?>
                    <!-- -->
                    <?php } ?>
                </div>                  
            </div>
            <div id="pagename"><?php the_title(); ?></div>
<?php endwhile; ?>
<?php // Restore original Query & Post Data
wp_reset_query();
wp_reset_postdata();?>

It can insert a page fragment (or full page) anywhere in the template files. Another example, inserting a page fragment (one of three) on the home page (home.php):

            <div class="hpanel one">
            <?php $pg1 = new WP_Query("page_id=$pgid1"); while($pg1->have_posts()) : $pg1->the_post();?>
                <div class="himg">
                    <?php the_post_thumbnail( array(304,304) );  ?>
                </div>
                <div class="hreadmore">
                    <a href="<?php the_permalink(); ?>"></a>
                </div>

                <?php $value = get_field('page_subhead');
                if ($value[0]) { ?>
                <h3 class="hpanelhd"><?php the_field('page_subhead'); ?></h3>
                <?php } else { ?>
                <h3 class="hpanelhd"><?php the_title(); ?></h3>
                <?php } ?>
                <div class="hexcerpt">
                <p><?php the_field('page_excerpt'); ?></p>
                </div>
            <?php endwhile; wp_reset_query(); wp_reset_postdata(); ?>
            </div>
share|improve this answer
add comment

Here's a (rudimentary) page for a loop I made in a webapp on WP a long time ago. I just upload this, create a page, and select "Updates Page" from the page template, and it loads in the 5 posts from the WP_Query() results.

If you want to load 5 EXACT posts, just replace your loop where mine is.

<?php include(CHILD_DIR.'/lib/global_var.php');

    /*
    Template Name: Updates Page
    */
    get_header(); ?>
    <h2>What's New!</h2>
    </div><!-- end the top wrap -->
</div><!-- end top content -->

<div id="primary" class="with-sidebar">
    <div class="c-s-wrap">
        <div id="content" role="main">
<?php


// The Query
$blog_query = new WP_Query( 'cat=4&posts_per_page=10' );
$counter = '0';
// The Loop
while ( $blog_query->have_posts() ) : $blog_query->the_post();
?>
<div class="blog-post">
    <h3><?php printf(the_title()); ?></h3>
    <span class="post-content"><?php printf(the_content()); ?></span>
</div>
<hr />
<?php
endwhile;

// Reset Post Data
wp_reset_postdata();


?>
        </div><!-- #content -->
        <aside id="sidebar">
            <?php include(CHILD_DIR.'/sidebar.php'); ?>
        </aside>
    </div>
    <div style="clear:both;"></div>
</div><!-- #primary -->
<?php get_footer(); ?>
share|improve this answer
    
Some more information can be found HERE about ordering posts specifically –  Des Oct 11 '12 at 17:55
    
Thansk, this also helps but it was a query for specific IDs in page array then for those IDs toi be added using a custom field. I got the code as seen in my edit above, but don't know how to call the custom vode in the page array without breaking the code. –  Mark R Oct 11 '12 at 17:59
add comment

The front-end output is controlled by theme, so you would need to study it for appropriate markup to reuse.

From there you would need to:

  1. Create page template.
  2. Implement secondary loop in it, using WP_Query or get_posts().
share|improve this answer
    
I got this: <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1> <a class="thumb" href="<?php the_permalink(); ?>"><?php dp_attachment_image($post->ID, 'thumbnail', 'alt="' . $post->post_title . '" class="hthumb"'); ?></a><?php the_excerpt(45); ?><a href="<?php the_permalink(); ?>" class="more">Read More..</a><div style="clear:both;"></div> Issue with that is.. Getting it to work inside the already existing page queryt is what's hard.. –  Mark R Oct 11 '12 at 17:47
    
I think to better explain, I don't know how to implement that as a secondary loop. Thanks. –  Mark R Oct 11 '12 at 17:48
    
@Mark R please add any code snippets by editing you original questions, comments are not meant for them. Linked get_posts() documentation has multiple examples of implementation. –  Rarst Oct 11 '12 at 17:50
    
Done this, got it working to an extent thanks to other guys and you, then added to my question the edits. –  Mark R Oct 11 '12 at 18:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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