【问题标题】:Wordpress posts from the same category below post来自以下同一类别的 Wordpress 帖子
【发布时间】:2012-09-02 15:50:04
【问题描述】:

我正在制作一个 wordpress 主题。现在我被一些东西困住了。我想在单个帖子的循环中显示来自同一类别的帖子。 “Aktuelt for deg”是应显示同一类别的帖子的位置。 Live preview.这是我的代码:

<?php get_header(); ?>
<div id="hold" class="clearfix">
    <div id="left">
        <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
        <div class="entry">
            <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
            <div class="author">Skrevet av <?php the_author(); ?></div>
            <?php the_content(); ?>
        </div>
        <div class="comment-template">
            <?php comments_template(); ?>
        </div>
    </div>
<div id="right">
            <div class="search">
                <form  action="<?php echo home_url( '/' ); ?>" method="get">
                                    <input type="text" value="Skriv her for å søke.." name="s" id="s" onblur="if (this.value == '') {this.value = 'Skriv her for å søke..';}"
                                    onfocus="if (this.value == 'Skriv her for å søke..') {this.value = '';}">
                                </form>
                                <script type="text/javascript">
                                    $(".search").keypress(function(ev){
                                        if (ev.keyCode == 13) {
                                            $("form")[0].submit();
                                        }
                                    });
                                </script>
            </div>
            <div class="box">
                <div class="heading">
                    <h1>Aktuelt for deg</h1>
                </div>​
                <ul>
                    <?php query_posts('posts_per_page=5' . '&orderby=rand'); 

                    while ( have_posts() ) : the_post();
                        echo '<li><div class="borderline"><a href="';
                        the_permalink();
                        echo '">';
                        the_title();
                        echo '</a></div><author>Skrevet av ';
                        the_author();
                        echo '</author></li>';
                    endwhile;

                    // Reset Query
                    wp_reset_query();

                    ?>
                </ul>
            </div>
        </div>
    </div>

    <?php endwhile; ?>
    <?php endif; ?>

<?php get_footer(); ?>

【问题讨论】:

    标签: php html css wordpress loops


    【解决方案1】:

    不要使用query_posts。它不是您想要做的事情的正确工具。

    改用WP_Query

    global $post;
    $current_category = get_the_category();
    
    $same_category = new WP_Query(array(
        'cat'            => $category[0]->cat_ID,
        'post__not_in'   => array($post->ID),
        'orderby'        => 'rand',
        'posts_per_page' => 5
    ));
    

    然后,要渲染它,使用这个:

    <?php while ( $same_category->have_posts() ) : $same_category->the_post(); ?>
        <li>
            <div class="borderline">
                <a href="<?php the_permalink(); ?>">
                    <?php the_title(); ?>
                </a>
            </div>
            <author>Skrevet av <?php the_author(); ?></author>
        </li>
    <?php endwhile; ?>
    

    【讨论】:

    • 这没有得到相同类别的帖子...?
    • @VemundEldegard - 你是说它不会让你获得同一类别的帖子吗?你得到什么
    • – 我收到所有其他帖子,而不是属于同一类别的帖子。
    • @VemundEldegard - 这很奇怪。我的意思是,真的很奇怪。原样的代码应该可以解决问题。如果没有,我真的不知道还能告诉你什么......对不起。
    【解决方案2】:

    对于后来偶然发现这个问题的其他人:

    Joseph Silber's 代码几乎是正确的。

    'cat'            => $category[0]->cat_ID,
    

    应该是

    'cat'            => $current_category[0]->cat_ID,
    

    因为 $category 没有在任何地方定义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多