无论如何使用Yoast的WordPress SEO将rel =“next”/ rel =“prev”添加到“页面模板”中?

时间:2015-06-19 15:51:37

标签: php wordpress

我们的网站目前正在使用“Yoast的WordPress SEO”  rel="next"rel="prev"在类别和归档页面上运行良好,但在我们创建的页面模板中,rel="next"rel="prev"未显示。 (此页面模板也有分页)

我们的网站结构=>我们有“Article”帖子类型

在文章中我们有类别

  • 信用卡
  • 现金卡
  • 贷款

我希望网址为www.sitename.com/loan而没有../category/loan

我创建了名为“贷款”的'Page',并使用page-loan.php作为页面模板来查询帖子类型'文章'类别'贷款'

我希望此页面模板中也显示rel="next"rel="prev" 我不知道有没有使用Yoast的WordPress SEO来做它?

或者是否仍然要修改插件中的以下脚本,以便在页面模板中显示rel="next"rel="prev"

我在插件中找到的脚本

    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;
    }
}

1 个答案:

答案 0 :(得分:0)

首先,您必须备份数据库。你可以在phpmyadmin或者为你做备份的插件上实现这一点。

接下来,你应该做url的事情。您可以从页面中的类别中删除类别路径&#34;搜索外观 - Yoast SEO&#34;,单击选项卡分类,然后删除类别URL。

在分页问题上,您可以手动输入每个页面上的链接或使用php函数获取此类帖子类型文章的所有链接:

hashCode()