Forums

[resolved] How to Display 4 Posts on Home Page Except From Featured Category (5 posts)

  1. cinemadiving
    Member
    Posted 2 years ago #

    Hi

    I'm not sure what I'm doing wrong here because this works fine on my local site. I'm trying to launch my first blog and get this error from the home page:

    Warning: Missing argument 1 for WP_Query::query(), called in http://blog.greggwatt.com/wp-content/themes/hybrid-custom/cross-home.php on line 49 and defined in http://blog.greggwatt.com/wp-includes/query.php on line 2512

    I have a custom template for the home page (cross-home.php) to display the first 4 posts except posts from the featured category (ID=3).

    Here is the code from the cross-home.php starting at line 49:

    <?php
    			$wp_query = new WP_Query('showposts=4&cat=-3');
    			/* Show only first four posts on home page */
    			$wp_query->query();
    			$more = 0;
    		?>
    
    		<?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>

    Any idea what I could be doing wrong? I appreciate any help.

    Cheers,
    Gregg

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    Try something like this:

    <?php
      $args=array(
        'cat' => 3,
        'showposts' => 4,
        'caller_get_posts'=> 1
        );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo 'List of: ' . $type .'(s)';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
         <?php
        endwhile;
      } //if ($my_query)
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  3. cinemadiving
    Member
    Posted 2 years ago #

    Hi MichaelH

    Thanks so much for your very prompt response and help. Everything works fine except for my "else" statement which I'm struggling to incorporate into your code. I'm sure that I've got the syntax wrong.

    <?php
      $args=array(
        'cat' => -3,
        'showposts' => 4,
        'caller_get_posts'=> 1
        );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
    
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
    			<div id="post-<?php the_ID(); ?>" class="<?php hybrid_entry_class(); ?>">
    
    				<?php hybrid_before_entry(); // Before entry hook ?>
    
    				<div class="entry-content entry">
    					<?php the_excerpt( __('Continue reading', 'hybrid') . the_title( ' "', '"', false ) ); ?>
    					<?php wp_link_pages( array( 'before' => '<p class="pages">' . __('Pages:', 'hybrid'), 'after' => '</p>' ) ); ?>
    				</div>
    
    				<?php hybrid_after_entry(); // After entry hook ?>
    
    			</div>
         <?php endwhile;
      } //if ($my_query) ?>
    
    <?php else: ?>
    
    			<p class="no-data"><?php _e('Sorry, no posts matched your criteria.', 'hybrid'); ?></p>
    
    <?php endif; ?>  
    
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    
    		<?php hybrid_after_content(); // After content hook ?>
  4. MichaelH
    Volunteer
    Posted 2 years ago #

    For the section beginning with endwhle, ending with wp_reset_query try:

    <?php endwhile;
    
    } else {
    
    <p class="no-data"><?php _e('Sorry, no posts matched your criteria.', 'hybrid'); ?></p>
    
    <?php } ?>  
    
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  5. cinemadiving
    Member
    Posted 2 years ago #

    Hi MichaelH

    You're a star! Thanks so much for your help ... I really appreciate it.

    Cheers,
    Gregg

Topic Closed

This topic has been closed to new replies.

About this Topic