-1

如何通过当前帖子的下一个帖子按类别显示相关帖子(不要最新帖子或随机帖子)。我正在为二十二个主题使用与代码相关的帖子。但是现在, goytwelve_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>';
       }
   }?>

4

1 回答 1

0

WordPress 习惯使用WP_Query类从数据库中获取帖子,如果您可以修改代码以使用WP_Query它会更容易。

WP_Query 参考

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

根据法典参考使用自定义选择查询显示帖子

你应该尝试这样的事情:

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
    }
}

其他有趣的帖子:

setup_postdata ($post) 有什么作用?

于 2015-05-15T06:04:49.073 回答