首页 文章

WordPress中的自定义博客存档页面

提问于
浏览
0

我想要做的是有一个页面列出我的所有帖子,一次十个,分页并从最近的帖子开始(第一页显示10个最新帖子,链接跳到下一个/最老/等.10帖子) . 我希望列表中的每个帖子都显示: Headers ,日期,摘录和“了解更多”按钮 . 当然,这意味着在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 ninjitsu?

3 回答

  • 0

    这是非常可行的,不是一个php高手(我将假设你至少有一些基本的编程知识) .

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

    所以它是 archive.php - > content.php .

    Archive.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() ): 这会检查是否有要显示的帖子 . 如果没有't it' ll显示 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 .

    Content.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() 检索并显示您可能希望将其包含在帖子链接中的帖子的 Headers .

    • the_time('jS F Y') 以特定格式获取时间 . 您可以学习如何更改格式here

    • the_excerpt() 显示帖子的摘录 . 如果您使用自定义帖子类型,那么您可以定义自己的摘录以显示,否则它只显示帖子中某个点的单词 .

    • the_permalink() 这是您帖子的生成链接 .

    我不能保证所有这些都可以立即起作用,但它应该足以让你前进 .

  • 0

    好吧,我一定找到了正确的搜索词,因为我弄清楚了!这是我在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个帖子,所以另一个页面应该可用,对吧?

  • 0

    Wordpress可以在某种程度上为您完成此操作 . 如果您真的想要个性化它,请连接到存储帖子的数据库(按照您提到的日期排序),然后构建一个视图模板,使用for循环为每个博客条目写入 Headers 和摘要 .

相关问题