仅某些类别的下一个和上一个代码故障

Posted

技术标签:

【中文标题】仅某些类别的下一个和上一个代码故障【英文标题】:Next and Previous code malfunction on certain categories only 【发布时间】:2020-05-17 00:55:55 【问题描述】:

我在这里完全不知所措。我在我的函数中放置了一个代码,以启用一个短代码按类别执行上一页和下一页/帖子。 我有7个类别。在 3 个类别中,我已经完成了短代码的完美工作。但不知何故它不在第四。我还没有尝试过其他类别。我已将所有页面放在同一发布日期,正确设置页面顺序,具有相同的父文件夹,具有相同的类别。我什至删除了该类别并重新创建了一个认为可能出现故障的类别。

请帮助我,我很困惑,真的不想回去做从一个页面到另一个页面的链接.... 我的网站是 somaliforkids.com Learn 中的数字、字母和颜色类别都很好.... 动物类别 (https://somaliforkids.com/learn/animals/) 毫无意义... 没有逻辑... 从第 1 页到第 3 页到第 9 页到第 6 页到16. 不遵循标题、日期的字母...我在我的页面中使用 Elementor 来放置简码。

这是我的代码:

     // next   
   add_shortcode( 'prev', 'prev_shortcode' );
   add_shortcode( 'next', 'next_shortcode' );
   function next_shortcode($atts) 
    global $post;
    ob_start(); 
    next_post_link( '<div class="nav-next">%link</div>', '<img class="aligncenter" 
    src="https://somaliforkids.com/wp-content/uploads/2020/05/right-black.png"  >', 
   '%title <span class="meta-nav">' . _x( '→', 'Previous post link', 'morphology' ) . '</span>',
         true);              
    $result = ob_get_contents();
    ob_end_clean();
    return $result;
   

   function prev_shortcode($atts) 
    global $post;
    ob_start();
    previous_post_link( '<div class="nav-previous">%link</div>', '<img class="aligncenter" 
   src="https://somaliforkids.com/wp-content/uploads/2020/05/left-black.png"  >', 
   '%title <span class="meta-nav">' . _x( '→', 'Previous post link', 'morphology' ) . '</span>',
         true );              
    $result = ob_get_contents();
    ob_end_clean();
    return $result;
   

感谢您的帮助, 伊莎贝尔

【问题讨论】:

【参考方案1】:

我认为您不需要为此创建简码,因为 wordpress 已经具备您需要的功能。您只是没有使用它们的参数:

next_post_link();
previous_post_link();

当您阅读 next_post_link 的函数代码参考时,您会看到它接受多个参数:https://developer.wordpress.org/reference/functions/next_post_link/

其中之一是:

$in_same_term(bool)

(可选)链接是否应该在同一个分类术语中。默认值:false

因此,您只需将 single.php 文件放在您想要的位置:

<?php next_post_link( '%link', 'Next post in category', true ); ?>

当然你可以使用你的箭头黑色图像等...真实的是 $in_same_term 参数。

如果您希望帖子属于特定分类,您可以使用:

<?php next_post_link( '%link', 'Next post in taxonomy', true, '', 'your_custom_taxonomy' ); ?>

看到没有价值的''?如果需要,这适用于排除术语 ID 的数组或逗号分隔列表。

希望这会有所帮助。

【讨论】:

以上是关于仅某些类别的下一个和上一个代码故障的主要内容,如果未能解决你的问题,请参考以下文章

滚动库中的下一个和上一个按钮中的错误

在 wordpress 中获取 2 级类别中的下一个、上一个帖子

下一个和上一个按键 Jquery

使用codeigniter中的下一个和上一个按钮进行分页

如何处理 AVPlayerViewController 中的下一个和上一个按钮?

如何从数据库c#中创建多年的下一个和上一个按钮