【问题标题】:Make an archive page with wordpress使用 wordpress 制作存档页面
【发布时间】:2015-09-16 11:10:10
【问题描述】:

我想创建一个按此顺序显示帖子的存档页面:

post_title |发布日期 | post_category

当然,这应该是可点击的,链接到帖子,或者如果你点击一个类别,它应该链接到一个类别

到目前为止我有什么:

    $args = array(
        'post_type' => 'post'
    );

    $post_query = new WP_Query($args);

    if($post_query->have_posts() ) {
      while($post_query->have_posts() ) {
        $post_query->the_post();
        ?>
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Link naar <?php the_title_attribute(); ?>"><?php the_title(); ?></a> | <?php the_date();?> | <?php get_the_category;?></h2>
        <?php
      }
    }
    ?>

我知道“the_permalink()”给了我发帖的链接,而“the_title”给了我标题。

此代码仅显示最后 10 个帖子,而不是 3000 个。

另一个问题是日期仅显示在 10 个帖子中的 2 个上。

该类别根本不显示。

这是我第一次尝试真正使用 wordpress,也许我真的做错了,所以我希望大家能帮助我。提前致谢。

【问题讨论】:

    标签: php wordpress pagination


    【解决方案1】:

    此代码仅显示最后 10 个帖子,而不是 3000 个。

    这是正常的 Wordpress 行为。默认情况下,如果您未将posts_per_pagenumberposts 参数指定给get_posts,它将使用在设置> 读数中设置的值。所以把你的 args 改成这个(-1 意味着它会显示你所有的帖子——如果你真的需要它是 3000,把它改成 3000):

    $args = array(
      'post_type' => 'post',
      'numberposts' => -1
    );
    

    另一个问题是日期只显示在 10 个帖子中的 2 个。

    奇怪的是,这也是默认的 Wordpress 行为(在我看来有点令人困惑)。如果您查看 the_date 文档页面上的特别说明,它会告诉您:

    当同一天发布的页面上有多个帖子时, the_date() 只显示第一篇文章的日期

    如果你想解决这个问题并显示所有帖子的日期,你需要使用get_the_date(它返回日期,所以你需要echo它)。

    该类别根本不显示。

    你错过了使用get_the_category - 它返回一个与帖子关联的类别对象数组,而不是echo 任何东西。要显示当前帖子类别的链接,您需要使用get_the_categoryget_category_link 的组合:

    $category = get_the_category();
    echo '<a href="' . get_category_link($category[0]->cat_ID) .'">' . $category[0]->cat_name . '</a>';
    

    【讨论】:

    • 请注意,如果您不介意使用列表标记获取所有类别,@Karthik 类别答案也适用(使用 get_the_category_list)。
    【解决方案2】:

    您可以像这样控制帖子的数量(比如 50 个):

    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 50
    );
    

    要显示日期和类别使用这个:

    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Link naar <?php the_title_attribute(); ?>"><?php the_title(); ?></a> |
    <?php
        echo get_the_date();
        echo ' | ';
        $category_list = get_the_category_list( ', ' );
        if ( $category_list )
            echo $category_list;
    ?>
    </h2>
    

    希望对您有所帮助。如果没有,请随时询问!

    【讨论】:

    • 感谢这真的很好。这样我就可以继续构建我的自定义存档
    猜你喜欢
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多