Provide zblog template_zblog them_wordpress template download and customization

Wordpress obtains the website title of the previous article and the next article separately

Tianxing Studio 2022-03-22 09:16 Wordpress Tutorial eight hundred and ninety-one 0 Comments


wordpress There are many functions available from the previous article and the next article. By default, the "previous_post_link()" function in the previous article and the "next_post_link()" function in the next article are used. However, these two functions output a complete a tag. Although they can also be customized, how to customize them still outputs html.

In some personalized topics, these two functions are not personalized, so there is a need. How can WordPress get the website titles of the previous and next articles separately?

We need the function "get_adjacent_post()". The code example is as follows:

 $prev_obj  = get_adjacent_post('','',true);// Get Previous $next_obj  = get_adjacent_post('','',false);// Get Next

The function outputs an object that contains information such as the article ID, title, publishing time, and article content. You can directly use "$prev_obj ->XX" to obtain the corresponding information. For example, "$prev_obj ->ID" can obtain the ID of the previous article, and "$prev_obj ->post_title" can obtain the title of the previous article.


Finally, we need to get the URL separately. We can get the dynamic address of the previous article directly with "$prev_obj ->guid", so we must use the "get_permalink" function to get the correct URL. The code example is as follows

 echo get_permalink($prev_obj->ID);// Get the complete static URL of the previous article

To ensure that no error is reported without the previous or next article, the complete code is as follows before use:

 <? php $prev_obj  = get_adjacent_post('','',true);// Get Previous $next_obj  = get_adjacent_post('','',false);// Get Next if($prev_obj){ Echo '<a href="'. get_permalink ($prev_obj ->ID). '">Previous:'$ prev_obj->post_title.'</ a>'; } if($next_obj){ Echo '<a href="'. get_permalink ($next_obj ->ID). '">Next:'$ next_obj->post_title.'</ a>'; } ?>

Can't find a tutorial that can solve your problem?

You can try to search or ask questions directly online. We also provide charging technical support. If you need it, you can contact us online.

Online questions Online Service

Related recommendations

welcome you Comment: Cancel Reply

 Please fill in the verification code
  • Latest articles
  • Hot article ranking
  • Most Comments
Label aggregation
  • Sign in
  • Registered account Forgot your password?
  • register
  • Already have an account? Direct login Forgot your password?
  • Social account login