在WordPress中获取相关文章无法正常工作

时间:2018-02-21 15:10:38

标签: php wordpress

我正在尝试在WordPress中显示所有具有相同类别的帖子,但是它无法正确显示,而只是显示所有内容。

这是php代码:

<?php

                        $related = get_posts( array( 
                            'category_in' => wp_get_post_categories($post->ID), 
                            'numberposts' => 3, 
                            'post_not_in' => array($post->ID) ) );

                        if( $related ) foreach( $related as $post ) {
                        setup_postdata($post); ?>

                            <div class="post">
                                <a href="<?php the_permalink(); ?>">
                                    <?php if ( has_post_thumbnail() ) {
                                        the_post_thumbnail();
                                    } 
                                      ?>
                                    <h3><?php the_title();?></h3>
                                </a>
                            </div>  

                        <?php }
                        wp_reset_postdata(); 
                        ?> 

从这里开始:https://wordpress.stackexchange.com/questions/41272/how-to-show-related-posts-by-category

如果它有帮助,那么指向该网站的链接代码无效:http://u1f8aki.nixweb23.dandomain.dk/cat-4-post-test/

有问题的代码位于红色文本下方的页面下方。您可以在面包屑的顶部看到类别。

1 个答案:

答案 0 :(得分:1)

您的代码中存在相当多的错误,我确信其中至少有一个是导致错误结果的罪魁祸首。

我已经使用一些评论重构了您的代码,解释了为什么以及为什么已经更改:

<?php
// For readability, save our categories in a variable for later use.
// $post->ID has been replaced with get_the_ID(), $post might not be accessible depending if you're exposing $post as a global or not.
$categories = wp_get_post_categories(get_the_ID());

/*
Instead of using get_posts(), use the recommended Wordpress loop in the form of WP_Query().
We start by defining our arguments for the loop
*/

$args = array(
    'category_in' => $categories, // here we use variable for readability
    'posts_per_page' => 3, //numberposts and posts_per_page has the same function, but posts_per_page is the more common of the two (IMO)
    'post__not_in' => array(get_the_ID()) // you were missing a '_', ie post_not_in instead of post__not_in
);

// Start the loop
$query = new WP_Query($args);

if($query->have_posts()): while($query->have_posts()): $query->the_post();
// No need to setup or reset postdata when using this method, it does it for you!
    ?>
    <div class="post">
        <a href="<?php the_permalink(); ?>">
            <?php
                if( has_post_thumbnail() ) {
                    the_post_thumbnail();
                } 
            ?>
            <?php
                // the_title() actually takes opening tag and closing tags as arguments in its function. So add the <h3> code like this.
                the_title('<h3>', '</h3>');
            ?>
        </a>
    </div> 
    <?php endwhile; endif; ?>

长话短说,你的代码因为错误命名的参数而无法正常工作。如果您不想用我的示例替换代码,只需将参数从numberposts更改为posts_per_page,将post_not_in更改为post__not_in

如果仍然无效,请检查每个帖子的wp_get_post_categories(get_the_ID())返回的内容,并确保所有帖子都没有共享您错过的某个类别。

编辑:numberposts实际上是一个有效的参数,改变了我的答案以反映这一点。