wordpress添加canonical集权标签

分享2种纯代码为 WordPress 首页、分类、标签和文章页自动添加 canonical 标签的方法,将下面任意一份代码添加到 WordPress 主题 functions.php 文件中。

  1. remove_action( 'wp_head', 'rel_canonical' );
  2. function cccitu_archive_link( $paged = true ) {
  3. $link = false;
  4. if ( is_front_page() ) {
  5. $link = home_url( '/' );
  6. } else if ( is_home() && "page" == get_option('show_on_front') ) {
  7. $link = get_permalink( get_option( 'page_for_posts' ) );
  8. } else if ( is_tax() || is_tag() || is_category() ) {
  9. $term = get_queried_object();
  10. $link = get_term_link( $term, $term->taxonomy );
  11. } else if ( is_post_type_archive() ) {
  12. $link = get_post_type_archive_link( get_post_type() );
  13. } else if ( is_author() ) {
  14. $link = get_author_posts_url( get_query_var('author'), get_query_var('author_name') );
  15. } else if ( is_single() ) {
  16. $link = get_permalink( $id );
  17. } else if ( is_archive() ) {
  18. if ( is_date() ) {
  19. if ( is_day() ) {
  20. $link = get_day_link( get_query_var('year'), get_query_var('monthnum'), get_query_var('day') );
  21. } else if ( is_month() ) {
  22. $link = get_month_link( get_query_var('year'), get_query_var('monthnum') );
  23. } else if ( is_year() ) {
  24. $link = get_year_link( get_query_var('year') );
  25. }
  26. }
  27. }
  28. if ( $paged && $link && get_query_var('paged') > 1 ) {
  29. global $wp_rewrite;
  30. if ( !$wp_rewrite->using_permalinks() ) {
  31. $link = add_query_arg( 'paged', get_query_var('paged'), $link );
  32. } else {
  33. $link = user_trailingslashit( trailingslashit( $link ) . trailingslashit( $wp_rewrite->pagination_base ) . get_query_var('paged'), 'archive' );
  34. }
  35. }
  36. echo '<link rel="canonical" href="'.$link.'" />';
  37. }
  38. add_action('wp_head', 'cccitu_archive_link');

第二种

  1. remove_action( 'wp_head', 'rel_canonical' );
  2. function cccitu_rel_canonical() {
  3. global $post;
  4. if (is_single() || is_page()) {
  5. echo "<link rel=\"canonical\" href=\"" . get_permalink( $post->ID ) . "\" />\n";
  6. }
  7. if (is_home()) {
  8. echo "<link rel=\"canonical\" href=\"".home_url("/")."\" />\n";
  9. }
  10. if (is_category() || is_category() && is_paged()) {
  11. echo "<link rel=\"canonical\" href=\"".get_category_link(get_query_var('cat'))."\" />\n";
  12. }
  13. if (is_tag() || is_tag() && is_paged()) {
  14. echo "<link rel=\"canonical\" href=\"".get_term_link(get_query_var('tag'), 'post_tag')."\" />\n";
  15. }
  16. if (is_search() || is_search() && is_paged()) {
  17. echo "<link rel=\"canonical\" href=\"".get_search_link(get_query_var('search'))."\" />\n";
  18. }
  19. if (is_author()) {
  20. echo "<link rel=\"canonical\" href=\"".get_option('home')."\" />\n";
  21. }
  22. if (is_date()) {
  23. echo "<link rel=\"canonical\" href=\"".get_option('home')."\" />\n";
  24. }
  25. }
  26. add_action('wp_head', 'cccitu_rel_canonical');

反正是一个意思

相关推荐

WordPress屏蔽英文评论

不想直接关闭评论功能,但是又不想每次都看到一大堆的英文评论,WordPress屏蔽英文评论 //防国外灌水 function scp_com ...

暂无评论

发表评论

您的电子邮件地址不会被公开,必填项已用*标注。