• I’m trying to mod the visionary theme in wordpress so that the homepage displays 7 specific posts/pages on the front page. these pages would be static and not change. The idea is they’d take the place of the latest news spots.
    My question is, can I use an array to call out specific post IDs all at once (in a loop)? If so, how would I go about doing this?
    I’ve read the get_post function/reference on the codex but I’m still none the wiser.

    I’ve been given this solution, but I’m wondering if there is a quicker/neater way of achieving the same thing?

    <?php
    $i = 1;
    while($i <= 7) {
    	switch($i) {
    	case 1:
    		$my_id = 351;
    		break;
    	case 2:
    		$my_id = 351;
    		break;
    	case 3:
    		$my_id = 351;
    		break;
    	case 4:
    		$my_id = 351;
    		break;
    	case 5:
    		$my_id = 351;
    		break;
    	case 6:
    		$my_id = 351;
    		break;
    	case 7:
    		$my_id = 351;
    		break;
    	default:
    		echo "No Posts!";
    		break;
    		}		
    
    	$post_id = get_post($my_id);
    	$title = $post_id->post_title;
    	$content = $post_id->post_content;
    
    	echo '<div class="post">';
    	echo '<h2 class="post-title">';
    	echo '<a href="' . get_permalink(351) . ' " title="' . $title . ' ">' . $title . '</a>';
    	echo '</h2>';
    	echo '<div class="entry">' . $content . '</div>';
    	echo '</div>';
    	$i++;
    	}
    ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • Someone really likes switch statements! ;)

    This will do the same, but benefits from a single query to the database (whereas using get_post() will generate a query for each *new* post ID).

    <?php
    $post_ids = '351, 352, 353, 354, 355, 356, 357';
    $query_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID IN ($post_ids) AND post_status = 'publish' ORDER BY post_date DESC");
    foreach( $query_posts as $post) : setup_postdata($post);
    ?>
    <div class="post">
    <h2 class="post-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    <div class="entry"><?php the_content(); ?></div>
    </div>
    <?php endforeach; ?>
    Thread Starter swollenpickles

    (@swollenpickles)

    Cool, thanks. Only problem is, I’m hot sure how to include that in the code to get it to work.

    Here’s what I’m working with at the moment:

    <h2 class="section-header">Recent Headlines &raquo;</h2>
    
    <?php
    // THE SECOND LOOP SHOWING THE ARTICLES AFTER THE FEATURE
    if (have_posts()) :
    $i = 1;
    while (have_posts()) : the_post();
    if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts);
    
    // CHECKS TO SEE IF THERE'S AN IMAGE FOR THIS POST
    $thumb = get_post_meta($post->ID, 'Thumbnail', $single = true);
    // CHECKS TO SEE IF THERE'S A SET ALT TEXT FOR THIS IMAGE
    $thumb_alt = get_post_meta($post->ID, 'Thumbnail Alt', $single = true);
    ?>
    
    <?php // for controlling the layout of the home pages bottom excerpts up to 10 posts
    if($i == '1' || $i == '3' || $i == '5' || $i == '7' || $i == '9') { ?>
    <div class="secondary">
    <?php } // endif ?>
    
    	<div class="<?php if($i % 2 == 0) { echo 'post-right'; } else { echo 'post-left';} ?>">
    	<div id="post-<?php the_ID(); ?>" class="post">
    	<h2 class="post-title"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    	<div class="entry">
    	<?php if($thumb !== '') {
    		?>
    		<p>
    		<img src="<?php echo $thumb?>" alt="<?php if($thumb_alt !== '') echo $thumb_alt; else echo the_title; ?>" class="left" />
    		</p>
    	<?php } ?>
    	<p class="byline"><?php the_time('F jS, Y') ?></p>
    	<?php the_excerpt(); ?>
    	<p><?php edit_post_link('Edit', '', ' | '); ?><?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?> | <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">Read full story &raquo;</a></p>
    	</div><!-- entry -->
    	</div><!-- post -->
    	</div><!-- post left or right -->
    
    <?php // for controlling the layout of the home pages bottom excerpts up to 10 posts
    if($i == '2' || $i == '4' || $i == '6' || $i == '8' || $i == '10') { ?>
    </div><!-- secondary -->
    <?php } // endif ?>
    <?php $i++; ?>
    <?php endwhile; endif; ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problem displaying specific pages/posts’ is closed to new replies.