• Jan 11, 2023

If you want to add a rel='canonical' attribute to the pagination links generated by the wp_pagenavi plugin, you can use the str_replace() function to replace the <a> tag with a new <a> tag that includes the rel='canonical'attribute.

Here’s an example of how you can do this:

function custom_wp_pagenavi( $html ) {
    $html = str_replace( '<a ', '<a rel="canonical" ', $html );
    return $html;
add_filter( 'wp_pagenavi', 'custom_wp_pagenavi' );

This code will replace all the '<a ' in the $html output, with '<a rel="canonical" ' adding the rel attribute, it will add the attribute to all the anchor tags in pagination links

It is important to note that this will only affect the pagination links generated by the wp_pagenavi plugin, not the main URL of the page.

The "rel=canonical" link element is used to tell search engines which version of the content is the original one. It is used to avoid duplicate content, for example, when you have a category page, pagination pages and search pages that have the same content.

Share on:
  • Dec 28, 2022

To add HTML code after the <body> tag in WordPress using the functions.php file, you can use the wp_body_open action hook. This hook is triggered after the <body> tag is opened in the document, and it allows you to insert any content you want at this point in the page.

Here’s an example of how you can use the wp_body_open action hook to add HTML code after the <body> tag:

function my_custom_code() {
  echo '<div>My custom HTML code goes here</div>';
add_action( 'wp_body_open', 'my_custom_code' );

In this example, the my_custom_code() function contains the HTML code that you want to insert. This function is then attached to the wp_body_open action hook using the add_action() function. When the hook is triggered, the HTML code in the my_custom_code() function will be inserted into the page.

Keep in mind that you will need to add this code to your theme’s functions.php file or a custom plugin in order for it to work.

Share on:
  • Nov 11, 2022

This code will help you to customize WP-PageNavi pagination html structure.

Example -1: Using this you can wrap a and span with li

add_filter( 'wp_pagenavi', 'ap_pagination', 10, 2 );
// customize the PageNavi HTML before it is output
function ap_pagination($html) {
	$out = '';
	//wrap a's and span's in li's
	$out = str_replace("<a","<li><a",$html);    
	$out = str_replace("</a>","</a></li>",$out);
	$out = str_replace("<span","<li><a",$out);   
	$out = str_replace("</span>","</a></li>",$out);
	$out = str_replace("<div class='wp-pagenavi'>","",$out);
	$out = str_replace("</div>","",$out);
	return '<ul>'.$out.'</ul> ';

Example 2: Add extra attribute to pagination link

add_filter('wp_pagenavi', 'ap_pagination', 10, 2);
function ap_pagination($html)
  $out = '';
  $out = str_replace("<a", "<a rel='canonical' ", $html);

  return  $out;
Share on: