静态页面,需要底部的上一个和下一个链接

时间:2020-01-10 作者:Jose Torres-Vega

我是新来的,我对PHP没有太多经验,但如果我有一个正确的块,我可以处理不同的函数。我有一个静态页面,目前,我将其设置为显示5个帖子片段,我们在这个类别上有很多帖子,我需要这个页面有一行链接

最新的1、2、3。。。最旧的。。。

我无法让它显示。。。

我当前的代码如下所示:

 <?php  while ( have_posts() ) : the_post(); ?> 
 <div class="title text-center">    
 <h1><strong><?php the_title();?></strong></h1>
 <img src="<?php echo get_template_directory_uri(); ?>/images/title.png" title="title-line" alt="title-line">

     </div>
 <div id="no-more-tables" class="legel">
 <?php the_content();?></div>
<?php 
$posts = get_posts(array(
  \'posts_per_page\'  => 5,
  \'category\'=>\'13\'
));
if( $posts ): ?>

<div>
<?php foreach( $posts as $post ): 

    setup_postdata( $post );


    ?>
我需要添加什么才能做到这一点?我正在看这个例子,但对我来说没有意义:

<?php if ( $the_query->have_posts() ) : ?>
  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <?php the_title(); ?>
    <?php the_excerpt(); ?>

  <?php endwhile; ?>
  <?php wp_reset_postdata(); ?>

<?php else : ?>
  <p><?php __(\'No News\'); ?></p>
<?php endif; ?>
如果您有任何建议,我将不胜感激。。。

2 个回复
SO网友:Andy Mardell

像这样的事情应该行得通。我在注释中对代码添加了一些解释:

<div class="title text-center">    
    <h1><strong><?php the_title(); ?></strong></h1>
    <img src="<?php echo get_template_directory_uri(); ?>/images/title.png" title="title-line" alt="title-line">
</div>

<div id="no-more-tables" class="legel">
    <?php the_content(); ?>
</div>

<?php
// Get the page from the url e.g. domain.com/articles?paged=2 would be page 2.
$paged = ( get_query_var( \'paged\' ) ) ? absint( get_query_var( \'paged\' ) ) : 1;

// Use WP_Query instead of get_posts() - it is much better for proper queries.
// get_posts() should only really be used for getting a few posts (e.g. related
// articles)
$category_posts = WP_Query(
    array(
        \'posts_per_page\' => 5,
        \'category\'       => \'13\',
        \'paged\'          => $paged,
    )
);
?>

<?php if ( $category_posts->have_posts() ) : ?>
    <?php while ( $category_posts->have_posts() ) : $category_posts->the_post(); ?>

        <?php the_title(); ?>
        <?php the_content(); ?>

    <?php endwhile; ?>

    <?php
    // Set $big as a massive integer
    $big = 999999999;
    // Echo the pagination (E.g. Prev 1 2 3 3 4 5 Next)
    echo paginate_links(
        array(
            \'base\'    => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
            \'format\'  => \'?paged=%#%\', // Matches your query above
            \'current\' => max( 1, get_query_var( \'paged\' ) ),
            \'total\'   => $category_posts->max_num_pages,
        )
    );
    ?>

    <?php wp_reset_postdata(); // This must be run after every WP_Query ?>

<?php else : ?>

    No Posts

<?php endif; ?>

SO网友:Jose Torres-Vega

Andy,它不起作用,但我认为这是因为它与out模板的结尾不匹配,在setup\\u postdata($post)。。。我们有以下填充:

  <div>
    <h3 style="padding-bottom:6px;">
      <a href="<?php echo esc_url(get_permalink($post->ID)); ?>"><?php the_title(); ?></a>
    </h3></div>
    <div style="color:#595959;">Posted on: <?php echo date(\'Y/m/d\',strtotime($post->post_date)); //echo date(\'F j, Y\', strtotime($post->post_date)); ?></div>
    <div><?php echo \'<p style="padding-bottom:6px !important;">\' . get_the_excerpt() . \'</p>\'?>
    </div>
  <?php endforeach; ?>
</div>
<?php wp_reset_postdata(); ?>

<?php endif; ?>
<?php   endwhile;  // End of the loop. ?>
<?php get_footer(); ?>