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.

Bare with me. Firstly, here is my whole script for my featured slider articles:

<div id="featured" class="<?php if ( $responsive ) echo 'flexslider' . $featured_auto_class; else echo 'et_cycle'; ?>">
<a id="left-arrow" href="#"><?php esc_html_e('Previous','Aggregate'); ?></a>
<a id="right-arrow" href="#"><?php esc_html_e('Next','Aggregate'); ?></a>

<?php if ( $responsive ) { ?>
    <ul class="slides">
<?php } else { ?>
    <div id="slides">
<?php } ?>
<?php global $ids;
$ids = array();
$arr = array();
$i=0;

$featured_cat = get_option('aggregate_feat_cat');
$featured_num = (int) get_option('aggregate_featured_num');

if (get_option('aggregate_use_pages') == 'false') query_posts("showposts=$featured_num&top=billboard"); // set which slug in custom taxonomy TOP it should filter
else {
    global $pages_number;

    if (get_option('aggregate_feat_pages') <> '') $featured_num = count(get_option('aggregate_feat_pages'));
    else $featured_num = $pages_number;

    query_posts(array
    ('post_type' => 'page',
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'post__in' => (array) get_option('aggregate_feat_pages'),
        'showposts' => (int) $featured_num
    ));
} ?>
<?php if (have_posts()) : while (have_posts()) :
    global $post;
    if (!$first_time) // START custom first post
    {
        $post_id = 105; // This is the ID of the first post to be displayed on slider
        $post = get_post($post_id);
        $first_time = 1;
    }
    else the_post(); // END custom first post
    ?>
    <?php if ( $responsive ) { ?>
            <li class="slide">
        <?php } else { ?>
            <div class="slide">
        <?php } ?>
    <?php
    $width = $responsive ? 960 : 958;
    $height = 340;
    $small_width = 95;
    $small_height = 54;
    $titletext = get_the_title();

    $thumbnail = get_thumbnail($width,$height,'',$titletext,$titletext,false,'Featured');

    $arr[$i]['thumbnail'] = get_thumbnail($small_width,$small_height,'',$titletext,$titletext,false,'Small');
    $arr[$i]['titletext'] = $titletext;

    $thumb = $thumbnail["thumb"];
    print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, ''); ?>
    <div class="featured-top-shadow"></div>
    <div class="featured-bottom-shadow feat<?php $category = get_the_category(); echo $category[0]->category_nicename; ?>"></div>
    <div class="featured-description">
        <div class="feat_desc">
            <h2 class="featured-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <p><?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,20); ?></p>
        </div>
    </div> <!-- end .description -->
    <?php if ( $responsive ) { ?>
            </li> <!-- end .slide -->
    <?php } else { ?>
            </div> <!-- end .slide -->
    <?php } ?>
    <?php $ids[] = $post->ID; $i++; endwhile; endif; wp_reset_query(); ?>
<?php if ( $responsive ) { ?>
    </ul> <!-- end .slides -->
<?php } else { ?>
</div> <!-- end #slides -->
<?php } ?>
</div> <!-- end #featured -->

It query_posts a set amount of posts from the custom taxonomy top=billboard. The following part of the code:

<?php if (have_posts()) : while (have_posts()) :
    global $post;
    if (!$first_time) // START custom first post
    {
        $post_id = 105; // This is the ID of the first post to be displayed on slider
        $post = get_post($post_id);
        $first_time = 1;
    }
    else the_post(); // END custom first post
    ?>

Adds the post ID 105 to the first post of the returned list. This allows me to control which post is displayed first. The problem is: If post ID 105 is already included in the custom taxonomy top=billboard, then it will appear twice in the list... so once because it was added in the above custom first post script, and once because it was added in the custom taxonomy top=billboard.

How do I adjust my code so that if post ID 105 exists in custom taxonomy top=billboard, then it should not appear twice (only as the first post).

share|improve this question
    
You could do with some re-formatting, the code is hard to read, so many unnecessary opening and closing tags e.g. <?php hello(); ?> <?php world(); ?> should be written as <?php hello(); world(); ?> –  Tom J Nowell Dec 21 '12 at 14:42
2  
Also I cannot strongly recommend enough that you abandon all use of query_posts, you will save yourself hundreds of hours of pain and misery by exorcising that function from your code. Learn to hate it with the fire of a hundred suns. Instead use WP_Query and get_posts. See here: slideshare.net/andrewnacin/… –  Tom J Nowell Dec 21 '12 at 14:44
    
Thank you, I am a learning programmer and yes, it has given me some pain indeed. Although with respect, this does not answer my question. –  Christine Cooper Dec 21 '12 at 14:55
    
@ChristineCooper, it doesn't answer the Q, but gives a good hint, what if you modify the code, same results? –  brasofilo Dec 21 '12 at 14:59
    
Modify my code with what? I am not entirely sure what your question is. –  Christine Cooper Dec 21 '12 at 15:01
show 1 more comment

1 Answer

Okay so I tried to read your code, and failed, and reformatted it as it's currently unreadable, and any errors are obscured by this unreadability. I cannot stress the importance of indenting correctly and following a standardised formatting for making your life infinitely easier:

<?php
$featured_class = 'et_cycle';
$tag = 'ul';
$attr = 'class';
if($responsive){
    $featured_class = 'flexslider' . $featured_auto_class;
    $tag = 'div';
    $attr = 'id';
}

