【问题标题】:Always get same post (the first)总是得到相同的帖子(第一个)
【发布时间】:2016-03-11 17:42:36
【问题描述】:

我总是收到相同的帖子。我不明白为什么我总是收到相同的帖子(第一个)而不是所有帖子。有人吗?

我怎样才能得到第二个或第三个等?

function my_lateral_fluid() {

$data = array();

$args = array(   
        "post_type" => "portfolio",
        "posts_per_page" => -1  
);  

$portfolio_query = new WP_Query($args);                 

while($portfolio_query -> have_posts() ){
    $portfolio_query -> the_post();

    $data = '
        <div id="all-content" class="container abs-position">
            <div id="primary" class="content-area-ajax text-center"> 
                <main id="main" class="site-main" role="main">              
                    <article id="post-'. get_the_ID().'"'. post_class().'>
                        <h1 class="entry-title">'.get_the_title().'</h1>
                        <div class="entry-content">'.get_the_content().'</div>
                    </article>
                </main>
            </div>     
        </div>  
        ';

}
wp_reset_postdata();


echo '<div id="postdata">'.$data.'</div>';

wp_die();
}

这里是lateral-fluid.js

var origHeight= 0;
jQuery(function($){

    jQuery('.more-info').click(function() {
        origHeight =   jQuery('#show').height();  
        //var postid = $(this).attr('data-postid');
        var page = jQuery(this).attr('data-href'); 

        jQuery.ajax({
                    url: ajaxFluid.ajaxurl,
                    type: 'get',
                    page: page,   
                    data: {
                        action: 'lateral_fluid'      
                    },

                    success: function( data ) {
                        console.log(data);
                        var $response   =   $(data);
                        var postdata    =   $response.filter('#postdata').html();
                        //ajax load data
                        jQuery('#ajax-inserted1').html(postdata);
                        //animate portfolio grid to left
                        jQuery('#remove').removeClass('to-screen').addClass('to-left');       
                    },
                    complete: function(){
                        //animate post to screen from right to left    
                        jQuery('.entry-content a img, .entry-content img').load(function () { 
                            jQuery('#ajax-inserted1, .controlls').removeClass('to-right').addClass('to-screen');
                            //HEIGHT CONTENT CALCULATION
                            var newHeight = jQuery('#ajax-inserted1 #primary').height()+100;
                            var el = jQuery('.show-container');
                            el.css({'height': newHeight + 'px'});
                            //STELLAR REFRESH
                            setTimeout(stellarRefresh, 500);
                            function stellarRefresh() {      
                                jQuery.stellar('refresh');
                            }
                        });   
                    }
            });

    });
    jQuery('#close-portfolio').click(function() {

        jQuery('#ajax-inserted1').removeClass('to-screen').addClass('to-right');
        jQuery('#remove').removeClass('to-left').addClass('to-screen');
        jQuery('.controlls').removeClass('to-screen').addClass('to-right');


        //ORIGINAL HEIGHT
        jQuery('.show-container').css({'height': origHeight + 'px'});


        setTimeout(ajaxEmpty, 500);
        function ajaxEmpty() {      
            jQuery('#ajax-inserted1').empty();
        }

    });

});

【问题讨论】:

  • 在代码中看起来没有问题......你在哪里调用你的函数 my_lateral_fluid()?你能把代码吗?
  • @PHPWeblineindia 代码有问题 ;-)
  • 您将$data 定义为一个数组,然后用一个字符串填充它。那么您希望$data 是什么?数组还是字符串?如果它是一个字符串,则将其定义为 $data = '';,然后在您的 while 中执行:$data . = '.. html here ..';
  • 这看起来像一个你想在 ajax 上调用的函数。如果是这样,请发布更多详细信息,您的原始 php 文件,您的 ajax 调用等...
  • @dingo_d 我用 javascript 代码更新了这个问题。抱歉耽误我睡觉了。

标签: php ajax wordpress custom-post-type


【解决方案1】:

have_posts() 循环中,您每次都覆盖$data,然后仅在循环完成后输出它,仅显示循环中的最后一篇文章(最新文章)。

将每个帖子附加到 $data(使用 $data .= &lt;html code&gt;)或将 echo '&lt;div id="postdata"&gt;'.$data.'&lt;/div&gt;'; 移动到 while 循环内。

【讨论】:

    【解决方案2】:

    您好,您使用的是普通变量。 当循环继续时,旧数据将被您的新帖子替换。 所以要么你可以做

    $data='';
    while($portfolio_query -> have_posts() ){
     $data.= '
            <div id="all-content" class="container abs-position">
    

    其余代码将相同。

    【讨论】:

      【解决方案3】:

      试试这个:

      function my_lateral_fluid() {
      
      $data = '';
      
      $args = array(   
              "post_type" => "portfolio",
              "posts_per_page" => -1  
      );  
      
      $portfolio_query = new WP_Query($args);                 
      
      if ( $portfolio_query -> have_posts() ) {
          while( $portfolio_query -> have_posts() ){
              $portfolio_query -> the_post();
      
              $data .= '
                  <div id="all-content" class="container abs-position">
                      <div id="primary" class="content-area-ajax text-center"> 
                          <main id="main" class="site-main" role="main">              
                              <article id="post-'. get_the_ID().' " '. post_class().'>
                                  <h1 class="entry-title">'.get_the_title().'</h1>
                                  <div class="entry-content">'.get_the_content().'</div>
                              </article>
                          </main>
                      </div>     
                  </div>  
                  ';
      
          }
      }
      wp_reset_postdata();
      
      die($data);
      }
      

      并且在您的 ajax 操作中使用 my_lateral_fluid 而不是 lateral_fluid。此外,由于您已经定义了jQuery(function($){...});,因此您可以在代码中的任何地方使用$ 而不是jQuery

      在您的 ajax 调用中,您希望 append&lt;div id="postdata"&gt;&lt;/div&gt;; 中的数据。

      【讨论】:

        猜你喜欢
        • 2013-10-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-14
        • 1970-01-01
        相关资源
        最近更新 更多