我们的网站目前正在使用"Yoast的WordPress SEO",
rel="next"并且rel="prev"在类别和存档页面上运行良好,但是在我们创建的页面模板中, rel="next"并rel="prev"没有显示.(此页面模板也有分页)
我们的网站结构=>我们有" Article"帖子类型
在文章中我们有类别
因为我希望网址 www.sitename.com/loan 没有../category/loan
我创建了名为"贷款"的"页面",并使用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;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 0
首先,您必须备份数据库。您可以在 phpmyadmin 上或使用一些为您执行备份的插件来实现此目的。
接下来,你应该做 url 的事情。您可以从“搜索外观 - Yoast SEO”页面中的类别中删除类别路径,单击“分类法”选项卡,然后删除类别 URL。
关于分页问题,您可以手动输入每个页面的链接,也可以使用php函数获取所有post类型文章的链接,如下所示:
wp_list_pages(array(
'child_of' => $post->post_parent,
'exclude' => $post->ID));
Run Code Online (Sandbox Code Playgroud)