【问题标题】:How to display posts with Category named news如何显示类别命名为新闻的帖子
【发布时间】:2017-02-17 07:59:34
【问题描述】:

我刚刚创建了一个新的页面模板,想打印所有只包含新闻类别的帖子。

在工作时使用了这个基于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>

希望你能帮忙。

【问题讨论】:

  • 使用 category_name 或 category 中的任何一个。
  • @Kushal Shah 我刚刚删除了其中一个,但仍然没有显示
  • 你可以像这样添加 the_title( '

    ', '

    ' );

标签: wordpress categories


【解决方案1】:
 <?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();
    ?>

【讨论】:

  • 当然。感谢支持
【解决方案2】:

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

希望这对你有用。谢谢

【讨论】:

  • 希望将标题包装在 html 标记中,但不起作用。请检查我的编辑
【解决方案3】:

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

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 2016-05-01
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多