2017-02-17 48 views
1

我剛剛創建了一個新的頁面模板,並且只想打印所有包含類別新聞的帖子。如何顯示類別爲news的帖子

使用此代碼基於wordpress codex工作時。

<?php 
    query_posts( 
     array ( 
      'post_type' => 'post', 
      'category_name' => 'news', 
      'category' => 1, 
      'posts_per_page' => 3) 
     );  
     // The Loop 

     while (have_posts()) : the_post(); 
      the_title(); 
      the_content();   
     endwhile; 
     // Reset Query 
     wp_reset_query(); 
?> 

如何將標題換成h1標籤並將內容換成div框?

我試過,但它給我語法錯誤:

<h1><?php the_title(); ?></h1> 

希望能對你有所幫助。

+0

使用任何一個category_name或category。 –

+0

@Kushal Shah我剛剛刪除了一個,但仍然沒有顯示 –

+0

你可以像這樣添加the_title('

','

'); –

回答

2
<?php global $post; 
    $args = array( 
     'posts_per_page' => 3, 
     'post_type' => 'post', 
     'category' => 1 
    ); 
    $myposts = get_posts($args); 
    foreach ($myposts as $post) : setup_postdata($post); 
the_title('<h1 class="entry-title">', '</h1>'); 
endforeach; 
     wp_reset_postdata(); 
    ?> 
+0

絕對。感謝支持 –

+0

非常感謝 –

1

我想你應該在參數的參數中刪除category = 1。

<?php 
global $post; 
$args = array( 
    'post_type' => 'post', 
    'posts_per_page' => 3, 
    'offset'=> 1, 
    'category_name' => 'news' 
); 
$myposts = get_posts($args); 
foreach ($myposts as $post) : setup_postdata($post); 
    the_title(); 
    the_content(); 

endforeach; 
wp_reset_postdata(); 
?> 

注意:請在category_name中使用slug類別。你也有語法錯誤

<?php the_content(); ?>"> //removed "> 

希望這會對你有用。謝謝

+0

想要將標題封裝在html標籤中但不起作用。請檢查我的編輯 –

0

我已經加入div標籤內容& h1標籤的更新代碼標題&。

請找到你的更新代碼:

<?php 
    query_posts( 
     array ( 
      'post_type' => 'post', 
      'category_name' => 'news', 
      'posts_per_page' => 3 
      ) 
     );  
     // The Loop 

     while (have_posts()) : the_post(); ?> 
      <h1><?php the_title(); ?></h1> 
      <div class="wrap"> 
       <?php the_content(); ?> 
      </div> 
     <?php endwhile; 
     // Reset Query 
     wp_reset_query(); 
?> 

希望,這將幫助你。