Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am soooo close to getting this to work but I have one outstanding issues.

  1. The featured posts code keeps repeating itself before/after each post.

Any help would be greatly appreciated. The code below is what is currently in my loop.

NEW CODE per CHIP

<?php
// Define featured posts query
$featured_query = new WP_Query( array(
'posts_per_page' => 3, 
'category_name' => 'featured'
) );
// Open featured posts loop
if ( $featured_query->have_posts() ) :

while ( $featured_query->have_posts() ) : $featured_query->the_post();

    // Featured posts loop markup goes here
    ?>
    <div class="featureportfolio<?php echo $i++;?>">

        <a href="<?php the_permalink() ?>" rel="bookmark" class="title">
<?php the_post_thumbnail('thumbnail'); ?></a>


    <div class="info">
       <a href="<?php the_permalink() ?>" rel="bookmark" class="title"><?php the_title(); ?></a>
    </div>        
    </div>
    <?php

// Close featured posts loop
endwhile;
endif;

// Clear/spacer DIV
?>
<div class="clear"></div>

This "works" minus the fact that ONLY featured posts show on the main page now.

share|improve this question
    
I have looked through my code and to my best guess, I can't find any missing syntax. –  Matthew Snider Apr 9 '12 at 16:54
    
Chips code contained wp_reset_postdata(); after the loop, whereas your modified version does not –  Tom J Nowell Apr 11 '12 at 14:48
add comment

3 Answers

up vote 0 down vote accepted

After discussing in chat and seeing the parent index.php file:

http://pastebin.com/27r0FJ3x

The main loop in loop.php is not actually the main loop at all:

<div id="main" class="<?php echo $containerWidth; ?>" role="main">

            <?php get_template_part('breadcrumbs'); ?>

            <?php if ( have_posts() ) { ?>

                    <?php while ( have_posts() ) { ?>
                            <?php the_post(); ?>
                            <?php get_template_part( 'loop', get_post_format() ); ?>
                    <?php } // end while ?>

                    <?php get_template_part('pagination'); ?>

            <?php } else { ?>

So I suggest moving the featured post code into a featured.php, and adjusting index.php as so:

<div id="main" class="<?php echo $containerWidth; ?>" role="main">

            <?php get_template_part('breadcrumbs'); ?>
            <?php get_template_part('featured'); ?>

            <?php if ( have_posts() ) { ?>

                    <?php while ( have_posts() ) { ?>
                            <?php the_post(); ?>
                            <?php get_template_part( 'loop', get_post_format() ); ?>
                    <?php } // end while ?>

                    <?php get_template_part('pagination'); ?>

            <?php } else { ?>

And then adjusting loop.php accordingly

share|improve this answer
    
This is it and TOTALLY my fault, I knew better. Index is where I needed it, not the loop which is post specific. –  Matthew Snider Apr 11 '12 at 16:01
add comment

Here you go: http://pastebin.com/dsZEbC6a

For #1, I moved the <?php /* Main Loop */ ?> <?php global $framework; ?> below the Featured Posts bit - I think that will keep the featured posts from running before each regular post in the loop.

Looks like #2 was an unclosed <a> tag from line 8 of your original code.

Hope this helps, cheers!

share|improve this answer
    
#2 is solved - THANK YOU MUCH –  Matthew Snider Apr 9 '12 at 15:02
add comment

Issue #1

Do not use query_posts() to create secondary loops; it is intended only for modifying the Primary Loop. To create a secondary loop, use WP_Query() or get_posts():

It is always best practice to keep your secondary/custom loops separate from the main query, unless your goal is to modify the primary loop.

(If you need further direction, let me know and I'll add code examples. I can't see your pastebin content at the moment.)

Edit

I see several issues:

  1. You've got your main loop inside your secondary loop:

       <?php while ($featured_query->have_posts()) : $featured_query->the_post(); ?>
       <?php while (have_posts()) : the_post(); ?>
    

    You need to close your featured posts loop before you open your main loop:

       <?php 
       // Open featured posts loop
       while ($featured_query->have_posts()) : $featured_query->the_post(); 
    
       // Close featured posts loop
       endwhile;
    
       // Open main loop
       while (have_posts()) : the_post(); 
       ?>
    
  2. You have an extra endif; after your loop(s):

    <?php endwhile;?>
    <?php endif; ?>
    <div class="clear"></div>
    

    You never call if ( have_posts() ) or if ( $featured_query->have_posts() ). I would wrap your while loops inside of if conditionals, but it's up to you. But if you don't, then get rid of the endif;

Corrected Code

I'm going to give you the bare-bones, and let you add in the meat. Your code should look like so:

<?php
// Define featured posts query
$featured_query = new WP_Query( array(
    'posts_per_page' => 3, 
    'category_name' => 'featured'
) );
// Open featured posts loop
if ( $featured_query->have_posts() ) :

    while ( $featured_query->have_posts() ) : $featured_query->the_post();

        // Featured posts loop markup goes here
        ?>
        <div class="featureportfolio<?php echo $i++;?>">

            <?php etc... ?>

        </div>
        <?php

// Close featured posts loop
    endwhile;
endif;

// Reset post() data
wp_reset_postdata();

// Clear/spacer DIV
?>
<div class="clear"></div>
<?php

// Open the main loop
if ( have_posts() ) : 

    while ( have_posts() ) : the_post();

        // Main loop markup goes here
        ?>
        <div id="post-<?php the_ID(); ?>" <?php post_class('post format-standard'); ?>>

            <?php etc... ?>

        </div>
        <?php

// Close main loop
    endwhile;
endif;
?>

Note that there's no need to call wp_reset_query(), since we're not touching the main query.

share|improve this answer
    
when i use get_posts or WP_Query, the sidebar is gone and it is a neverending loop. WP_Query wipes clean all content under this code. –  Matthew Snider Apr 9 '12 at 15:03
    
That sounds like a PHP syntax problem. Make sure all of your if statements and while loops are closed properly. –  Chip Bennett Apr 9 '12 at 15:38
    
I have looked but I can't find anything that a novice user like myself can find. –  Matthew Snider Apr 9 '12 at 15:41
    
Would mind some further direction or code examples if you have the time. NO idea what I am doing besides pasting together examples found online. –  Matthew Snider Apr 9 '12 at 17:32
    
"NO idea what I am doing besides pasting together examples found online." - This tactic is usually a recipe for disaster. :) Once I am able later today to look at your pastebin, I'll try to offer some code assistance. –  Chip Bennett Apr 9 '12 at 18:08
show 20 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.