将值从unctions.php传递到模板

时间:2017-10-02 作者:jordan

因此,我很难解决这个问题,也许有人会为我找到解决方案,我通过ajax点击加载更多帖子,我想在没有更多帖子可加载时隐藏“加载更多”按钮。我知道要实现这一点,需要比较哪些值,问题是它们在我的函数中。php文件和按钮位于我的模板文件中。如何将这些值传递给模板?提前感谢!

功能。php

function get_publications() {

    if ( isset($_REQUEST) ) :

            // global $maxpost,$mypage;
    $args = array( \'post_type\' => \'publications\', \'posts_per_page\' => 2, \'offset\' => 2 );
    $loop = new WP_Query( $args );

            $mypage = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
            $maxpost = $loop->max_num_pages;

    while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <section class="col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2  publications-section tagged-posts post">
      <?php if ( has_post_thumbnail() ) : ?>
      <a href="<?php echo get_permalink( $post->ID ); ?>">
        <div class="col-sm-4 col-sm-push-8 publications-section-image" style="background-image:url(\'<?php the_post_thumbnail_url(); ?>\');"></div>
      </a>
      <?php endif; ?>
      <div class="col-sm-8 col-sm-pull-4">
       <h2><a href="<?php echo get_permalink( $post->ID ); ?>"><?php the_title(); ?></a></h2>
       <p><?php the_excerpt(); ?></p>
       <small><?php echo get_post_meta($post->ID, "_contact", true); ?></small>
       <small class="pub-tags loaded"> <span><?php the_terms( $post->ID,\'mytag\',\'#\',\' #\'); ?></span></small>
     </div>
    </section>
    <?php echo ++$mypage; echo $maxpost; endwhile;
endif;
die();
}
 add_action( \'wp_ajax_get_publications\', \'get_publications\' );
 add_action( \'wp_ajax_nopriv_get_publications\', \'get_publications\' );
加载。js公司

$(document).ready(function(){
$(\'#loadmore\').click(function(event) {
if (event.preventDefault) {
    event.preventDefault();
} else {
    event.returnValue = false;
}

  $.ajax({
      url: ajaxurl,
      data: {
          \'action\' : \'get_publications\',
      },
      success:function(data) {
        $(\'#posts section:last-child\').after(data);

        // console.log(max_post_vars.posts);
      }
  });
  console.log(\'loadmore clicked\');
  return false;
  });

  });
模板文件

$args = array(
          \'post_type\' => \'publications\',
          \'posts_per_page\' => 2,
      );

    $query = new WP_Query( $args ); ?>

    <?php if ($query->have_posts()) : ?>
      <div id="posts">
        <?php while ($query->have_posts()) : $query->the_post(); ?>
          <section class="col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2  publications-section tagged-posts post">
            <?php if ( has_post_thumbnail() ) : ?>
            <a href="<?php echo get_permalink( $post->ID ); ?>">
              <div class="col-sm-4 col-sm-push-8 publications-section-image" style="background-image:url(\'<?php the_post_thumbnail_url(); ?>\');"></div>
            </a>
            <?php endif; ?>
            <div class="col-sm-8 col-sm-pull-4">
             <h2><a href="<?php echo get_permalink( $post->ID ); ?>"><?php the_title(); ?></a></h2>
             <p><?php the_excerpt(); ?></p>
             <small><?php echo get_post_meta($post->ID, "_contact", true); ?></small>
             <small class="pub-tags pub"> <span><?php the_terms( $post->ID,\'mytag\',\'\',\' \'); ?></span></small>
           </div>
          </section>
        <?php echo $mypage; endwhile; wp_reset_postdata();  ?>
      </div>

      <div class="col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2" id="pub-btn-section">
        <input type="submit" class="btn" id="loadmore" name="" value="<?php the_field(\'load_more\',pll_current_language(\'slug\')); ?>">
      </div>
    <?php endif;  ?>

1 个回复
最合适的回答,由SO网友:ngearing 整理而成

您可以让get\\u publications函数返回包含所需各种信息的JSON对象。

e、 g级

function get_publications() {
    // Your query here.

    $data = [
        \'html\' => $your_html,
        \'found_posts\' => $query->found_posts,
        \'current_post\' => $query->current_post,
        \'current_page\' => $page,
        etc...
    ];

    echo json_encode($data);

}
然后用javascript解析数据。

结束

相关推荐

开发插件以添加像JqueryUI这样的组件

我正在开发WordPress的插件,我需要在管理页面上添加一些组件http://jqueryui.com/ 我需要添加,模态窗口,手风琴,进度条和滑块。我想知道为什么我对在WordPress中开发插件很陌生:WordPress集成了jQuery UI</在的官方页面https://codex.wordpress.org 我是否有类似的选项https://jqueryui.com/ 是否有要复制和粘贴的加载项我可以将jQuery UI之类的库添加到插件中,而不会在WordPress中产生任何问题,谢谢