I'm trying to change meta and title depending on the search query. My search results are starting with a hash tag, so it's seems hard to get it with php. I followed some tutorials:
/wp-content/themes/theme/assets/js/hash.js
$(document).ready(function() {
$(window).bind('hashchange', function() {
var hash = window.location.hash.substring(1);
$.get('/wp-content/themes/theme/assets/php/ajax-hash.php', { tag: hash },
function(data) { $('#tag').html(data); }
);
});
});
/wp-content/themes/theme/assets/php/ajax-hash.php
<?php
$ajaxhashurl = isset($_GET['tag']) ? $_GET['tag'] : 'none';
?>
/wp-content/themes/theme/functions.php
function hash_tracking() {
if( is_front_page() ) {
wp_enqueue_script( 'hash_tracker', get_stylesheet_directory_uri().'/assets/js/hash.js', array( 'jquery' ), '1.0.0', true );
}
}
add_action( 'wp_enqueue_scripts', 'hash_tracking' );
add_filter('wpseo_title', 'filter_search_wpseo_title');
function filter_search_wpseo_title($title) {
if( is_front_page() ) {
include '/wp-content/themes/theme/assets/php/ajax-hash.php';
if (strpos($ajaxhashurl,'"2":"200"') !== false) {
$title = 'Something';
}
return $title;
}}
This however doesn't seems to be working and also breaks my page. the page itself is here - https://sochi.asp.sale
Thanks for any help in advance!
At you callback, where you do function(data) { $('#tag').html(data); }
function(data) { $('#tag').html(data); }
change to:
function(data) {
document.getElementsByTagName('title')[0].innerHTML = data;
}
Also, there is no element with id "tag" at the page you provided.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.