0

我一直在一起拼湊各種示例的各個部分,但似乎無法將我的頭圍繞在此。定期發佈第一個(降序),然後在WP搜索中定製發佈類型(按字母順序排列)

我定期發佈博客帖子(新聞),我希望顯示最新的最新消息,然後是我希望在新聞下面分組的自定義帖子類型(商家)。我正在使用Sage,這是我的第一個主題。

這是迄今爲止我初學代碼:

<?php get_template_part('templates/page', 'header'); ?> 

    <?php if (!have_posts()) : ?> 
     <div class="alert alert-warning"> 
      <?php _e('Sorry, no results were found.', 'sage'); ?> 
     </div> 
     <?php get_search_form(); ?> 
      <?php endif; ?> 

       <?php if (have_posts()) : ?> 
        <?php while (have_posts()) : the_post(); ?> 
         <?php $post_type = get_post_type_object(get_post_type()); ?> 
          <?php $type = get_post_type(); ?> 

           <?php if ($type == 'post') { ?> 
            <h2>News Results</h2> 
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
             <div> 
              <a href="<?php the_permalink(); ?>"> 
               <?php $cats=get_the_category(); ?> 
                <?php echo $cats[0]->cat_name; ?> 

              </a> 
             </div> 
             <div> 
              <h3><?php the_title(); ?></h3> 

              <div class="result-excerpt"> 
               <?php if (has_excerpt($post->ID)) { 
       echo the_excerpt(); 
      } else { 
       echo get_excerpt(); 
      } ?> 
              </div> 

             </div> 
            </article> 

            <?php } elseif ($type == 'business') { ?> 

             <h2>Business Results</h2> 
             <article <?php post_class(); ?>> 
              <header> 
               <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
               <?php if (get_post_type() === 'post') { get_template_part('templates/entry-meta'); } ?> 
              </header> 
              <div class="entry-summary"> 
               <?php the_excerpt(); ?> 
              </div> 
              <a href="<?php the_permalink(); ?>"> 
               <?php $cats=get_the_category(); ?> 
                <?php echo $cats[0]->cat_name; ?> - 
                 <?php the_title(); ?> 
              </a> 
              <h3><?php echo the_sub_field('title'); ?></h3> 
              <?php if(get_sub_field('content')): ?> 
               <div class="result-excerpt"> 
                <?php echo custom_field_excerpt(); ?> 
               </div> 
               <?php endif; ?> 
             </article> 

             <?php } ?> 

              <?php endwhile; ?> 
               <?php endif; ?> 
+0

因此,您是否爲商業文章和未使用的類別創建了特定的自定義帖子類型? –

+0

要求澄清,以便我可以爲您量身定製建議的修補程序。 –

+0

CPT業務包含類別和標籤。 – James

回答

0

我認爲問題是,你遍歷每一個崗位,爲了和決定如何處理它。這將導致輸出,不管它是什麼,按照與輸入相同的順序。

我不會講很流利的PHP,但我覺得發生了什麼是算法讀起來就像這樣:

-for each post: 
    - if it's a 'post', display it like «this» 
    - otherwise, if it's a 'business', display it instead like «this» 

我想你想有兩個循環,就像這樣:

-for each post: 
    - if it's a 'post', display it like «this» 
-for each post: 
    - if it's a 'business', display it like «this» 

不幸的是,我沒有看到一個明顯的引用來存儲數組並循環。我真的不知道聖人是如何工作的,所以我必須將實施留給你。我最好的猜測 - 我真誠懷疑這將工作 - 如下:

<?php get_template_part('templates/page', 'header'); ?> 

<?php if (!have_posts()) : ?> 
    <div class="alert alert-warning"> 
     <?php _e('Sorry, no results were found.', 'sage'); ?> 
    </div> 
    <?php get_search_form(); ?> 
     <?php endif; ?> 

      <?php if (have_posts()) : ?> 
       <?php while (have_posts()) : the_post(); ?> 
        <?php $post_type = get_post_type_object(get_post_type()); ?> 
         <?php $type = get_post_type(); ?> 

          <?php if ($type == 'post') { ?> 
           <h2>News Results</h2> 
           <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
            <div> 
             <a href="<?php the_permalink(); ?>"> 
              <?php $cats=get_the_category(); ?> 
               <?php echo $cats[0]->cat_name; ?> 

             </a> 
            </div> 
            <div> 
             <h3><?php the_title(); ?></h3> 

             <div class="result-excerpt"> 
              <?php if (has_excerpt($post->ID)) { 
      echo the_excerpt(); 
     } else { 
      echo get_excerpt(); 
     } ?> 
             </div> 

            </div> 
           </article> 

             <?php endwhile; ?> 
              <?php endif; ?> 







     <?php if (!have_posts()) : ?> 
      <div class="alert alert-warning"> 
       <?php _e('Sorry, no results were found.', 'sage'); ?> 
      </div> 
      <?php get_search_form(); ?> 
       <?php endif; ?> 

        <?php if (have_posts()) : ?> 
         <?php while (have_posts()) : the_post(); ?> 
          <?php $post_type = get_post_type_object(get_post_type()); ?> 
           <?php $type = get_post_type(); ?> 

            <?php if ($type == 'business') { ?> 

              <h2>Business Results</h2> 
              <article <?php post_class(); ?>> 
               <header> 
                <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
                <?php if (get_post_type() === 'post') { get_template_part('templates/entry-meta'); } ?> 
               </header> 
               <div class="entry-summary"> 
                <?php the_excerpt(); ?> 
               </div> 
               <a href="<?php the_permalink(); ?>"> 
                <?php $cats=get_the_category(); ?> 
                 <?php echo $cats[0]->cat_name; ?> - 
                  <?php the_title(); ?> 
               </a> 
               <h3><?php echo the_sub_field('title'); ?></h3> 
               <?php if(get_sub_field('content')): ?> 
                <div class="result-excerpt"> 
                 <?php echo custom_field_excerpt(); ?> 
                </div> 
                <?php endif; ?> 
              </article> 

              <?php } ?> 

               <?php endwhile; ?> 
                <?php endif; ?> 

祝你好運!

相關問題