サポート » 使い方全般 » foreachで出力された記事をソート

  • 解決済 fumidalat

    (@fumidalat)


    こんにちは。
    category.phpで下記のように子カテゴリーに属する記事の一覧を出力しています。
    この記事をソートして並び替えたいのですが、うまくいきません。

    <?php
      $cates = get_the_category();
      $deepest = 0;
      foreach($cates as $cate) {
          // カテゴリの祖先オブジェクトのIDの配列を取得
          $ancestors_ids = get_ancestors( $cate->cat_ID, 'category' );
          // 祖先オブジェクトの個数を取得
          $ancestor_num = count($ancestors_ids);
          // 祖先オブジェクトの個数が一番多いカテゴリのIDを取得
          if( $ancestor_num > $deepest) {
              $deepest = $ancestor_num;
              $deepest_id = $cate->cat_ID;
          }
      }
      $args = array(
          'category__in' => $deepest_id,         // カテゴリーIDを配列で指定
      );
      $query = new WP_Query($args);
      if( $query -> have_posts() ): while ($query -> have_posts()) : $query -> the_post();
    ?>
     <section class="child-box w23  bigger">
    		<a href="<?php the_permalink(); ?>"><figure class="mb15">
    <?php $value = get_post_meta($post->ID, 'list-img', true);?>
    <?php if(empty($value)):?>
    <img src="<?php echo get_template_directory_uri(); ?>/images/no-img.png
    " alt=""><?php else:?>
    <img src="<?php the_field('list-img'); ?>"  /><?php endif;?>
    			</figure>
    			<h2><?php the_title(); ?></h2>
    		<p class="right mt10">
    <?php $value6 = get_post_meta($post->ID, 'price', true); ?>
    <?php if(empty($value6)): ?>
    <?php else: ?>
    			<?php
    $price = number_format(get_field('price'));
    echo $price . '円';
    ?>
    <?php endif; ?>
    </p></a>
    </section>
    <?php endwhile; endif; wp_reset_postdata(); ?>

    通常のループであれば

    <div class="select flex-ed">
    		<p>並び順:</p>
    		<form name="irekae" method="get" id="sort_form5" class="sort_form5">
           <select name='narabi' onChange="document.forms['irekae'].submit()" >
               <option value="sort1"<?php if(!isset($_REQUEST['narabi']) || $_REQUEST['narabi'] == 'sort1') echo ' selected="selected"';?>>新着順</option>
                <option value="sort2"<?php if(isset($_REQUEST['narabi']) && $_REQUEST['narabi'] == 'sort6') echo ' selected="selected"';?>>価格順</option>
           </select>
    </form>
    <?php $sortset = $_GET['narabi']; ?>
    <?php $sortall = array(
      "sort1" => 'order=DESC&posts_per_page=6&paged=',
      "sort2" => 'order=ASC&orderby=meta_value_num&meta_key=price&posts_per_page=6&paged='
    ); ?>
    
    <?php if($sortset != '') {
           $sort = $sortall[$sortset];
        } else {
           $sort = $sortall["sort1"];} ?>
    <?php query_posts($sort . $paged); ?>
    <?php
    while ( have_posts() ) :
    the_post();
    ?>

    以下略

    でソートできるのですが、foreachでのループの場合はどのようにすればソートが可能でしょうか

    よろしくお願いします。

    ヘルプの必要なページ: [リンクを見るにはログイン]

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック投稿者 fumidalat

    (@fumidalat)

    すみません。
    解決はしていないのですが、問題が別のところにあることがわかりました。

    したがって本件は一度家帰結済みとします。

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「foreachで出力された記事をソート」には新たに返信することはできません。