如何按类别显示与当前 post 的下一个 post 相关的 post(不要最新 post 或随机 post)

How to display related post by categories with next post of current post (don't latest post or random post)

如何按类别显示与当前 post 的下一个 post 相关的 post(不要最新 post 或随机 post)。我使用与 twentytwelve 主题相关的代码 post。但是现在,wentytwelve_entry_meta() 中的作者重复了。

请帮助我:

<div id="related_posts">
    <?php
        $categories = get_the_category($post->ID);
        if ($categories) {
            $category_ids = array();
            foreach((get_the_category()) as $category) {
                $id = $category->cat_ID;
            }
            global $wpdb;
            $query = "
                SELECT * FROM $wpdb->posts
                LEFT JOIN $wpdb->term_relationships ON
                ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
                LEFT JOIN $wpdb->term_taxonomy ON
                ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
                WHERE $wpdb->posts.post_status = 'publish'
                AND $wpdb->term_taxonomy.taxonomy = 'category'
                AND $wpdb->term_taxonomy.term_id = $category_ids[0]
                AND $wpdb->posts.id < $post->ID
                ORDER BY id DESC limit 3
           ";
           $my_query = $wpdb->get_results($query);
           if( $my_query) {
               echo '<h3>Related Posts</h3><ul>';
               foreach($my_query as $key => $post) {
               ?>
                   <li>
                        <div class="entry-header">
                            <div class="header-l">
                                 <h1 class="entry-title">
                                      <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
                                </h1>
                                <p class="datetime">
                                    <?php twentytwelve_entry_meta(); ?>
                                </p>
                            </div>
                            <?php the_post_thumbnail(); ?>
                      </div>

                      <div class="entry-summary">
                          <?php
                              $str_content = wp_trim_words($post->post_content);
                              $str_content = str_replace('[', '<', $str_content);
                              $str_content = str_replace(']', '>', $str_content);
                              echo $str_content;
                          ?>
                     </div>
                </li>
           <?php
           }
           echo '</ul>';
       }
   }?>

WordPress习惯上是使用WP_Queryclass从数据库中获取posts,如果你可以修改你的代码使用WP_Query会更容易。

WP_Query Reference

当您使用自定义查询从数据库加载 post 时,the_permalink()the_title_attribute()the_title() 等模板标签将无法正常工作,即主题功能 twentytwelve_entry_meta() 失败的原因。

根据法典参考 Displaying Posts Using a Custom Select Query

你应该尝试这样的事情:

global $wpdb;
$query = "
    SELECT * FROM $wpdb->posts
    LEFT JOIN $wpdb->term_relationships ON
    ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON
    ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->posts.post_status = 'publish'
    AND $wpdb->term_taxonomy.taxonomy = 'category'
    AND $wpdb->term_taxonomy.term_id = $category_ids[0]
    AND $wpdb->posts.id < $post->ID
    ORDER BY id DESC limit 3";

$my_query = $wpdb->get_results($query);

if($my_query) {
    global $post;
    echo '<h3>Related Posts</h3><ul>';
    foreach($my_query as $key => $post) {
        //use setup postdata, it works only for variable named $post.

        setup_postdata($post); 
        //you can safely use template tags now.

        ?>
        <li>
            <div class="entry-header">
                <div class="header-l">
                    <h1 class="entry-title">
                        <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
                    </h1>
                    <p class="datetime">
                        <?php twentytwelve_entry_meta(); ?>
                    </p>
                </div>
                <?php the_post_thumbnail(); ?>
            </div>


            <div class="entry-summary">
              <?php
                  $str_content = wp_trim_words($post->post_content);
                  $str_content = str_replace('[', '<', $str_content);
                  $str_content = str_replace(']', '>', $str_content);
                  echo $str_content;
              ?>
            </div>
        </li>
        <?php
    }
}

其他有趣的post:

What does setup_postdata ($post ) do?