如何将自定义html添加到ajax加载更多(wordpress)

时间:2016-09-08 18:39:00

标签: javascript php ajax wordpress

我添加了一个"加载更多"按照这个帖子发送按钮与ajax ...

Load More Posts Ajax Button in Wordpress

但是,我需要为内容添加一些自定义HTML。我尝试制作一个模板并将其添加到我的ajax调用中,但我无法使其工作,因为整个模板会发布5次(我将帖子递增5)。我的问题是,如何在PHP中为ajax对象构建HTML?

我的ajax功能在这里(在functions.php中):

function more_post_ajax(){

$ppp = (isset($_POST["ppp"])) ? $_POST["ppp"] : 5;
$page = (isset($_POST['pageNumber'])) ? $_POST['pageNumber'] : 0;

header("Content-Type: text/html");

    $myposts = get_posts( $args );

    $postcounter = 1;
    $mediumcounter = 0;
    $smallcounter = 0;

$args = array(
    'suppress_filters' => true,
    'post_type' => 'post',
    'posts_per_page' => $ppp,
   // 'cat' => 8,
    'paged'    => $page,

);

$loop = new WP_Query($args);

$out = '';

if ($loop -> have_posts()) :  while ($loop -> have_posts()) : $loop -> the_post();
    /******I NEED TO CHANGE THE BELOW*******/
     $out .= '<div class="parent">
            <h1>play ball '.get_the_title().'</h1>
            <p>'.get_the_content().'</p>

     </div>';


endwhile;
endif;
wp_reset_postdata();
die($out);

我试图加载的模板文件看起来像这样......

<?php

    $args = array(
      'posts_per_page' => 5
    );

    $myposts = get_posts( $args );

    $postcounter = 1;
    $mediumcounter = 0;
    $smallcounter = 0;

    foreach ( $myposts as $post ) : setup_postdata( $post );


    if($postcounter == 1 ){

            echo '<div class="parent">';
            echo '<div class="single postwrapper child-2 postwrapper'.$postcounter.' mediumpost gray">';
    }
    else if($postcounter == 2 ){

        echo '<div class="multi postwrapper child-2 ">';
        echo '<div class="parent">';    


    }else{
        $smallcounter = $smallcounter + 1;
        if(in_array($smallcounter, array(1,2))){
            $postcolor = 'blue';
        }else{
            $postcolor = 'gray';
        }
    }
    echo '<div class="postwrapper child-2 postwrapper'.$postcounter.' smallpost '. $postcolor .'">';

      get_template_part( 'homenew', get_post_format() );

      echo '</div>';  

       if($postcounter == 1 || $postcounter == 5 ){
           echo '</div>';
           if($postcounter == 5){
             echo '</div>';  
           }
       }
        $postcounter = $postcounter + 1;

    endforeach;
    wp_reset_postdata();?>

我是否必须在ajax函数中编写模板文件?或者只是将帖子信息发送到javascript并使用jquery附加自定义js?

0 个答案:

没有答案