Javascript 使用AJAX的Wordpress动态帖子
我想使用内部UL中单击的项目的post ID加载此代码。如何做到这一点 当前创建包含指定类别的侧菜单 之后,它在中创建一个ul子菜单,每个li都是该类别中的一个帖子。类别中的帖子也使用ajax插入到指定的div中 我需要在div和side菜单中显示的帖子在单击时也加载到div中 目前我正在使用cat ID创建菜单并插入到div中。我需要从子菜单使用post ID 这很难解释 index.phpJavascript 使用AJAX的Wordpress动态帖子,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,我想使用内部UL中单击的项目的post ID加载此代码。如何做到这一点 当前创建包含指定类别的侧菜单 之后,它在中创建一个ul子菜单,每个li都是该类别中的一个帖子。类别中的帖子也使用ajax插入到指定的div中 我需要在div和side菜单中显示的帖子在单击时也加载到div中 目前我正在使用cat ID创建菜单并插入到div中。我需要从子菜单使用post ID 这很难解释 index.php <?php $args = array( 'include' => '4,5
<?php
$args = array(
'include' => '4,5,7,8'
);
$categories = get_categories($args);
?>
<ul id="category-menu" class="nav nav-list">
<?php
foreach ( $categories as $cat ) { ?>
<?php $show = 'cat=' . $cat->term_id; ?>
<li id="cat-<?php echo $cat->term_id; ?>">
<a class="<?php echo $cat->slug; ?> ajax tree-toggle" onclick="cat_ajax_get('<?php echo $cat->term_id; ?>');" href="#">
<?php echo $cat->name; ?>
</a>
<ul class="nav nav-list tree" style="display: none">
<?php query_posts($show); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php $post_id = $wp_query->post->ID; echo $post_id; ?>
<li>
<a class="ajax" onclick="cat_ajax_get('<?php echo $post_id ?>');" href="#"><?php the_title(); ?></a>
</li>
<?php endwhile; endif; ?>
</ul>
</li>
<script>
function cat_ajax_get(catID) {
jQuery("a.ajax").removeClass("current");
jQuery("a.ajax").addClass("current"); //adds class current to the category menu item being displayed so you can style it with css
var ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); //must echo it ?';
jQuery.ajax({
type: 'POST',
url: ajaxurl,
data: {"action": "load-filter", cat: catID },
success: function(response) {
jQuery("#category-post-content").html(response);
return false;
}
});
}
</script>
add_action( 'wp_ajax_nopriv_load-filter', 'prefix_load_cat_posts' );
add_action( 'wp_ajax_load-filter', 'prefix_load_cat_posts' );
function prefix_load_cat_posts () {
$cat_id = $_POST[ 'cat' ]; $args = array (
'cat' => $cat_id,
'posts_per_page' => 10,
'order' => 'DESC'
);
$posts = get_posts( $args );
ob_start ();
foreach ( $posts as $post ) {
setup_postdata( $post ); ?>
<div id="post-<?php echo $post->ID; ?> <?php post_class(); ?>">
<h1 class="post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<div id="post-content">
<?php the_content(); ?>
</div>
</div>
<?php
}
wp_reset_postdata();
$response = ob_get_contents(); ob_end_clean();
echo $response;
die(1);
}
你能澄清一下“这个代码”吗?我已经补充了进一步的解释。