2017-02-17 51 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(); 
?> 

希望,这将帮助你。