• There is a spot on my clients site for him to add a featured employee and have it show up in a little spot on the home page.

    I have a file called inc_tophome.php that is used on the home page of my site. In that page i added this code:

    <?php query_posts('category_name=featured-employee');  ?>
    		<?php while (have_posts()) : the_post(); ?>
            <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    			<a href="<?php the_permalink(); ?>" class="blogtitles" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
                	<div style="width:135px;">
                    	<?php the_post_thumbnail();?>
                    </div>
                    <div style="width:200px;">
                    	<?php the_excerpt(); ?>
                    </div>		</div><!-- #post-## -->
            <?php endwhile; ?>

    It kinda works but it removed my home page content text and duplicates itself. What am I doing wrong?
    Here is the page: Website

Viewing 7 replies - 1 through 7 (of 7 total)
  • Use get_posts() or WP_Query() – not query_posts(). The latter over-writes the main query.

    Thread Starter crobley

    (@crobley)

    When I change it to get_posts it duplicates my home page content text?

    Website

    Try reviewing multiple Loops.

    Thread Starter crobley

    (@crobley)

    After reviewing that I changed it to:

    <?php $my_query = new WP_Query('category_name=featured-employee&posts_per_page=1'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
      <!-- Do special_cat stuff... -->
    <?php endwhile; ?>

    But now nothing shows up at all in that box. Very strange.

    Thread Starter crobley

    (@crobley)

    Maybe there is a simpler way to do this.
    Perhaps a widget that displays a category post and thumbnail in the sidebar :/

    I can’t see anything wrong with that code – assuming you really do have another Loop in place of <!-- Do special_cat stuff... -->.

    Thread Starter crobley

    (@crobley)

    OK I think I have a pretty decent grasp of how multiple loops work but I’ve run into a little issue that I thought I had fixed.

    When I have a single post in the category “Blog” it shows up on my home page just fine. However, when I add a second post into the “Blog” category – then the loop doesn’t seem to end. I end up with my home page repeating the 2 posts 100 times.

    I thought I could eliminate this problem with a posts_per_page=2 but no luck.

    <?php query_posts('category_name=blog&posts_per_page=2');  ?>
    <?php if (have_posts()) : the_post(); ?>
         <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
         <span style="font-size:14px;"><a href="<?php the_permalink(); ?>" class="blogtitles" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></span>
          <div style="width:128px; float:left">
              <?php the_post_thumbnail();?>
          </div>
          <div style="width:700px; min-height:40px">
               <?php the_excerpt(); ?>
          </div>
          </div><!-- #post-## -->
          <?php endif; ?>
          <?php endwhile; // end of the loop. ?>

    What am I missing?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Show single post from category in an included php file?’ is closed to new replies.