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 really, really don't get it.

On my archive.php and category.php it doesn't find any content. I know the correct templates are loaded, but WP_Query just don't find any content.

This is what i've got for archive.php:

    <?php

    if($displayMobileTheme){
        get_template_part('page', 'mobile');
        exit;
    }
    get_header();

    echo '<div class="blog-page">';
    get_sidebar('blog');
?>
    <h1>Arkiv: <?php echo ucfirst(get_the_date('F Y')); ?></h1>
    <?php
    $blog = new WP_Query(array(
        'post_type'     => 'post',
        'year'          => get_the_date('Y'),
        'monthnum'      => get_the_date('n')

    ));
    while($blog->have_posts()): $blog->the_post();
        get_template_part('excerpt');
    endwhile;
    wp_reset_query();
    ?>

</div> <!-- blog page -->
<?php
    get_footer();
?>

get_the_date() does return the correct year and monthnum, so why don't i get any results?

Excerpt file, as requested:

    <?php if(!is_single(get_the_ID())) : ?>
    <article class="hentry excerpt clearfix" id="post-<?php the_ID(); ?>">
        <header class="left">
            <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <small class="entry-meta author">
                <?php $post_categories = wp_get_post_categories(get_the_ID()); ?>
                Skrivet av <?php the_author(); ?> den <?php the_time('j F Y'); ?>, 
                Kategorier: 
                <?php
                    $output = '';
                    foreach($post_categories as $c){
                        $cat = get_category($c);
                        $output .= '<a href="'.get_category_link($c).'">'.$cat->name.'</a>, ';
                    }
                    echo substr($output, 0, -2);
                ?>
            </small>
        </header>
        <div class="entry-content left">
            <?php if(has_post_thumbnail()): ?>
                <a class="left" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                    <div class="img-wrapper">
                        <?php the_post_thumbnail('blog_thumb', 'thumbnail'); ?>
                    </div>
                </a>
            <?php endif; ?>

            <?php the_excerpt();?>
            <a href="<?php the_permalink(); ?>">Läs mer</a>
            </p>
        </div>
    </article>
<?php endif; ?>
share|improve this question
    
get_the_date grabs that data from your main query (the $wp_query global) but you're looping through your secondary query in $blog. Try and print_r the $blog object and see if it contains any results. If it does, you should also post the content of your excerpt.php file. –  kovshenin Oct 16 '12 at 8:20
    
I did indeed get results! $blog['posts'] contains the results i want to display. See OP update for excerpt file. –  qwerty Oct 16 '12 at 8:33

1 Answer 1

up vote 2 down vote accepted

The is_single function (used in your excerpt.php file) returns true only when the main query contains one single post. Since you're calling get_template_part from an archives page, your query contains more than one post, so is_single returns false and your excerpt.php file bails. Remove the check for is_single and you should be good to go.

More about is_single: http://codex.wordpress.org/Function_Reference/is_single

More about conditional tags: http://codex.wordpress.org/Conditional_Tags

Hope this helps.

share|improve this answer
    
Yes, that was indeed the issue. It sort of works, but not quite as i want. When i click a blog post i want to display all blog posts below the one that is shown, but i don't want the current one that is shown to show up below itself. Get it? That's why i added the if(is_single()) code. Is there any workaround to do what i want? –  qwerty Oct 16 '12 at 8:46
2  
Maybe you want to add post__not_in parameter to the WP_Query. It excludes certain posts from the results. –  Mridul Aggarwal Oct 16 '12 at 9:24
    
Yup, that did it! –  qwerty Oct 16 '12 at 9:47

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.