类别.php中的查询重复自身

时间:2011-10-22 作者:AKNL

我的问题需要一些帮助。

我正在建立一个分类页面,在上面的帖子需要显示自定义字段键“uitleg”的值“ja”在哪里。每个类别只有一个具有该值的帖子。我把这篇文章用作介绍文章。在这个帖子下面,我需要显示除介绍帖子以外的所有其他同类帖子。这部分很有魅力。

但后来发生了一些奇怪的事情。如果我在一个类别中得到4篇帖子(1篇介绍帖子和3篇其他帖子),下面的查询会在每个类别下重复4次。有没有办法阻止这一切?必须在我的一个foreach查询中。但我看不出哪里出了问题。任何人都能看到哪里出了问题?

提前感谢!

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

                <?php $drukproef = get_post_meta($post->ID, \'drukproef\', $single = true); ?>
                <?php $drukproefimage = get_post_meta($post->ID, \'drukproefimage\', $single = true); ?>
                <?php $actief = get_post_meta($post->ID, \'actief\', $single = true); ?>
                <?php $uitleg = get_post_meta($post->ID, \'uitleg\', $single = true); ?>
                <?php $artistnaam = get_post_meta($post->ID, \'artistnaam\', $single = true); ?>
                <?php $artisturl = get_post_meta($post->ID, \'artisturl\', $single = true); ?>


                <?php global $post;
                    $categories = get_the_category();
                    foreach ($categories as $category) :
                ?>

                 <?php
                 global $post;
                 $tmp_post = $post;
                 $myposts = get_posts(\'posts_per_page=1\' . \'&paged=\' . get_query_var(\'paged\') . \'&meta_key=uitleg&meta_value=ja&category=\'. $category->term_id);
                 foreach($myposts as $post) :
                   setup_postdata($post);
                 ?>
                 <?php $thumb = get_post_meta($post->ID, \'thumb\', $single = true); ?>



                <?php $drukproef = get_post_meta($post->ID, \'drukproef\', $single = true); ?>
                <?php $drukproefimage = get_post_meta($post->ID, \'drukproefimage\', $single = true); ?>
                <?php $actief = get_post_meta($post->ID, \'actief\', $single = true); ?>
                <?php $uitleg = get_post_meta($post->ID, \'uitleg\', $single = true); ?>

                <div class="titleactive">
                        <span id="drukproeftitel"><?php the_title(); ?>:</span>
                        <span id="drukproefactief"><?php echo $drukproef; ?></span> 
                        <img src="<?php bloginfo(\'template_directory\'); ?>/images/uitleg-actief-ja.png" width="20" height="20" />
                    </div>
                    <div class="contentactive">
                        <div id="contentholder">
                            <div class="uitleg">  
                                <?php the_content(\'\'); ?>
                            </div>
                            <div id="uitlegdivider"><img src="<?php bloginfo(\'template_directory\'); ?>/images/uitlegdivider.png" alt="" width="26" height="171" /></div>
                            <div class="voorbeeldimage">
                                <img src="<?php echo $drukproefimage; ?>" />
                            </div>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                    <?php endforeach; ?>
                    <?php $post = $tmp_post; ?>


                    <div id="uitlegbanners">
                        <div class="inzendingen">Overige inzendingen</div>
                    </div>
                    <div class="clearfix"></div>

                        <?php
                        $posts = get_posts(\'posts_per_page=1000&category=\'. $category->term_id . \'&paged=\' . get_query_var(\'paged\'));
                        foreach($posts as $post) :
                        $thumb = get_post_meta($post->ID, \'thumb\', $single = true);
                        ?>
                    <ul>

                        <?php if (!empty($thumb)) { ?>
                            <li>
                                <?php $thumb = get_post_meta($post->ID, \'thumb\', $single = true); ?>
                                <?php $artistnaam = get_post_meta($post->ID, \'artistnaam\', $single = true); ?>
                                <a href="<?php the_permalink(); ?>"><img src="<?php echo $thumb; ?>" class="img" alt="<?php the_title(); ?>" width="171" height="171" /></a>
                                <div class="corners"></div>                               
                                <a href="<?php the_permalink(); ?>"> 
                                <div class="info">
                                    <h3><?php echo $artistnaam; ?></h3>
                                    <div class="excerpt"><?php the_excerpt(); ?></div>
                                    <div class="currentrating"><?php echo the_ratings_results(get_the_id()); ?></div>
                                </div>
                                </a>
                            </li>
                        <?php } ?>
                    </ul>
                        <?php endforeach; ?>
                    <?php endforeach; ?>
                    <?php wp_reset_query(); ?>
            <?php endwhile; ?>        
            <?php else : ?>
                Sorry, no posts matched your criteria.
            <?php endif; ?>

3 个回复
SO网友:Sagive

您应该将条件排除在上述循环含义之外

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
因此,每次循环开始时,它都会启动检查,而不是执行检查,如果条件存在,则启动循环。

很难理解,因为我不理解div名称,但我认为应该只在循环中

                        <div id="contentholder">
                        <div class="uitleg">  
                            <?php the_content(\'\'); ?>
                        </div>
                        <div id="uitlegdivider"><img src="<?php bloginfo(\'template_directory\'); ?>/images/uitlegdivider.png" alt="" width="26" height="171" /></div>
                        <div class="voorbeeldimage">
                            <img src="<?php echo $drukproefimage; ?>" />
                        </div>
                    </div>
说明:the loop = if (have_posts()) : while (have_posts())......

任何东西都会反复出现,直到没有符合你之前设置的条件的帖子,也没有在里面。

SO网友:Brooke.

您应该能够在get_post()

$myposts = get_posts(\'posts_per_page=1\' . \'&paged=\' . get_query_var(\'paged\') .\'&meta_key=uitleg&meta_value=ja&category=\'. $category->term_id.\' offset =>1\');

SO网友:AKNL

用另一种方法修复了它

        <?php global $wp_query;
    $cat_ID = get_query_var(\'cat\'); ?>

    <?php query_posts(\'cat=\' . $cat_ID . \'&meta_key=uitleg&meta_value=ja&posts_per_page=1\' . \'&paged=\' . get_query_var(\'paged\')); ?>
    <?php if (have_posts()) : ?>  

结束

相关推荐

如何通过unctions.php设置固定链接结构

我正在建立一个Wordpress网络,希望所有新网站都具有相同的永久链接结构(即“/%年%/%月%/%postname%/”)。我想知道这是否可以通过函数中的钩子或黑客来实现。php,而无需依赖用户来选择该结构。