0
votes

i make a wordpress advance search with jquery. when user click on search button, by using jquery i get all my search parameters and send with $.post() function to admin-ajax.php ... i create a function in functions.php file and receive all search values and write wp_query to fetch wp posts.

$args = array(
'category_name' => '3d');$query = new WP_Query($args);

if ($query->have_posts()) {
while ($query->have_posts()) {
    $query->the_post();
    echo '<a href="'. the_permalink() .'">test</a>';
}
wp_reset_postdata();

}

in the above code when i printed a tag, output is something like this:

http://localhost/site/%d8%af%d8%a7%d9%86%d9%84%d9%88%d8%af-%d9%81%db%8c%d9%84%d9%85-zootopia/test0

problem 1: test is a link but doesn't have any href! the href of link printed outside of link like a string! problem 2: "0" character automatically printed after all output values even empty echo in php will print "0" character! please help me, thanks...

1

1 Answers

0
votes

You need to replace code with this. Because you are using the_permalink() instead of get_the_permalink() when inside echo.

https://developer.wordpress.org/reference/functions/get_the_permalink/ https://developer.wordpress.org/reference/functions/the_permalink/

$args = array('category_name' => '3d');
$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        echo '<a href="'. get_the_permalink() .'">test</a>';
    }
    wp_reset_postdata();
}