A
A
Anton Belinsky2021-07-22 15:44:42
PHP
Anton Belinsky, 2021-07-22 15:44:42

How to add a canonical to a pagination page?

I can’t prescribe a canonical for a pagination page from the admin panel, but I found a script in the footer.php file after an old developer that prescribes a canonical for pages, I can’t understand why it doesn’t work for this one. Perhaps the code needs to be improved, but there is no particular understanding of how. Screenshots and sample code below:60f9676a4931c522290175.png

<?
 if(is_paged()):?>
    <script>
    let myString = window.location.href;
    let myRegexp = /(.*\/)([^/]*)(\/[^/]*\/)$/g;
    let match = myRegexp.exec(myString);
    const canonical = document.querySelector('link[rel="canonical"]')
    canonical.href = match[1];
    <?php endif;?>
   

    </script>
    <?php
if( get_query_var('paged') < 2 ){ ?>
<script>
    const canonical = document.querySelector('link[rel="canonical"]')

    canonical.href = document.location.href
</script>
<?
}?>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question