###■目的
WP-PostViewsというプラグインを使って、「現在表示中の記事のカテゴリ内の、人気記事」を表示したいです。(ただしカスタム投稿タイプになります。)
###■現状
下記のように書いているのですが、h3タグの部分しか表示されません。
single-example.php
php
1<!-- 関連記事ランキング表示 --> 2<div class="wp-postviews"> 3 <h3>関連記事</h3> 4 <ol> 5 <?php 6 //現在のカテゴリIDを配列で取得 7$now_id = get_queried_object_id($post->ID,'fields=ids');?> 8 </ol> 9<?php 10global $post; 11$args = array( 12 'post_type' => $now_id, 13 'posts_per_page' => 5, 14 'terms_in' => $now_id, 15 'meta_key' => 'views', 16 'orderby' => 'meta_value_num' 17); 18$posts = get_posts( $args ); 19foreach( $posts as $post ) { 20 setup_postdata($post); 21 ?> 22 <li><?php the_post_thumbnail( array(100, 100), array('alt'=>get_the_title()) ); ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 23 <?php 24 } 25wp_reset_postdata(); 26?> 27</div>
###■質問
①カテゴリIDを取得する部分↓が間違っているのかなって思います。
get_queried_object_id($post->ID,'fields=ids');
現状はこう書いておりますけれど、違うようです。
どうすれば、カスタム投稿タイプで「いま表示中のページのカテゴリID」を取得できるのでしょうか?
②それとも別のところでしょうか?
以上です。①か②か、お分かりになる方、よろしくお願いいたします。m(__)m
###■ちなみに
これはあっていると思いますが念のため。
functions.phpではこのように書いてカスタム投稿タイプにカテゴリを設定しています。
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}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/10/14 04:00
退会済みユーザー
2016/10/14 14:27
退会済みユーザー
2016/10/14 14:35
退会済みユーザー
2016/10/14 14:41
2018/01/24 05:12