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 trying to retrieve two types of posts composing a total of 5 posts on the first page of a site the first post is styled differently to the following 4 posts (to maintain a sense of clarity let's call the one post A and the group of 4 posts B) . So to achieve this I am using an if(sticky)/else statement to retrieve A then a nested loop querying the custom category in the else statement to return only B posts and not all posts, I tried querying the category before the loop using get_posts but if i do so it just returns the sticky inside of the B's block and not in the stickies desired position, moving on to the nested loop option. I am using the code below but it just seems to return 20 repeated posts of the category that B's are assigned to instead of just B once here is the code I currently have which seems to be the closest to what i need..:

        <div class="post-box">

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

            <?php while ( have_posts() ) : the_post(); ?>

<!-- START: content.php -->
                    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                                    <?php if ( is_sticky() && is_home() && ! is_paged() ) : ?>
                                        <div class="row" id="sticky-post-main">
                                                    <div class="six columns">
                                                                        <?php the_post_thumbnail(); ?>
                                                    </div>
                                                    <div class="six columns">
                                                                                <header class="entry-header">
                                                                                    <h1 class="entry-title">
                                                                                        <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'requiredfoundation' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
                                                                                    </h1>
                                                                                            <?php if ( 'post' == get_post_type() ) : ?>
                                                                                            <?php get_template_part('entry-meta', get_post_format() ); ?>
                                                                                            <?php endif; ?>
                                                                                </header><!-- .entry-header -->
                                                                                <div class="entry-summary">
                                                                                            <?php the_excerpt(); ?>
                                                                                </div><!-- .entry-content -->
                                                    </div>
                                        </div>

                                    <?php else: ?>

                                        <?php
                                           $my_query = new WP_Query( "cat=11&numberposts=4" );
                                           if ( $my_query->have_posts() ) {
                                               while ( $my_query->have_posts() ) {
                                                   $my_query->the_post(); ?>


                                                        <div class="three columns below-sticky">

                                                                            <?php the_post_thumbnail(); ?>
                                                                                <header class="entry-header">
                                                                                    <h1 class="entry-title">
                                                                                        <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'requiredfoundation' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
                                                                                    </h1>
                                                                                    <?php if ( 'post' == get_post_type() ) : ?>
                                                                                    <?php get_template_part('entry-meta', get_post_format() ); ?>
                                                                                    <?php endif; ?>
                                                                                </header><!-- .entry-header -->
                                                                                <div class="entry-summary">
                                                                                            <?php the_excerpt(); ?>
                                                                                </div><!-- .entry-content -->
                                                        </div>
                                        <?php        }
                                           }
                                           $GLOBALS['post'] =  $GLOBALS['wp_query']->post;
                                        ?> 
                            <?php endif; ?>

                    </article><!-- #post-<?php the_ID(); ?> -->
                    <!-- END: content.php -->
            <?php endwhile; ?>

        <?php endif; ?>
        </div>

I have also tried the following in my else statement:

    <?php else: ?>
<?php wp_reset_postdata(); get_posts(  array(
    'numberposts'       =>  5,
    'offset'            =>  0,
    'category'          =>  3,
    'orderby'           =>  'post_date',
    'order'             =>  'DESC',

    'post_type'         =>  'post',
     )
) ?>
<div class="three columns below-sticky">

                    <?php the_post_thumbnail(); ?>
        <header class="entry-header">
            <h1 class="entry-title">
                <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'requiredfoundation' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
            </h1>
            <?php if ( 'post' == get_post_type() ) : ?>
            <?php get_template_part('entry-meta', get_post_format() ); ?>
            <?php endif; ?>
        </header><!-- .entry-header -->
        <div class="entry-summary">
                    <?php the_excerpt(); ?>
        </div><!-- .entry-content -->
</div>

            <?php endif; ?>

and this:

    <?php else: ?>
        <?php global $post;
         $my_query = get_posts('numberposts=4&cat=11');
  // numberposts is required with get_posts so I just set it to a large number
    foreach($my_query as $post) :
        setup_postdata($post);
        $link = get_post_meta($post->ID, 'site-url', true); ?>
        <li><a href="<?php echo $link; ?>"><php the_title(); ?></a></li>
<?php endforeach; ?>

<div class="three columns below-sticky">

                    <?php the_post_thumbnail(); ?>
        <header class="entry-header">
            <h1 class="entry-title">
                <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'requiredfoundation' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
            </h1>
            <?php if ( 'post' == get_post_type() ) : ?>
            <?php get_template_part('entry-meta', get_post_format() ); ?>
            <?php endif; ?>
        </header><!-- .entry-header -->
        <div class="entry-summary">
                    <?php the_excerpt(); ?>
        </div><!-- .entry-content -->
</div>

            <?php endif; ?>

Now I am open to suggestions on how this should work or any other way this might work, maybe not using custom categories, a plugin or any bright ideas anybody comes up with?? thank you for taking the time to read this. Kind regards Chris

share|improve this question
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.