-1

現在の投稿の次の投稿で関連する投稿をカテゴリ別に表示する方法 (最新の投稿やランダムな投稿ではありません)。2012 テーマのコード関連の投稿を使用しています。しかし今、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クラスを使用して DB から投稿を取得します。コードを変更して使用できる場合WP_Queryは、より簡単になります。

WP_Query リファレンス

the_permalink()カスタム クエリを使用して DB から投稿をロードしているため、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 に答える