Function and usage of adjacent_posts_rel_link() in wordpress

Question

Answers ( 1 )

    0
    2024-01-05T13:02:52+00:00

    The adjacent_posts_rel_link() function in WordPress is used to display relational links for posts that are adjacent (next or previous) to the current post. This function is particularly useful for creating a more navigable and user-friendly post structure on a WordPress site. Here's a detailed breakdown of the function and its parameters:

    Function:

    • Purpose: Displays the relational links for the posts adjacent to the current post.

    Parameters:

    1. $title (string, optional)

      • Description: Specifies the format of the link title. The default format is '%title', which will display the title of the adjacent post.
      • Default Value: '%title'
    2. $in_same_term (bool, optional)

      • Description: Determines whether the link should point to a post within the same taxonomy term. If set to true, the adjacent post will be from the same category or tag (depending on the taxonomy set).
      • Default Value: false
    3. $excluded_terms (int[] | string, optional)

      • Description: This parameter can be either an array or a comma-separated list of term IDs that you want to exclude. Posts within these terms will not be considered as adjacent posts.
      • Default Value: '' (empty string, meaning no terms are excluded)
    4. $taxonomy (string, optional)

      • Description: Specifies the taxonomy to be used when $in_same_term is true. Common values include 'category', 'post_tag', etc.
      • Default Value: 'category'

    Sample Usage:

    Let's consider a few examples of how adjacent_posts_rel_link() might be used:

    1. Basic Usage:

      adjacent_posts_rel_link();
      

      This will display relational links with the default settings: link titles based on the post title, not limited to the same term, and no excluded terms.

    2. Same Term Usage:

      adjacent_posts_rel_link('%title', true);
      

      This will display relational links for posts in the same category as the current post.

    3. With Excluded Terms:

      adjacent_posts_rel_link('%title', false, '3,7,9');
      

      This will display links excluding posts from the terms with IDs 3, 7, and 9.

    4. Custom Taxonomy Usage:

      adjacent_posts_rel_link('%title', true, '', 'post_tag');
      

      This will display links for posts in the same tag as the current post.

    By utilizing adjacent_posts_rel_link(), you can create a more connected and engaging post layout, making it easier for users to navigate through related content on your WordPress site.

Leave an answer