简体   繁体   中英

Is there anyway to add rel=“next”/rel=“prev” into “Page Template” with WordPress SEO by Yoast?

Our site is currently using "WordPress SEO by Yoast" rel="next" and rel="prev" is working fine on category and archive page, however in page template that we created, rel="next" and rel="prev" is not showing. (This page template also has pagination)

Our website structure => we have " Article " Post type

in Article we have category

  • Credit card
  • Cash card
  • Loan
  • etc.

As I want the url to be www.sitename.com/loan without having ../category/loan

I created 'Page' called 'Loan' and using page-loan.php as page template to query Post type 'Article' category 'Loan'

I want to have rel="next" and rel="prev" appear in this page template as well I wonder is there anyway to use WordPress SEO by Yoast to do it?

or is there anyway to modify below script in the plugin to make rel="next" and rel="prev" appear in Page template as well?

the script I found in the plugin

    public function adjacent_rel_links() {
    // Don't do this for Genesis, as the way Genesis handles homepage functionality is different and causes issues sometimes.
    /**
     * Filter 'wpseo_genesis_force_adjacent_rel_home' - Allows devs to allow echoing rel="next" / rel="prev" by WP SEO on Genesis installs
     *
     * @api bool $unsigned Whether or not to rel=next / rel=prev
     */
    if ( is_home() && function_exists( 'genesis' ) && apply_filters( 'wpseo_genesis_force_adjacent_rel_home', false ) === false ) {
        return;
    }

    global $wp_query;

    if ( ! is_singular() ) {
        $url = $this->canonical( false, true, true );

        if ( is_string( $url ) && $url !== '' ) {
            $paged = get_query_var( 'paged' );

            if ( 0 == $paged ) {
                $paged = 1;
            }

            if ( $paged == 2 ) {
                $this->adjacent_rel_link( 'prev', $url, ( $paged - 1 ), true );
            }

            // Make sure to use index.php when needed, done after paged == 2 check so the prev links to homepage will not have index.php erroneously.
            if ( is_front_page() ) {
                $url = wpseo_xml_sitemaps_base_url( '' );
            }

            if ( $paged > 2 ) {
                $this->adjacent_rel_link( 'prev', $url, ( $paged - 1 ), true );
            }

            if ( $paged < $wp_query->max_num_pages ) {
                $this->adjacent_rel_link( 'next', $url, ( $paged + 1 ), true );
            }
        }
    }
    else {
        $numpages = 0;
        if ( isset( $wp_query->post->post_content ) ) {
            $numpages = ( substr_count( $wp_query->post->post_content, '<!--nextpage-->' ) + 1 );
        }
        if ( $numpages > 1 ) {
            $page = get_query_var( 'page' );
            if ( ! $page ) {
                $page = 1;
            }

            $url = get_permalink( $wp_query->post->ID );

            // If the current page is the frontpage, pagination should use /base/
            if ( $this->is_home_static_page() ) {
                $usebase = true;
            }
            else {
                $usebase = false;
            }

            if ( $page > 1 ) {
                $this->adjacent_rel_link( 'prev', $url, ( $page - 1 ), $usebase, 'single_paged' );
            }
            if ( $page < $numpages ) {
                $this->adjacent_rel_link( 'next', $url, ( $page + 1 ), $usebase, 'single_paged' );
            }
        }
    }
}

/**
 * Get adjacent pages link for archives
 *
 * @since 1.0.2
 *
 * @param string  $rel                  Link relationship, prev or next.
 * @param string  $url                  the un-paginated URL of the current archive.
 * @param string  $page                 the page number to add on to $url for the $link tag.
 * @param boolean $incl_pagination_base whether or not to include /page/ or not.
 *
 * @return void
 */
private function adjacent_rel_link( $rel, $url, $page, $incl_pagination_base ) {
    global $wp_rewrite;
    if ( ! $wp_rewrite->using_permalinks() ) {
        if ( $page > 1 ) {
            $url = add_query_arg( 'paged', $page, $url );
        }
    }
    else {
        if ( $page > 1 ) {
            $base = '';
            if ( $incl_pagination_base ) {
                $base = trailingslashit( $wp_rewrite->pagination_base );
            }
            $url = user_trailingslashit( trailingslashit( $url ) . $base . $page );
        }
    }
    /**
     * Filter: 'wpseo_' . $rel . '_rel_link' - Allow changing link rel output by WP SEO
     *
     * @api string $unsigned The full `<link` element.
     */
    $link = apply_filters( 'wpseo_' . $rel . '_rel_link', '<link rel="' . esc_attr( $rel ) . '" href="' . esc_url( $url ) . "\" />\n" );

    if ( is_string( $link ) && $link !== '' ) {
        echo $link;
    }
}

First of all, you have to make a backup of your database. You can achieve this on phpmyadmin or with some plugin that do the backup for you.

Next, you should do the url thing. You can remove the category path from categories in the page "Search Appearance - Yoast SEO", click on tab Taxonomies, then remove the Category URLs.

On the pagination issue, you could manually input the links on each page or use a php function to get all the links of post type articles like this:

wp_list_pages(array(
'child_of' => $post->post_parent,
'exclude' => $post->ID));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM