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 think that I have a problem with the loop on my taxonomy page. On any part of the code (specially on the sidebar) that I need the permalink, it always return the link of the first item of the taxonomy loop, but the correct, is of the current page.

This is my taxonomy page code:

<?php get_header(); ?>
<div id="header_titulo">
    <div class="container">
        <?php $term_global = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy')); ?>
        <h1 class="esquerda"><a title="Voltar" href="<?php echo get_bloginfo('url') . '/colecoes'; ?>">< Voltar</a></h1>
        <h1 class="direita_voltar"><?php echo $term_global->name; ?></h1>
    </div>
    <div class="hr"></div>
</div>
<div id="content-container" class="center">
    <div class="sidebar esquerda">
        <?php if (have_posts()){ ?>
            <?php while (have_posts()){ ?>
                <?php the_post(); ?>
            <?php } ?>
        <?php } ?>
        <?php rewind_posts(); ?>
        <?php wp_reset_query(); ?>
        <?php get_sidebar('colecoes'); ?>
        <?php wp_reset_query(); ?>
    </div>
    <div id="content" class="direita">
        <?php
            $args = array('post_type' => 'colecoes',  'paged' => $paged, 'posts_per_page' => 12, 'order' => 'DESC', 'taxonomy' => 'categorias', 'term' => $term_global->slug);
            $loop = new WP_Query($args);
            $contagem = 0;
            if (sizeof($loop->posts) >= 1){
            echo "<ul class='listagem'>";
                while ($loop->have_posts()){ 

                    $loop->the_post();
                    $args = array(
                        'post_type' => 'attachment',
                        'numberposts' => null,
                        'post_status' => null,
                        'post_parent' => $post->ID,
                        'numberposts' => 1
                    );
                    $attachments = get_posts($args);
                    if ($contagem == 2 || $contagem == 5 || $contagem == 8 || $contagem == 11 || $contagem == 14){
                        echo "<li class='sem_borda'>";
                    }else{
                        echo "<li>";
                    }
                    if ($attachments) {
                        echo '<a class="imagem" href="' . get_bloginfo('url') . '/colecao/' . $loop->post->post_name . '" title="' . get_the_title() . '">';
                            echo '<div class="fotos">';
                                the_thumb(get_the_title(),0,1,false);
                                the_thumb(get_the_title(),1,2,false);
                            echo '</div>';
                        echo '</a>';
                    }
                    echo '<div class="box">';
                        echo '<p><a href="' . get_bloginfo('url') . '/colecao/' . $loop->post->post_name . '" title="' . get_the_title() . '">'.get_the_title().'</a></p>';
                    echo '</div>';
                    echo "</li>";
                    if ($contagem == 2 || $contagem == 5 || $contagem == 8){
                        echo '<div class="hr colecao"></div>';
                    }

                    $contagem = $contagem + 1;
                }
                echo "</ul>";
            }else{
                echo category_description(); 
            }
        ?>
    </div>
    <div id="content" class="direita">
        <div class="paginacao">
            <?php wp_pagenavi(array('query' => $loop)); ?>
            <?php rewind_posts(); ?>
            <?php wp_reset_query(); ?>
        </div>
    </div>
</div>
<?php get_footer(); ?>
share|improve this question
add comment

2 Answers 2

I didn't test your code, but I would check first the follow functions,

<?php rewind_posts(); ?> <?php wp_reset_query(); ?>

before you call your sidebar. I don't think you need both of them.

share|improve this answer
    
Hello Daniel. I tried with only one and both. The rusult is the same =/ –  Marcomar Nov 3 '11 at 16:19
    
Did you try to comment all your <?php rewind_posts(); ?> <?php wp_reset_query(); ?> on the code? –  Daniel Nov 3 '11 at 17:15
    
rewind_posts() resets the pointer to the first post in the current query. wp_reset_query() resets the query to be the main query on the page. So if you wanted to do something with the posts in the main query after iterating over your $loop query, you might call wp_reset_query(). I'm not sure what you're trying to do with it. –  goldenapples Nov 3 '11 at 17:30
add comment

You're trying to build the permalinks yourself? That looks really tricky and fragile.

Also, you're getting a little confused trying to deal with the $loop WP_Query object. When you call $loop->the_post();, what you're doing is populating the global variable $post with the next post in the $loop query object. So everywhere that you're iterating over $loop->post, you could actually just write $post.

So, for example, replace this code:

<a href="' . get_bloginfo('url') . '/colecao/' . $loop->post->post_name . '"

with something that uses WordPress's permalink functions, like

<a href="' . get_permalink( $post->ID ) . '"
share|improve this answer
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.