2017-06-19 68 views
1

我創建了一個名爲news的自定義帖子類型並添加了某些新聞。現在我想將它顯示在存檔頁面中。我在函數文件中添加了'has_archive'=>'true'。 我的檔案頁面的代碼是:自定義帖子類型的存檔頁面

<?php 
     $args = array('post_type' => 'news', 
         'post_status' => 'publish'); 

     $news=wp_get_recent_posts($args); 
    ?> 
     <div class="container mc_tb_p"> 
       <h1>NEWS/RELEASES</h1> 
        <?php 
         foreach ($news as $row) 
          { 
          $id  = $row['ID']; 
          $ntitle = $row['post_title']; 
          $ncontent = $row['post_content']; 
          $ncontent = strip_tags($ncontent); 
           if (strlen($ncontent) > 100) 
            { 
            $stringCut = substr($ncontent, 0, 200).'... <a href="'.get_permalink($id).'">Read More</a>'; 
            } 
           else{ 
            $stringCut = $row['post_content']; 
            } 
          $ndate  = $row['post_date']; 
          $trim  = new DateTime($ndate); 
          $trimdate  = $trim->format('d-m-Y'); 
          // var_dump($trimdate); 
        ?> 
       <div class="news_releases"> 
        <a href="<?php echo get_permalink($id)?>"><h3><?php echo $ntitle?></h3></a> 
         <h5><i>Published On:&nbsp;<?php echo $trimdate?></i></h5> 
          <p><?php echo $stringCut;?></p> 
       </div> 
         <?php 
         } 
         ?> 

     </div> 

現在,當我給我的網址:https //站點名稱/新聞...它造就了第二個消息,並沒有別的單頁,我已經嘗試了一切,但沒有似乎work.Please幫助

回答

1

你有兩個選擇,
1.創建一個文件名作爲當前活動的主題文件夾「archive- {} post_type .PHP」,並在該文件中使用下面的代碼,

<?php if (have_posts()) : 
      while (have_posts()) : the_post(); ?>  
    <!-- do stuff ... --> 
    <?php endwhile; 
     endif; ?> 

REF:https://codex.wordpress.org/Post_Type_Templates

OR

2.創建自定義模板文件,並使用下面的代碼,

<?php 
    $loop = new WP_Query(array('post_type' => 'posttypename', 'paged' => $paged)); 
    if ($loop->have_posts()) : 
     while ($loop->have_posts()) : $loop->the_post(); ?> 
      <div class="pindex"> 
       <?php if (has_post_thumbnail()) { ?> 
        <div class="pimage"> 
         <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a> 
        </div> 
       <?php } ?> 
      </div> 
     <?php endwhile; 
     if ( $loop->max_num_pages > 1) : ?> 
      <div id="nav-below" class="navigation"> 
       <div class="nav-previous"><?php next_posts_link(__('<span class="meta-nav">&larr;</span> Previous', 'domain')); ?></div> 
       <div class="nav-next"><?php previous_posts_link(__('Next <span class="meta-nav">&rarr;</span>', 'domain')); ?></div> 
      </div> 
     <?php endif; 
    endif; 
    wp_reset_postdata(); 
?> 

* posttypename - YPU崗位類型名稱

1
add_action('init', 'create_post_type'); 
function create_post_type() { 
    register_post_type('deals', 
     array(
      'labels' => array(
       'name' => __('Deals'), 
       'singular_name' => __('Deal') 
      ), 
     'public' => true, 
     'has_archive' => true, 
     ) 
    ); 
} 

=>The best way to start would be to copy the code from your theme’s archive.php file and paste it in your archive-{posttype}.php file. Then start tweaking from there. You can style this archive file to your heart’s desire. A very basic template would look like this 
<?php 
get_header(); 
if(have_posts()) : while(have_posts()) : the_post(); 
    the_title(); 
    echo '<div class="entry-content">'; 
    the_content(); 
    echo '</div>'; 
endwhile; endif; 
get_footer(); 
?> 

參考: http://www.wpbeginner.com/wp-tutorials/how-to-create-a-custom-post-types-archive-page-in-wordpress/

+0

我試圖做到這一點,但它沒有按照它應該的方式工作!存在同樣的問題。我正在使用可選框架主題碩士..它是sumthng與它有關 – Jackson

+0

我甚至從列表中刪除了存檔頁面,仍然一邊去我的網址/新聞它直接提出了我的第二條新聞的單頁! – Jackson

+0

你可以檢查陣列和自定義帖子類型中的所有slu and和名字嗎? –

相關問題