живой поиск xml файл в теме WordPress - PullRequest
0 голосов
/ 26 января 2020

Я пытаюсь интегрировать опцию живого поиска в WordPress CMS, просто пытаюсь найти XML файл со ссылками и именами:

<pages>
<link>
<title>John Freedy</title>
<url>/profile.php?=freedy</url>
</link>
<link>
<title>mayson nador</title>
<url>/profile.php?=nador</url>
</link>
<link>
<title>jack sage</title>
<url>/profile.php?=sage</url>
</link>
</pages>

Я создал шаблон страницы, который я назвал livesearch . php с php script:

<?php

/*
Template Name: Search Page
*/

get_header();

$xmlDoc = new DOMDocument();
$xmlDoc->load("/wp-includes/links.xml");

$x=$xmlDoc->getElementsByTagName('link');

//Get the q parameter from URL

$q=$_GET["q"];

//Lookup all links from the xml file if length of q>0

if(strlen($q)>0){
    $hint="";
    for($i=0; $i<($x->length); $i++){
        $y=$x->item($i)->getElementsByTagName('title');
        $z=$x->item($i)->getElementsByTagName('url');
        if($y->item(0)->nodeType==1){

            //find a link matching the search text
            if(stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)){

                if($hint==""){

                    $hint="<a href='" .

                    $z->item(0)->childNodes->item(0)->nodeValue.
                    "' target='_blank'>" .

                    $y->item(0)->childNodes->item(0)->nodeValue. "</a>";
                }
                else
                {
                    $hint=$hint . "<br/><a href='" .

                    $z->item(0)->childNodes->item(0)->nodeValue .
                    "' target='_blank'>".

                    $y->item(0)->childNodes->item(0)->nodeValue. "</a>";
                }
            }
        }
    }
}

// Set Output to "No results found" if no hint was found or to the correct values

if($hint==""){
    $response = "No results found";
}
else
{
    $response=$hint;
}

//output the response
echo $response;

get_footer();           
?>

и я добавил пользовательские функции js к . php файл, как показано ниже:

function my_custom_scripts() {
    wp_enqueue_script( 'custom-js', get_stylesheet_directory_uri() . '/js/mainsearch.js', array( 'jquery' ),'',true );
}
add_action( 'wp_enqueue_scripts', 'my_custom_scripts' );

mainsearch. js код:

function showResult(str){
    if(str.length==0){
        document.getElementById("livesearch").innerHTML="";
        document.getElementById("livesearch").style.border="0px";
        return;
    }
    if(window.XMLHttpRequest){
        // Code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {//Code for IE6, IE5
    xmlhttp=new ActiveXobject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readyState==4 && xmlhttp.status==200){
            document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
            document.getElementById("livesearch").style.border="1px solid #A5ACB2";
        }
    }
    xmlhttp.open("GET", "livesearch.php?q="+str,true);
    xmlhttp.send();
}

, и я создал страницу в WordPress с шаблоном 'livesearch. php ' я создал раньше, вот HTML страницы:

<form>
<h1 style="text-align: center;">Search</h1>
<p style="text-align: center;"><input size="50" type="text" /></p>

<div id="livesearch" style="text-align: center;"></div>
</form>

, когда я пытаюсь получить доступ к этой странице, он дает мне:

Нет результатов

...