如何显示类别名为新闻的帖子

How to display posts with Category named 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>

希望能帮到你。

我认为你应该删除 arguments 的参数中的 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 ">

希望这对您有用。谢谢

 <?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();
    ?>

我已经为内容添加了 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();
?>

希望对您有所帮助。