いつもお世話になります。
WordPressにて会社でサイト作成を任されたのですが、当方HTML・CSSを主としており、PHP部分は色々と調べながら制作している現状です。
なぜ書き換えたいのか
現状のコードでは正常に記事の一覧が表示されておりますが、「特定の記事を先頭に固定表示する」処理をしたく、それでは「get_posts」では反映されないようですので、「WP_Query」に書き換えたいと思っています。
現状のコード
カスタム投稿タイプ: brand
カスタムタクソノミー: brandcat
PHP
1 <?php $my_posts = get_posts( array( 'post_type' => 'brand', 'numberposts' => '-1', 'tax_query' => array( array( 'taxonomy' => 'brandcat', 'field' => 'term_id', 'terms' => $term->term_id ) ) ) ); ?> 2 <?php foreach ( $my_posts as $my_post ) : ?> 3 4 5 <!--表示させたい表記--> 6 <a href="<?php the_permalink( $my_post ); ?>"> 7 <div class="itiran-fchq-official"> 8 <?php 9 $image = get_field('brand-official'); 10 $size = 'full'; // (thumbnail, medium, large, full or custom size) 11 if( $image ) { 12 echo wp_get_attachment_image( $image, $size ); 13 } 14 ?> 15 </div> 16 <div class="itiran-brand-logo"> 17 <?php 18 $image = get_field('brand-logo',$my_post); 19 $size = 'サムネ_70x100'; // (thumbnail, medium, large, full or custom size) 20 if( $image ) { 21 echo wp_get_attachment_image( $image, $size ); 22 } 23 ?> 24 </div> 25 <h2><?php echo get_the_title( $my_post ); ?></h2> 26 </a> 27 <!--表示させたい表記ここまで--> 28 29 30 <?php endforeach; ?> 31 <?php endforeach; ?>
試したコード
PHPの基礎知識もないまま調べて試行錯誤していますのでうまく出来ないのも当然ですが、念のため、以下のように試していました。
PHP
1 <?php 2 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 3 4 $my_posts = array( 5 'paged' => $paged, 6 'posts_per_page' => 150, 7 'taxonomy' => 'brandcat', 8 'field' => 'term_id', 9 'term_id', 'terms' => $term->term_id, 10 ); 11 12 $wp_query = new WP_Query($my_posts); 13 ));?> 14 15 <?php foreach ( $my_posts as $my_post ) : ?> 16 17 18 <!--表示させたい表記--> 19 <a href="<?php the_permalink( $my_post ); ?>"> 20 <div class="itiran-fchq-official"> 21 <?php 22 $image = get_field('brand-official'); 23 $size = 'full'; // (thumbnail, medium, large, full or custom size) 24 if( $image ) { 25 echo wp_get_attachment_image( $image, $size ); 26 } 27 ?> 28 </div> 29 <div class="itiran-brand-logo"> 30 <?php 31 $image = get_field('brand-logo',$my_post); 32 $size = 'サムネ_70x100'; // (thumbnail, medium, large, full or custom size) 33 if( $image ) { 34 echo wp_get_attachment_image( $image, $size ); 35 } 36 ?> 37 </div> 38 <h2><?php echo get_the_title( $my_post ); ?></h2> 39 </a> 40 <!--表示させたい表記ここまで--> 41 42 43 <?php endforeach; ?> 44 <?php endforeach; ?>
私の勉強不足ではございますが、いくら試してもうまくいかず、時間もありませんのでこちらでお力添えいただけないかと思い質問させていただきました。
どうかご教示のほどよろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/27 13:14
2019/08/28 05:32