繁体   English   中英

下一步如何添加博客?

[英]How do add the blog next prev navigation?

我对日记帖子使用自定义帖子类型,但是我想为上一页和下一页添加导航。 因此,如果我的日记页面上有5个以上的帖子,我将在下一个链接或2、3、4等数字上进行分页。如果没有更多的帖子,则它应该显示在前。 现在,我的日记页面在index.php上。

在我的wordpress阅读设置中,我在首页显示中使用了静态页面。 我的首页是front-page.php,帖子页面是期刊页面。 博客页面最多显示5个帖子。 联合供稿源显示了最近的5条帖子。

如何使用自定义帖子类型“新闻”添加下一个和上一个导航?

<?php 

    get_header();

    ?> 

    <!-- journal -->
    <section class="container-wrap">

        <?php

            $args  = array('post_type' => 'journals');
            $query = new WP_Query($args);

            while($query -> have_posts()) : $query -> the_post();

        ?>

        <article class="post-wrap">

            <header>
                <a href="<?php the_permalink(); ?>" class="post-title">
                    <h1 class="post-title"><?php the_title(); ?></h1>
                </a>
                <span class="post-date"><?php echo(types_render_field('date', array('format' => 'm.d.Y') )); ?></span>
            </header>
        </article>
        <?php endwhile; ?>

        <?php wp_reset_query(); ?>

    </section>
    <!-- /journal -->

    <?php 

    get_footer();

    ?>

paginate_links挂钩与自定义WP_Query数组结合使用。 确保为查询指定paged数组参数。 这将设置查询以返回分页结果。

  <?php
    // 1- Setup paging parameter
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

    // 2- Setup WP_query variable to get last 12 posts
    $args = array(
      'posts_per_page' => 12,
      'post_type' => 'journals',
      'orderby' => 'most_recent',
      'paged' => $paged,
    );
    $the_query = new WP_Query( $args );

    // 3- Setup new loop
    if($the_query->have_posts()) : 
      while($the_query->have_posts()) : 
        $the_query->the_post();

    // 4- Output parameters you want here
        echo '<div class="col-md-4">';
        echo '<h4><a href="' . the_permalink() . 'title="Read more">' . the_title() . '</a></h4>';
        echo '<a href="' . the_permalink() . '">' . the_post_thumbnail() . '</a>';
        echo the_excerpt();
        echo '</div>';

    // 5- close up loop
      endwhile;
    endif;

    // 6- Output paginate_links just below post loop
    echo paginate_links( array(
      'base' => str_replace( 999999, '%#%', esc_url( get_pagenum_link( 999999 ) ) ),
      'format' => '?paged=%#%',
      'current' => max( 1, get_query_var('paged') ),
      'total' => $the_query->max_num_pages
    ) );

    // 7- reset post data query
    wp_reset_postdata(); 
  ?>

试试我的代码,它可以在我的网站上运行http://www.thehiddenwhy.com/blog/,请点击此链接如何在wordpress的页面代码中创建分页?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM