WordPress Tips: Add title attribute to the link of the previous and next articles

» WordPress » WordPress Tips: Add title attribute to the link of the previous and next articles

WordPress小技巧:上一篇下一篇文章链接添加title属性-极客公园

Recently, a friend asked me a question when he was learning Wordpress. Why didn't he add the previous and next articles on the article page title Properties? I didn't notice this problem when I looked carefully, so I searched Baidu and found that many people have this demand, but there is no satisfactory answer. One solution introduced online is to modify Wordpress program files, but I personally don't like it. One is that you'd better not move the program files unless you want to be a Wordpress program developer; The second is that when you update the program next time, you have to modify it again, which is too troublesome and not suitable for lazy people like me.

Well, take a closer look. Generally, the code for adding the previous article and the next article looks like this:

 <? php previous_post_link('%link','<<') ?>  <? php next_post_link('%link','>>') ?>

The code finally parsed from this code is roughly as follows:

 ……    ……

The structure of this sub is very simple. If I want to add title target When the attribute value is equal, the above two functions alone cannot do it. In fact, it is very simple to solve this problem. I wonder if you have access to these two functions: get_previous_post get_next_post Through these two functions, we can obtain the relevant information of the previous article and the next article. You can go to the official website to see the introduction of these two functions.

Ok, let's dry up and talk about the solution to the problem. As long as:

 <? php previous_post_link('%link','<<') ?>  <? php next_post_link('%link','>>') ?>

Replace with:

 <? php  $prev_post = get_previous_post();   if (! empty( $prev_post )): ?>    < a title="<? php echo $prev_post->post_title; ?>" href="<?php echo get_permalink( $prev_post->ID ); ?> "><?php echo $prev_post->post_title; ?></a>  <?php endif; ?>  <?php  $next_post = get_next_post();   if (! empty( $next_post )): ?>    < a title="<? php echo $next_post->post_title; ?>" href="<?php echo get_permalink( $next_post->ID ); ?> "><? php echo $next_post->post_title; ?></a>  <?php endif; ?>

Through the above replacement, the problem is solved perfectly. In addition to adding title In addition to the properties, you can add the properties opened in the new window if necessary: target:"_blank"

Well, I hope this tip will be useful to you. If you have good tips, you can also share them with us, so that everyone can enjoy your sense of achievement.

Reprinted from: WordPress Tips: Add title attribute to the link of the previous and next articles

--End--

最近有个朋友在学Wordpress时,期间问了我一个问题,当在文章页面添加上一篇和下一篇文章时,怎么没有title属性?我仔细一看还真没有,以前倒没注意到这个问题,于是在百度上搜索了一番,发现挺多人都有这方面的需求,但没有一个满意的答案,网上介绍的一种解决方法是修改Wordpress程序文件来实现,但我个人非常不喜欢。一则是程序的文件你最好别动,除非你想成为Wordpress的程序开发人员;二则是当你下次更新程序的时候,又得重新修改,太麻烦了,不适合我这种懒人。

好吧,仔细研究一下吧,一般我们添加上一篇和下一篇文章时的代码是这样子的:

<?php previous_post_link('%link','<<') ?>  <?php next_post_link('%link','>>') ?>

该代码最终解析出来的代码大概如下:

 ……    …… 

这样子的结构是非常简单,如果我要增加titletarget等属性值时,单靠上面两个函数是办不到的。其实要解决这个问题很简单,不知道大家有没有接触到这两个函数:get_previous_postget_next_post。通过这两个函数我们可以获取到上一篇和下一篇文章的相关信息。大家可以到官网看看这两个函数的介绍。

好了,下面就来干货,说下解决问题的方法。只要将:

<?php previous_post_link('%link','<<') ?>  <?php next_post_link('%link','>>') ?>

替换成:

<?php  $prev_post = get_previous_post();  if (!empty( $prev_post )): ?>    <a title="<?php echo $prev_post->post_title; ?>" href="<?php echo get_permalink( $prev_post->ID ); ?>"><?php echo $prev_post->post_title; ?></a>  <?php endif; ?>  <?php  $next_post = get_next_post();  if (!empty( $next_post )): ?>    <a title="<?php echo $next_post->post_title; ?>" href="<?php echo get_permalink( $next_post->ID ); ?>"><?php echo $next_post->post_title; ?></a>  <?php endif; ?>

通过上面的替换,问题就完美解决了。除了可以添加title属性外,大家如果有需要也可以加上新窗口打开的属性:target:"_blank"

好了,这个小技巧希望对大家有用。如果你有好的小技巧也可以和我们一起分享,让大家一起享受你的成就感。

转载自:WordPress小技巧:上一篇下一篇文章链接添加title属性

-- 完 --

Post reply

Your email address will not be disclosed. Required items have been used * tagging