Loop problem - Taxonomy

时间:2011-11-03 作者:Marcomar

我想我的分类页面上的循环有问题。在我需要permalink的代码的任何部分(特别是侧栏上),它总是返回分类循环第一项的链接,但正确的是当前页面的链接。

这是我的分类页面代码:

<?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(); ?>

2 个回复
SO网友:goldenapples

你想自己建造永久链接?这看起来非常棘手和脆弱。

此外,您在处理$loop WP\\U查询对象。当你打电话的时候$loop->the_post();, 您要做的是填充全局变量$post 下一篇文章$loop 查询对象。所以无论你在哪里迭代$loop->post, 你可以直接写$post.

因此,例如,替换此代码:

<a href="\' . get_bloginfo(\'url\') . \'/colecao/\' . $loop->post->post_name . \'"
使用WordPress的永久链接功能,比如

<a href="\' . get_permalink( $post->ID ) . \'"

SO网友:Daniel

我没有测试你的代码,但我会先检查以下函数,

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

在调用侧边栏之前。我认为你不需要两者兼得。

结束

相关推荐

Get taxonomy slug from url

抱歉,我是分类法新手&;努力快速学习。我已经设置了部门并为其分配了一个职位。如果我去邮局,我可以看到网址是www。mydomain。com/扇区/自动/如何从URL获取自动信息?谢谢