Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

Change Archive Title if is search WordPress Genesis

I have a site I am working on that when you do a search for a post_type, it won't change the title.  I have the following hack but it does not work because it runs before the jQuery library is included.

function searchfilter($query) {
    if ($query->is_search && !is_admin() ) {
        if(isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
                if($type == 'product') {
                    ?>
    <script>
    (function($){
        var term = '<?php the_search_query(); ?>';

        $('.archive-title').text("Your Search for: " + term + "");
        })(jQuery);
    </script> 
    <?php
                }
        }       
    }
return $query;
}
add_filter('pre_get_posts','searchfilter');

Open in new window


You can see what I mean here: https://gardenersstage.wpengine.com/?s=seeds&post_type=product
"Shop Our Store"  Should say "Your Search for: " + term + "" Or do you know how to change the title of the page if the post type is set?
Avatar of leakim971
leakim971
Flag of Guadeloupe image

<script>
function gup(name) {
  name = name.replace(/[\[]/, '\\\[').replace(/[\]]/, '\\\]');
  var v = new RegExp('[?&]'+name+'=?([^&#]*)').exec(location.href);
  return v && v[1];
}
if( gup("post_type") == "product" ) {
  document.title = gup("s");
}
</script>

Open in new window

Avatar of Robert Granlund

ASKER

@leakim Can you please explain the script you posted?  It is not at all what I was expecting and I don't quit understand what is going on.  Thank you in advance.
Please note : you need to remove what you're currently doing.

line 2 to 6 : the gup function "Get Url Parameter" get the value of a parameter name you pass to it.
line 7 : we check if the parameter "product" is present in the current URL
line 8 : if the parameter product is found, we get the value of the parameter "s" and set the title of the current page with it
OH!  Thank you for the information.  This is very helpful.  However, I have miscommunication my question.  What I meant was to Change the page_title within a specific div on the page:   The Content within the H1 tag.  If you reference the above url, you will see what I mean.
so just replace :
document.title = gup("s");
by :
document.getElementsByClassName('archive-title')[0].innerText = "Your Search for: " + gup("s");
With the following I get the error message: Uncaught TypeError: Cannot set property 'innerText' of undefined

which is:  document.getElementsByClassName('archive-title')[0].innerText = "Your Search for: " + gup("s");


function searchfilter($query) {
    if ($query->is_search && !is_admin()) {
                ?>
                <script>                  
                    function gup(name) {
                        name = name.replace(/[\[]/, '\\\[').replace(/[\]]/, '\\\]');
                        var v = new RegExp('[?&]' + name + '=?([^&#]*)').exec(location.href);
                        return v && v[1];
                    }
                    if (gup("post_type") == "product") {
                        document.getElementsByClassName('archive-title')[0].innerText = "Your Search for: " + gup("s");
                    }
                </script>
                <?php

    }
    return $query;
}

add_filter('pre_get_posts', 'searchfilter');

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial