WordPressテーマのfunctions.phpに下記のようなブログ投稿表示用の関数を作成しましたが、
HTML表示部分の所が、echoの繰り返しで見にくくなっています。
もう少し見通しのよいような書き方はできないでしょうか?
PHP
1 2function displayBlogList( $num_list ) { 3 $args = array( 4 'post_type' => 'post', 5 'orderby' => 'date', 6 'posts_per_page' => $num_list 7 ); 8 $the_query = new WP_Query( $args ); 9 10 if ( $the_query->have_posts() ) : 11 while ( $the_query->have_posts() ) : $the_query->the_post(); 12 $post_id = get_the_ID(); 13 $category = get_the_category($post_id); 14 $cat_slug = $category[0] -> slug; 15 $cat_name = $category[0] -> name; 16 $category_link = get_category_link( $category[0] -> term_id ); 17 echo '<div class="up-inner">'; 18 echo '<div class="date">'; 19 the_time('Y.m.d'); 20 echo '</div>'; 21 echo '<div class="cat"><a href="' . $category_link . '">' . $cat_name . '</a></div>'; 22 echo '<a class="title" href="'; 23 the_permalink(); 24 echo '">'; 25 the_title(); 26 if ($cat_slug == "abc" || $cat_slug == "efg") { 27 echo "の記事を公開しました。"; 28 } 29 echo '</a>'; 30 echo '</div>'; 31 endwhile; 32 wp_reset_postdata(); 33 else : 34 echo '<p>投稿がありません。</p>'; 35 endif; 36}
回答2件
あなたの回答
tips
プレビュー