###■目的と質問
WP-PostViewsというプラグインを使って、「現在表示中の記事のカテゴリ内の、人気記事」を表示したいです。(ただしカスタム投稿タイプになります。)
下記に現状のコードを記載しましたので、間違っている部分をご指摘いただければ幸いです。
よろしくお願いいたします。
###■現状のfuntions.phpのコード
- test1というカスタム投稿タイプがある
- test1に所属する、test1-categoryというカスタムタクソノミーがある
- 今表示している記事と同じタームがついた記事を、カスタムフィールドViewsの順番で5件表示したい
php
1// カスタム投稿タイプの追加 2add_action( 'init', 'custum_post_type' ); 3function custum_post_type() { 4 //test1の投稿タイプ追加 5 register_post_type( 'test1', 6 array('labels' => 7 array( 8 'name' => __( 'test1' ), 9 'singular_name' => __( 'test1' ) 10 ), 11 //投稿タイプの設定 12 'public' => true, 13 'menu_position' => 5, 14 'hierarchicla' => false, 15 'has_archive' => true, 16 //パーマリンクを /test1/投稿名/ にします。 17 'rewrite' => array( 18 'slug' => 'wp-test1' 19 ), 20 //投稿編集ページの設定 21 'supports' => array('title','editor','thumbnail', //タイトル,編集,アイキャッチ 22 'custom-fields','excerpt','author','trackbacks', //カスタムフィールド,抜粋文&作成者,トラックバック 23 'comments','revisions','page-attributes') //コメント,リビジョン,作成者,表示順のボックス 24 ) 25 ); 26 //test1のカテゴリー追加 27register_taxonomy( 28 'test1-category', /* タクソノミーのslug */ 29 'test1', /* 属する投稿タイプ */ 30 array( 31 'hierarchical' => true, 32 'update_count_callback' => '_update_post_term_count', 33 'label' => 'カテゴリー', 34 'singular_label' => 'カテゴリー', 35 'public' => true, 36 'show_ui' => true 37 ) 38); 39}
###■現状のsingle.phpのコード
php
1<?php 2$terms = the_terms(get_the_ID(),'test1-category'); 3$term_ids = array(); 4foreach($terms as $term){ 5 $term_ids[] = $term->term_id; 6} 7$args = array( 8 'post_type' => 'test1', 9 'posts_per_page' => 5, 10 'meta_key' => 'views', 11 'orderby' => 'meta_value_num', 12 'tax_query' => array( 13 array( 14 'taxonomy' => 'test1-category', 15 'field' => 'term_id', 16 'terms' => $term_ids, 17 'operator' => 'IN' 18 ), 19 ), 20); 21$posts = get_posts($args); ?> 22 23<div class="wp-postviews"> 24 <h3>関連記事</h3> 25 <ol> 26 <?php foreach($posts as $post): setup_postdata($post); ?> 27 <li>...表示の内容...</li> 28 <?php wp_reset_postdata(); endforeach; ?> 29 </ol> 30</div>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/10/24 05:04