首页 文章

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

提问于
浏览
0

我试图在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 回答

  • 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实际上是一个有效的参数,改变了我的答案以反映这一点 .

相关问题