【问题标题】:Custom Blog Archive Page in WordPressWordPress中的自定义博客存档页面
【发布时间】:2014-06-17 04:15:30
【问题描述】:

我想要做的是有一个页面列出我所有的帖子,一次十个,分页并从最近的帖子开始(第一页显示 10 个最近的帖子,链接可以跳到下一个/最旧的/等 10 个帖子)。我希望列表中的每个帖子都显示:标题、日期、摘录和“了解更多”按钮。当然,这意味着在 div 中设置每个归档帖子的样式。这是我如何在我的 html 原型中设置样式的屏幕截图:

https://plus.google.com/105168282869678130658/posts/hMxABrRqKDf

下面是 div 的结构(CSS 样式工作得很好,所以我不会让你厌烦):

<div class="storyPreview">
   <h2>[TITLE OF POST]</h2>
   <p><em>[DATE OF POST]</em></p><br>
   <p>[EXCERPT FROM POST]</p><br>
   <a class="learnMore" href="[LINK TO POST HERE]">Learn More</a>
</div>

这是我的archive.php,到目前为止:

<?php
/*
Template Name: Archives
*/
?>

<?php get_header(); ?>

    <main role="main">
        <!-- section -->
        <section>

            <h1>News &amp; Blog Archive</h1>


        </section>
        <!-- /section -->
    </main>

<?php get_footer(); ?>

这可能吗?是否需要超级复杂的 PHP 忍术?

【问题讨论】:

    标签: php html wordpress customization archive


    【解决方案1】:

    Wordpress 可以在一定程度上为您做到这一点。如果您真的想个性化它,请连接到存储帖子的数据库(按您提到的日期排序),然后构建一个视图模板,使用 for 循环为每个博客条目编写标题和摘要。

    【讨论】:

      【解决方案2】:

      这是非常可行的,无需成为 php 高手(我假设您至少具备一些基本的编程知识)。

      让我们从了解 Wordpress 如何生成页面开始。在您的情况下,您需要存档。您的archive.php 页面将寻找创建单独的帖子,在我的首选情况下,它将寻找文件 content.php 以查找要吐出的内容。您可以在 here 的层次结构上查看更多信息。

      所以archive.php -> content.php

      存档.php

      <?php
      /*
      Template Name: Archives
      */
      ?>
      
      <?php get_header(); ?>
      
          <main role="main">
              <section>
                  <h1>News &amp; Blog Archive</h1>
      
                  <?php if ( have_posts() ) :?>
      
                      <?php while ( have_posts() ) : the_post();?>
                          <?php
                              get_template_part( 'content', get_post_format());
                          ?>
                      <?php endwhile; ?>
      
                  <?php else : ?>
                      <?php get_template_part( 'no-results', 'archive' );?> 
                  <?php endif; ?>
              </section>
          </main>
      
      <?php get_footer(); ?>
      

      我会解释每个部分的作用

      • if ( have_posts() ): 这会检查是否有要显示的帖子。如果没有,它将显示 no-results.php 页面。
      • while ( have_posts() ) : the_post(); 这将遍历您的所有帖子
      • get_template_part( 'content', get_post_format()); 显示实际帖子,检查文件 content.php
      • get_template_part( 'no-results', 'archive' ); 如果没有找到帖子,则显示 no-results.php 页面。

      所以现在你已经让archive.php 寻找content.php

      内容.php

      您可能需要重新设置样式,我使用了一篇文章(其中包含帖子信息)来包装您的 div 的外部。

      <?php
      /**
       * @package Generate
       */
      ?>
      
      <article id="post-<?php the_ID(); ?>" <?php post_class(); ?> itemprop="blogPost" itemtype="http://schema.org/BlogPosting" itemscope="itemscope">
          <div class="storyPreview">
             <h2><?php the_title(); ?></h2>
             <p><?php the_time('jS F Y'); ?></p><br>
             <p><?php the_excerpt(); ?></p><br>
             <a class="learnMore" href="<?php the_permalink(); ?>">Learn More</a>
          </div>
      </article>
      

      应该更不言自明,但无论如何我都会解释它们:

      • the_title() 检索并显示帖子的标题您可能希望将其包含在帖子的链接中
      • the_time('jS F Y') 获取特定格式的时间。您可以学习如何更改格式here
      • the_excerpt() 显示帖子的摘录。如果您使用的是自定义帖子类型,那么您可以定义要显示的自己的摘录,否则它只会显示帖子中某个点的字词。
      • the_permalink()这是生成的帖子链接。

      我不能保证所有这些都能立即奏效,但至少足以让你继续前进。

      【讨论】:

      • 太棒了!快到了!但是,出于某种原因,它只显示一个“帖子”:新闻和博客页面。所以基本上,while 循环只显示一页——它本身——而不是我添加的 11 个测试帖子。如何让它显示帖子而不是页面本身的链接?再次感谢。
      【解决方案3】:

      好的,我一定找到了正确的搜索词,因为我想通了!这是我在archive.php中使用的:

      <main role="main">
          <?php $args = array(
          'numberposts'     => 10,
          'offset'          => 0,
          'orderby'         => 'post_date',
          'order'           => 'DESC',
          'post_type'       => 'post',
          'post_status'     => 'publish',
          'suppress_filters' => true ); ?>
      
          <?php $posts_array = get_posts( $args ); ?>
          <!-- section -->
          <section>
      
          <h1>News &amp; Blog Archive</h1>
              <?php if ($posts_array) : ?>
      
                  <?php foreach ($posts_array as $post) :  setup_postdata($post); ?>
                      <?php
                          get_template_part( 'content', get_post_format());
                      ?>
                  <?php endforeach; ?>
               <?php else : ?>
              <?php endif; ?>
      
              <?php next_posts_link('View Older Entries') ?>
      
      
          </section>
          <!-- /section -->
      </main>
      

      但是...还有最后一期,如果您知道,请随时发表评论(否则,我相信我最终能找到答案):next_posts_link 不起作用;什么都没有出现。我目前有 11 个帖子,所以应该有另一个页面可用,对吧?

      【讨论】:

      • 好的,所以我发现(使用 if/else)next_posts_link 没有显示,因为该页面无法识别超过第十个的旧帖子。注释掉“numberposts”行也无济于事。如何使用此代码让旧帖子分页?我可以吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-18
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 2015-07-16
      相关资源
      最近更新 更多