WordPress.org

Ready to get started?Download WordPress

Forums

make only specific post appear on front page (4 posts)

  1. graham85
    Member
    Posted 1 year ago #

    hi im trying to make site only show four specific posts on the homepage.
    any help would be appreciated. the code is

    <?php { echo ''; } ?>
    <?php } else { include(TEMPLATEPATH . '/includes/slide.php'); } ?>
    
    	<?php if (have_posts()) : ?>
    
    		<?php while (have_posts()) : the_post();
            $c++; // increment the counter
             if( $c % 2 != 0) {
          	   $extra_class = 'leftpost';
               } else {
               $extra_class = 'rightpost';
               }
            ?>
    
    			<div <?php post_class($extra_class) ?> id="post-<?php the_ID(); ?>">
    
                  <?php if ( has_post_thumbnail() ) { ?>
                    <?php if ( function_exists( 'get_the_image' ) ) {
                    get_the_image( array( 'custom_key' => array( 'post_thumbnail' ), 'default_size' => 'full', 'image_class' => 'aligncenter', 'width' => '299', 'height' => '149' ) ); }?>
                    <?php comments_popup_link('0', '1', '%', 'comm'); ?>
                  <?php } ?>
    
    				<h2 class="title"><a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    
    				<div class="entry">
    					<?php //the_content(''); ?>
    					<?php truncate_post(150, true);  ?>
    				</div>
    
                    <div class="meta">
                     <?php the_time('F d, Y'); ?>
                     in <?php the_category(', ') ?>
                    </div>
    			</div>
    
                <?php if(++$counter % 2 == 0) : ?>
               <div class="clearp"></div>
              <?php endif; ?>
    
    		<?php endwhile; ?>
    
    		<div class="navigation">
            <?php
                include('includes/wp-pagenavi.php');
                if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
            ?>
    		</div>
    
    	<?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    		<?php get_search_form(); ?>
    
    	<?php endif; ?>

    [the above code is partly broken because is was posted without being properly marked as code - see http://codex.wordpress.org/Forum_Welcome#Posting_Code ]

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    only show four specific posts on the homepage

    how are you going to define 'specific' ?

  3. graham85
    Member
    Posted 1 year ago #

    by their category's. at the moment the homepage shows every post i have i only want 4 specific posts to feature on the homepage.
    regards

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

    try and add this to functions.php of your theme:

    function specific_home_page_posts( $query ) {
        if ( is_admin() || ! $query->is_main_query() )
            return;
    
        if ( is_home() ) {
            // Display specific posts for the original blog archive
            $query->set( 'posts_per_page', 4 );
            $query->set( 'category_name', 'specific' );
            return;
        }
    }
    add_action( 'pre_get_posts', 'specific_home_page_posts', 1 );

    (untested)

Topic Closed

This topic has been closed to new replies.

About this Topic