WordPress.org

Forums

Prblems in advanced loop customizing (6 posts)

  1. maorb
    Member
    Posted 5 years ago #

    Hi all,

    I use this loop for displaying the last posted item as the "featured" item (without using a special category for featured), and then all other items in regular order.
    This is the code, which is working very well:

    <?php if (have_posts() ) {  // if is 1st item on home page
                         $count = 0;
                             while (have_posts()) : the_post();
    
                             if (is_front_page() && !is_paged() && ($count == 0)  ) { ?>  
    
                              <div class="mainitem">
    				some code for the main item
    		          </div>
    			    <?php $count++ ?>
    
    			 <?php   } else {  // all other items  ?>   
    
                            <div class="regular-item">
                    		some code for showing other items
    			</div>
    		         <?php }  endwhile; ?>
            <?php } ?>
    		<!-- End items -->

    Now, I want to have some categories excluded from the loop, and to show them in the sidebar. Suppose I want to exclude category id #19, I tried to add
    if (in_category(19)) continue;
    but then I've got weired things happening, such as displaying only 1 item per page, or haveing the 1st page empty and items only starting at page 2.

    I'm desparated here, trying for hours to find a good syntax and way to do that exclusion. What am I doing wrong in here?

    Or maybe the whole of my loop concept is wrong, and I should not use this way to display the featured/last post ?

  2. s_ha_dum
    Member
    Posted 5 years ago #

    Use query_posts() to exclude the posts with categories you don't want, then you no longer have to worry about dealing with them in your loop.

  3. maorb
    Member
    Posted 5 years ago #

    When I built this loop first, couple of months ago (then I didn't need to exclude categories) I tried to use query_posts(), but then I've had problems with the main loop not showing well the last item in the main css class and the others in the other css class.

    Since this loop creates the last item posted with count = 0 and show the special class only in the front_page().
    When then tried to use the query_posts() I had to use offset=n and then I had problems with the pagination..
    So I'm not sure that going back to query_posts() will be a good idea for me in this case.
    Isn't it?
    Maybe there is another idea?

    Thanks

  4. maorb
    Member
    Posted 5 years ago #

    Yes, as I remember, trying now again to use query_posts("cat=-19"); just before my loop - I've got on the 1st and 2nd pages the same posts, and also the class for the main posts being displayed on the 2nd page, which is not whatt I want...

  5. maorb
    Member
    Posted 5 years ago #

    Anyone has an idea?

    Thnaks

  6. s_ha_dum
    Member
    Posted 5 years ago #

    There is an example in the codex that explains how to deal with pagination.

    <?php
    global $wp_query;
    query_posts(
            array_merge(
                    array('cat' => -19),
                    $wp_query->query
            )
    );
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic