【发布时间】:2018-08-22 08:59:03
【问题描述】:
我不知道如何解决这个问题。在 header.php 我有这个 javascript
<script language="javascript">
$(document).ready(function() {
$(".catfilter").click(function(){
$("#category-post-content").addClass("shownewsajax");
$(".sasasa").addClass("rrrrrrrrrrrr");
});
});
</script>
它非常适用于#category-post-content,但由于“sasasa”元素在functions.php中(在add_action函数中),“rrrrrrrr”类不会在.catfilter点击时添加。
即使需要该类的元素在functions.php中,关于如何使其工作的任何想法?
这是functions.php中的代码,其中.sasasa是
///Ajax post filtering by category
add_action( 'wp_ajax_load-filter', 'prefix_load_cat_posts' );
function prefix_load_cat_posts () { ?>
<div class="allposts row blogrow">
<?php ob_start ();
$query = new WP_Query(
$args = array (
'showposts' => 999,
'cat' => $cat_id = $_POST[ 'cat' ]
) );
$posts = get_posts( $args );
foreach ( $posts as $post ) {
setup_postdata( $post );
?>
<div class="sasasa news-wrap">
<div class="newsflex">
<div class="postfeat"> <?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID ), 'my-custom-thumb' ); ?>
<img src="<?php echo $image[0]; ?>">
<?php endif; ?>
</div>
<div class="postinfo">
<div class="postcat">
<?php foreach( (get_the_category($post)) as $category) { ?> <?php echo $category->cat_name; ?><?php } ?>
</div>
<h4 class="newstitle"><?php echo $post->post_title; ?></h4>
<div class="postexc"><?php $excerpt = get_the_excerpt($post); echo $excerpt;?></div>
<a href="<?php echo get_permalink; ?>">READ MORE <span class="rightarrow"></span></a>
</div>
</div>
</div>
<?php
} wp_reset_postdata(); ?>
</div>
<div class="next-button"><a href="#" id="loadMoreCat">Load More Posts CAT</a></div>
<? $response = ob_get_contents();
ob_end_clean();
echo $response;
die(1);
}
【问题讨论】:
-
你应该用
$(document).ready(function() {}):包装你的javascript -
你能给出设置 .sasasa 项的 HTML 代码吗?
-
另外,我们不知道您如何包含“functions.php” - 因为这不是 PHP 问题,请单击
<>并将 HTML 和脚本发布到 minimal reproducible example -
Inazo 我加了代码,你能看一下吗?
标签: javascript php jquery wordpress