试图显示使用jQuery是给我一些麻烦的内容。 我创建了三个按钮,点击时,他们从我的服务器上的PHP文件加载数据。 一切正常,除了当我尝试发送加载WordPress的帖子的请求。 下面的致命错误出现:调用未定义功能get_posts()。 我学会了如何从做https://perishablepress.com/slide-fade-content/ 。
对于脚本,我有这样的:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script type="text/javascript" src="http://example.com/slide-fade-content/jquery.colorfade.min.js"></script> <script type="text/javascript"> // slide & fade content @ http://m0n.co/r $(document).ready(function() { $('.more').on('click', function(e) { e.preventDefault(); var href = $(this).attr('href'); if ($('#ajax').is(':visible')) { $('#ajax').css({ display:'block' }).animate({ height:'0' }).empty(); } $('#ajax').css({ display:'block' }).animate({ height:'200px' },function() { $('#ajax').html('<img id="loader" src="loader.gif">'); $('#loader').css({ border:'none', position:'relative', top:'24px', left:'48px', boxShadow:'none' }); $('#ajax').load('slide-fade-content.php ' + href, function() { $('#ajax').hide().fadeIn('slow').colorFade({ 'fadeColor': 'rgb(253,253,175)' }); }); }); }); }); </script>
这是我的三个按钮:
<ul> <li><a class="more" href="#first-item">First Item</a></li> <li><a class="more" href="#second-item">Second Item</a></li> <li><a class="more" href="#third-item">Third Item</a></li> </ul>
这是内容显示在哪些
<div id="ajax"></div>
这是内容
<div id="load"> <div id="first-item"> <?php $args = array( 'posts_per_page' => 5, 'offset'=> 0, 'category' => '2,3,4', 'orderby' => 'meta_value_num', 'meta_key' => 'views', 'order' => 'DESC', 'suppress_filters' => true, 'date_query' => array( 'after' => date("jS F, Y", strtotime('-24 hours')) ) ); $myposts = get_posts( $args ); foreach ( $myposts as $post ) : setup_postdata( $post ); ?> <div> <li> <ul><?php $categories = get_the_category(); $separator = ' '; $output = ''; if($categories){ foreach($categories as $category) { $output .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator; } echo trim($output, $separator) ; } ?> </ul> <a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail( $post_id, array(300, 160), $attr ); ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?> </li> </div> <?php endforeach; wp_reset_postdata();?> </div> <div id="second-item"> Test2 </div> <div id="third-item"> Test3 </div> </div>
我想我必须定义一个函数,但几个小时的反复试验后,我迷路了。 我无法弄清楚如何做到这一点或在哪里定义它。