?>
<div id="featured" class="<?php echo $featured_class; ?>">
    <a id="left-arrow" href="#"><?php esc_html_e( 'Previous', 'Aggregate' ); ?></a>
    <a id="right-arrow" href="#"><?php esc_html_e( 'Next', 'Aggregate' ); ?></a>
    <?php


    echo '<'.$tag.' '.$attr.'="slides">';

    global $ids;
    $ids = array();
    $arr = array();
    $i=0;

    $featured_cat = get_option( 'aggregate_feat_cat' );
    $featured_num = (int) get_option( 'aggregate_featured_num' );

    $query_args = array();
    if ( get_option( 'aggregate_use_pages' ) == 'false' ){
        // set which slug in custom taxonomy TOP it should filter
        $query_args = array(
            'showposts' => $featured_num,
            'top' => 'billboard'
        );
    }else {
        global $pages_number;

        if ( get_option( 'aggregate_feat_pages' ) <> '' ){
            $featured_num = count( get_option( 'aggregate_feat_pages' ) );
        } else{
            $featured_num = $pages_number;
        }
        query_args = array(
                'post_type' => 'page',
                'orderby' => 'menu_order',
                'order' => 'ASC',
                'post__in' => (array) get_option( 'aggregate_feat_pages' ),
                'showposts' => (int) $featured_num
        );
    }

    // needs to die, use WP_Query instead
    query_posts($query_args);


    if ( have_posts() ) {
        while ( have_posts() ) {
            global $post;
            // START custom first post
            if ( !$first_time ) {
                $post_id = 105; // This is the ID of the first post to be displayed on slider
                $post = get_post( $post_id );
                $first_time = 1;
            }else{
                the_post(); // END custom first post
            }
            if ( $responsive ) {
                echo '<li';
            }else {
                echo '<div';
            }
            echo ' class="slide">';
            $width = $responsive ? 960 : 958;
            $height = 340;
            $small_width = 95;
            $small_height = 54;
            $titletext = get_the_title();

            $thumbnail = get_thumbnail( $width, $height, '', $titletext, $titletext, false, 'Featured' );

            $arr[$i]['thumbnail'] = get_thumbnail( $small_width, $small_height, '', $titletext, $titletext, false, 'Small' );
            $arr[$i]['titletext'] = $titletext;

            $thumb = $thumbnail["thumb"];
            print_thumbnail( $thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, '' );
            ?>
            <div class="featured-top-shadow"></div>
            <div class="featured-bottom-shadow feat<?php $category = get_the_category(); echo $category[0]->category_nicename; ?>"></div>
            <div class="featured-description">
                <div class="feat_desc">
                    <h2 class="featured-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                    <p><?php $excerpt = get_the_excerpt(); echo string_limit_words( $excerpt, 20 ); ?></p>
                </div>
            </div> <!-- end .description -->
                <?php if ( $responsive ) { ?>
                        </li> <!-- end .slide -->
                <?php } else { ?>
                        </div> <!-- end .slide -->
                <?php } ?>
                <?php $ids[] = $post->ID; $i++;
        }
    }

    wp_reset_query();
    echo '</'.$tag.'>';
    ?>
</div> <!-- end #featured -->

You'll notice i made some simplifications and swapped out the if statements so they all use {} rather than a muddle of shorthand and standard.

There are a number of issues here, the one at the end being wp_reset_query, and the biggest alarm bell being the use of query_posts.

I strongly recommend you never use query_posts and instead use either WP_Query or get_posts.

The culprit here is your loop:

        while ( have_posts() ) {
            global $post;
            // START custom first post
            if ( !$first_time ) {
                $post_id = 105; // This is the ID of the first post to be displayed on slider
                $post = get_post( $post_id );
                $first_time = 1;
            }else{
                the_post(); // END custom first post
            }

Here we see the beginning of your post loop, it checks if there are additional posts, then calls the_post to advance forwards.

When the first post happens however, you don't call the_post, so the loop fails to advance. This is not how a loop should work, you should always call the_post regardless.

Instead, I'd suggest you specify in your query not to include the post 105. ( I would also avoid hardcoding the post ID, it may not always be a 105 ).

So, to start with, use the post__not_in parameter on your query, e.g.:

$query_args['post__not_in'] = array(105);
share|improve this answer
    
I tested your cleaned up code and it returned a Parse error: syntax error, unexpected '=' error. Nonetheless, can you please elaborate on where I add the $query_args['post__not_in'] = array(105);? Thank you for the reply. –  Christine Cooper Dec 21 '12 at 20:16
    
If you look at my modified version of your code, instead of 2 query_posts calls, I instead create an array and pass it into query_posts, filling it with different things depending on the if statement. You will want to insert before the query posts call. Think of query_args as a shopping list, then imagine what would happen if you asked for stuff after WordPress had gone out to get it ( e.g. does it make sense to say "hey you've just come back from the shop, I'd like you to get some milk while you're there" ) –  Tom J Nowell Dec 21 '12 at 20:59
    
that parse error, I cant see where it would be coming from, can you provide more information? like what's the line of code is the error on? –  Tom J Nowell Dec 21 '12 at 21:03
    
Thank you for the elaboration. The error is related to line 43. I should have stated that in my previous comment, apologies. –  Christine Cooper Dec 21 '12 at 21:22
    
which line is that? I don't know where you've copy pasted my code to, if you can find line 43 and let me know what it says? –  Tom J Nowell Dec 23 '12 at 0:25
add comment

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.