I have in my wordpress site, inside the loop of single.php , a select tag in which the options are the posts of the current category returned via a custom query.
On changing selected option, I have many javascript functions that are working well, but the last function among them (function f_next-previous
), doesnt seem to work.
The aim of this function is to update the next and previous links without reloading the page.
The code relative to navigation links (next and previous) in my template is working well and is above:
<div id="nav-above" class="navigation">
<div class="nav-previous"><?php previous_post_link( '%link', '<img height="34" src="' . get_bloginfo("template_directory") . '/images/previous.png" />' ); ?></div>
<div class="nav-next"><?php next_post_link( '%link', '<img height="34" src="' . get_bloginfo("template_directory") . '/images/next.png" />' ); ?></div>
</div><!-- #nav-above -->
The javascript code of this function is:
function f_next-previous(id)
{
$.ajax({
cache: true,
type: "GET",
timeout: 5000,
url: 'wp-content/themes/twentyten/pages/next-previous.php?p='+id,
success: function(msg)
{
$('#nav-above').html(msg);
},
error: function(msg)
{
alert("Your browser broke!");
return false;
}
});
}
The file next-previous.php
content is :
<?php
$p=$_GET['p'];
require( '../../../wp-load.php' );
$my_query = new WP_Query();
$my_query->query(array( 'post__in' => array($p)));
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="nav-previous"><?php previous_post_link( '%link', '<img height="34" src="' . get_bloginfo("template_directory") . '/images/previous.png" />' ); ?></div>
<div class="nav-next"><?php next_post_link( '%link', '<img height="34" src="' . get_bloginfo("template_directory") . '/images/next.png" />' ); ?></div>
<?php
endwhile;
endif;
?>
While testing this php file by giving it a value to the p parameter, it gives logical result in the browser. Jquery and function scripts are well included and all AJAX in my website is ok. What am I missing in this work????
UPDATE: Note that the part of my single.php file responsible of triggering the AJAX call is:
<form method="post" action="">
<select class="select2" name="" id="" >
<option value="<?php the_ID();?>"><?php the_title();?></option>
<?php
global $post;
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$myposts = get_posts("paged=$paged&category=4");
foreach($myposts as $post) :?>
<option value="<?php the_ID();?>"><?php the_title();?></option>
<?php endforeach;
wp_reset_postdata(); ?>
</select>
</form>