WP:简化词汇表php脚本/代码

时间:2015-09-26 18:37:36

标签: php wordpress

我想简化我的代码以避免重复相同的代码格式。我从A-Z做了一个自定义帖子类型的词汇表,虽然我做了一个基本的自定义查询帖子,但它最终导致凌乱的代码输出。

<?php
  $args = array (
    'post_type'       => array( 'cre-library' ),
    'posts_per_page'  => -1,
    'orderby'         => 'title',
    'order'           => 'ASC',
    'meta_query'      => array( array( 'key' => 'post_category', 'value' => 'A', 'compare' => 'LIKE', 'type' => 'CHAR' ) )
  );
  $query = new WP_Query ( $args );
  if ($query->have_posts()) :
    echo '<div id="content-for-a" class="active">';
    echo '<h2>A</h2>';
    while ($query->have_posts()) : $query->the_post();
      the_title('<h3>','</h3>'); 
      the_content();
    endwhile;
    echo '</div>';
  endif;
  wp_reset_postdata();
?>

<?php
  $args = array (
    'post_type'       => array( 'cre-library' ),
    'posts_per_page'  => -1,
    'orderby'         => 'title',
    'order'           => 'ASC',
    'meta_query'      => array( array( 'key' => 'post_category', 'value' => 'B', 'compare' => 'LIKE', 'type' => 'CHAR' ) )
  );
  $query = new WP_Query ( $args );
  if ($query->have_posts()) :
    echo '<div id="content-for-b">';
    echo '<h2>B</h2>';
    while ($query->have_posts()) : $query->the_post();
      the_title('<h3>','</h3>'); 
      the_content();
    endwhile;
    echo '</div>';
  endif;
  wp_reset_postdata();
?>

<?php //...and so on ?>

此代码已经有效。如果我们可以将它简化为PHP的一个循环块,并使每个条目匹配post_category值并同时显示所有帖子。

0 个答案:

没有答案