下面的代码来自Yoast的Wordpress SEO插件。我正在尝试将页码添加到分页帖子的元描述中(以避免谷歌出现重复问题)。
function metadesc( $echo = true ) {
if ( get_query_var(\'paged\') && get_query_var(\'paged\') > 1 )
return;
global $post, $wp_query, $page;
$options = get_wpseo_options();
$metadesc = \'\';
if (is_singular()) {
$metadesc = wpseo_get_value(\'metadesc\');
if ($metadesc == \'\' || !$metadesc) {
if ( isset($options[\'metadesc-\'.$post->post_type]) && $options[\'metadesc-\'.$post->post_type] != \'\' )
$metadesc = wpseo_replace_vars($options[\'metadesc-\'.$post->post_type], (array) $post );
}
}
$metadesc = apply_filters( \'wpseo_metadesc\', trim( $metadesc ) );
if ( $echo ) {
if ( !empty( $metadesc ) )
echo \'<meta name="description" content="\'.esc_attr( strip_tags( stripslashes( $metadesc ) ) ).\'\'.Page .\'\'.$page.\'"/>\'."\\n";
else if ( current_user_can(\'manage_options\') && is_singular() )
echo \'<!-- \'.__( \'Admin only notice: this page doesn\\\'t show a meta description because it doesn\\\'t have one, either write it for this page specifically or go into the SEO -> Titles menu and set up a template.\', \'wordpress-seo\' ).\' -->\'."\\n";
} else {
return $metadesc;
}
}
我添加了$page作为全局变量,可以输出页码,如下所示:
<meta name="description" content="Wordpress SeoPage3"/>
我希望输出在第0页(即第一页)上省略页码,然后添加空格和分隔符(管道或破折号),使其显示
<meta name="description" content="Wordpress Seo Page | 3"/>
谢谢。。
最合适的回答,由SO网友:fuxia 整理而成
要向任何插件添加页码,请使用过滤器。不要更改插件文件。您不能以其他方式运行更新。
示例:
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: T5 Add page number to title
* Description: Adds <code> | Page $number</code> to the page title.
* License: MIT
* License URI: http://www.opensource.org/licenses/mit-license.php
*/
if ( ! function_exists( \'t5_add_page_number\' ) )
{
function t5_add_page_number( $s )
{
global $page;
$paged = get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : 1;
! empty ( $page ) && 1 < $page && $paged = $page;
$paged > 1 && $s .= \' | \' . sprintf( __( \'Page: %s\' ), $paged );
return $s;
}
add_filter( \'wp_title\', \'t5_add_page_number\', 100, 1 );
add_filter( \'wpseo_metadesc\', \'t5_add_page_number\', 100, 1 );
}