WordPress导航PHP代码



我正在我的网站上工作。我真的很喜欢我正在使用的主题,但帖子导航(下一篇文章/上一篇文章)相当基本。它仅显示博客文章标题(没有缩略图或摘录等)。您可以在本页底部看到一个示例:

http://www.telly.media/technology/test-post-seven/

相关的代码位似乎在单个.php文件中:

<?php the_post_navigation(); ?> 

有没有办法改变这一点,以便指向下一个/上一个帖子的链接看起来更像我主页上的框:

http://www.telly.media

谢谢!

您可以实现所需的功能,但不能仅通过使用<?php the_post_navigation(); ?>函数。

<?php the_post_navigation(); ?>功能显示指向下一篇文章/上一篇文章的导航链接。这就是为什么您只看到这些帖子的蓝色链接,而没有任何信息,例如摘录或缩略图。

另一方面,主页上的框可能是在Wordpress循环中创建的。这意味着Wordpress正在遍历所有可用的帖子,并且每个帖子都显示一个带有摘录和缩略图的框。

为了实现您想要的功能,您需要使用 get_adjacent_post() 函数以编程方式提取上一篇文章和下一篇文章的 id:

<?php
    $prev_post = get_adjacent_post( true, '', true, 'your_taxonomy_slug' );
    if ( is_a( $prev_post, 'WP_Post' ) ){
        $prev_post_id = $prev_post->ID;
    }
    $next_post = get_adjacent_post( true, '', false, 'your_taxonomy_slug' );
    if ( is_a( $next_post, 'WP_Post' ) ){
        $next_post_id = $next_post->ID;
    }
?>

然后,您将需要使用<?php $prev_post_obj = get_post($prev_post_id); ?><?php $next_post_obj = get_post($next_post_id); ?>对象来访问该帖子的字段,例如摘录,标题,永久链接或缩略图.etc 有关您可以访问的字段的完整列表,请参阅此链接的第一条评论。

您可以通过多种方式解决此问题,但您必须编写一些 css 样式或使用博客列表页面中已经使用的现有类。 使用此示例代替 the_post_navigation() 函数。示例 1:

<div class="navigation">
   <p>
      <?php posts_nav_link('&#8734;','&laquo;&laquo;','&raquo;&raquo;'); ?>
   </p>
</div>

示例 2:

<div class="navigation">
   <div class="alignleft">
       <?php previous_post_link('&laquo; &laquo; %','Toward The Past: ', 'yes');?>
   </div>
   <div class="alignright">
       <?php next_post_link('% &raquo; &raquo; ','Toward The Future: ', 'yes'); ?>
   </div>
</div>

或查看此链接以获取更多详细信息 https://codex.wordpress.org/Next_and_Previous_Links

我希望它能解决您的问题

最新更新