0

I modifying a wordpress template to be left with two columns on the main page with the following structure:

enter image description here

The first 4 are of a category and the last 4 of another

The index code is:

http://pastebin.com/dMC0saBN

The page is (Columns are made, but all they do is repeat the post. They haven't order or filter)

  • The code should be embedded in the Question too, but that's too much, IMO. Note that pure CSS or HTML questions are off-topic (faq). What's the exact problem, making two queries to get those posts? – brasofilo Mar 23 '13 at 15:17
  • I created the 2 columns on my page, but the problem is I can't separate the two columns for each different show only post of different categories. [The code of the index.php file is in the question] – benitogonzalezh Mar 23 '13 at 15:27
  • Sorry, the Question should contain the code, not an external site. And I suggest you shorten it to the essentials, it's too much code. – brasofilo Mar 23 '13 at 15:33
  • Does the $high_query() loop output the expected posts? – Chip Bennett Mar 23 '13 at 15:47
  • @ChipBennett if you check the link in the first comment you'll see that the same set of posts shows up twice, once per column. – montrealist Mar 23 '13 at 21:04
1

Before line # 43, call wp_reset_query like the following and it should fix your repeated post problem

<?php wp_reset_query(); ?>

Ref: http://codex.wordpress.org/Function_Reference/wp_reset_query

1

Try putting this before your second query:

wp_reset_postdata();

It will restore your $post global, which should help your second occurence of have_posts and the_post to work properly.

That's how I generally do multiple queries on one page. I'm manually saving and restoring the $post global, but either way should work fine.

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

<article class="single-article" id="article-<?= $post->ID ?>">
<!-- output the page content here -->
</article>
<? endif ?>

<?php endwhile; ?>

<!-- start first custom query output -->
<?php
global $post;
$tmp_post = $post;
$args = array(
    'meta_key'        => 'featured',
    'meta_value'      => '1',
    'post_status'     => 'publish',
    'post_type'       => 'post' );

$featured_posts = get_posts( $args );

if( !empty($featured_posts) ): ?>

<section id="featured-posts">
    <? foreach( $featured_posts as $post ) : setup_postdata($post); ?>
        <a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
        <!-- and so on -->
    <?php endforeach; ?>
</section>

<?php endif; $post = $tmp_post; /* restore global post variable */ ?>

<!-- start second custom query output -->
<?php
$tmp_post = $post;
$story_args = array(
    'meta_key'         => 'featured',
    'meta_value'       => '1',
    'post_status'      => 'publish',
    'post_type'        => 'story' );

$story_posts = get_posts( $story_args );

if( !empty($story_posts) ): ?>

<section id="stories">
    <? foreach( $story_posts as $post ) : setup_postdata($post); ?>
        <a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
        <!-- and so on -->
    <?php endforeach; ?>
</section>

<?php endif; $post = $tmp_post; /* restore global post variable */ ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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