来自以下帖子的同一类别的Wordpress帖子 [英] Wordpress posts from the same category below post

查看:50
本文介绍了来自以下帖子的同一类别的Wordpress帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个WordPress主题.现在,我陷入了困境.我想在单个帖子中显示循环中来自同一类别的帖子.应显示"Aktuelt for deg",用于显示同一类别的帖子.实时预览.这是我的代码:

I'm making a wordpress theme. Right now I'm stuck with something. I want to show the posts from the same category within the loop on a single post. "Aktuelt for deg" is where the posts from the same category should be shown. Live preview. This is my code:

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

推荐答案

请勿使用 query_posts .这不是您要执行的操作的正确工具.

Don't use query_posts. It's not the right tool for what you're trying to do.

改为使用 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
));

然后使用以下命令渲染它:

Then, to render it, use this:

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

这篇关于来自以下帖子的同一类别的Wordpress帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