WordPress中的帖子/页面导航链接的条件标记

Have you ever visited a site where they have the styling for post navigation links, but there are no links there? It looks quite ugly and it is distracting for the user. Well if you are a theme designer, or a site owner that is having this issue then this solution is just for you. In this article we are going to share how you can add a conditional tag for post/page navigation links to ensure the best styling for your blog design.

您是否曾经访问过他们的站点具有导航链接样式,但是那里没有链接? 它看起来很丑陋,并且让用户分心。 好吧,如果您是主题设计师或遇到此问题的网站所有者,那么此解决方案仅适合您。 在本文中,我们将分享如何为帖子/页面导航链接添加条件标签,以确保博客设计的最佳样式。

First open your theme’s functions.php file and add the following code:

首先打开主题的functions.php文件,并添加以下代码:

/**
* If more than one page exists, return TRUE.
*/
function show_posts_nav() {
global $wp_query;
return ($wp_query->max_num_pages > 1);
}

/ **
*如果存在多个页面,则返回TRUE。
* /
函数show_posts_nav(){
全局$ wp_query;
返回($ wp_query-> max_num_pages> 1);
}

Then place the following code where you want the navigation to be:

然后将以下代码放置在要导航的位置:

<?php if (show_posts_nav()) : ?>
<div class='navigation'>
<span class='older'><?php next_posts_link('&laquo; Older Entries'); ?></span>
<span class='newer'><?php previous_posts_link('Newer Entries &raquo;'); ?></span>
</div>
<?php endif; ?>

<?php if(show_posts_nav()):?>
<div class ='导航'>
<span class ='older'> <?php next_posts_link('&laquo; Older Entries'); ?> </ span>
<span class ='newer'> <?php previous_posts_link('New Entries&raquo;'); ?> </ span>
</ div>
<?php endif; ?>

You don’t have to follow our styling at all. You can modify this code any way you like. The important part is that you keep the navigation styling in between this function:

您完全不必遵循我们的样式。 您可以按自己喜欢的任何方式修改此代码。 重要的部分是您应将导航样式保留在此函数之间:

<?php if (show_posts_nav()) : ?>

<?php if(show_posts_nav()):?>

<?php endif; ?>

<?php endif; ?>

This function will check to see if there is more than one page. If there are more than one page, then it will show the navigation post with the styling. If there are no other pages, then it will not show the styling or the navigation.

此功能将检查是否有一页以上。 如果有多个页面,则它将显示带有样式的导航帖子。 如果没有其他页面,则不会显示样式或导航。

Note: the WordPress navigation function is smart enough to not show navigation on it’s own if there are no other pages. But if you have navigation wrapped around with your own custom styling, then those styling code still shows whether navigation is there or not. The function in this post take care of this problem.

注意:WordPress导航功能足够智能,如果没有其他页面,则不会自行显示导航。 但是,如果您使用自己的自定义样式来包装导航,则这些样式代码仍会显示导航是否存在。 这篇文章中的功能解决了这个问题。

其他资源: (Additional Resources: )

Eric Martin’s Post – Source for this snippet

埃里克·马丁(Eric Martin)的帖子-此摘要的来源

Digging into WordPress (A Great eBook that everyone should buy if you are learning WordPress)

深入研究WordPress(一本很棒的电子书,如果您正在学习WordPress,每个人都应该购买)

翻译自: https://www.wpbeginner.com/wp-themes/conditional-tag-for-postpage-navigation-links-in-wordpress/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值