如何使用jQuery动态添加帖子缩略图?

时间:2017-03-21 作者:FRQ6692

在我的wordpress网站中,我试图添加post_thumbnail 至divid="post-cont" 动态使用jquery。成功添加后post_titel.

Jquery:

<script>
$(function(){
 $(\'body\').on(\'click\', \'.post-link\', function(){
  var names = [\'aa\', \'ab\', \'ac\', \'ad\'];
  $(\'#post-cont\').fadeIn();
  var post_title = $(this).closest(\'div\').find(\'a\').text();
  var rel = $(this).attr(\'rel\');
  var post_thumb = jQuery(this).append(\'<img src="<?php echo wp_get_attachment_image_url(get_post_thumbnail_id()); ?>" />\');

  for (var i=0; i<names.length; i++) {
    if ($(\'#post-cont\').find(\'input[name=\' + names[i] + \']\').length < 1) {
      $(\'#post-cont\').append( \'<div class="rows"><input name="\' + names[i] + \'" value="\'+rel+\'">\'+post_thumb+\' \'+post_title+\'-- <a href="#" class="close">REMOVE</a></input></div>\' );   
  break;
 }
}
});
$(\'body\').on(\'click\', \'.close\', function() {
  $(this).closest(\'.rows\').remove();
});

 $(\'.close\').click(function(){
 $(\'#post-cont\').fadeOut();
 });
});
</script>

index.php

<?php 
 if( have_posts() ):             
 while( have_posts() ): the_post();                 
   <?php the_post_thumbnail (\'large\', array(\'class\' => \'img-responsive\'));?>
   <button  class="post-link" rel="<?php the_ID(); ?>"> ADD </button>
endwhile;
endif; 
wp_reset_query();
?>

<div id="post-cont"> ...... </div>
我需要把两者都加上post_titlepost_thumb 带按钮class="post-link" .

How can i add post_thumbnail dynamically?

非常感谢您的帮助

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

js(编辑4)

    $(function() {
        $(\'body\').on(\'click\', \'.post-link\', function() {
        // Jquery
        var ajax = {};
        ajax.id= $(this).attr(\'rel\');
        var ajaxurl = \'/wp-admin/admin-ajax.php\';
        jQuery.post(
        ajaxurl,
        {
        \'action\': \'get_img_post_and_title\',
        \'data\': ajax
        },
        function(response){
        // add html
        if(response.success){
        $(\'#post-cont\').append("<li>ID: "+response.data.post_id+" "+response.data.post_title+" <img src=\'"+response.data.post_thumb+"\'/> --<a href=\'#\' class=\'close\'>X</a></li>");  
        }
        }
        );
    });
    });
php(编辑4)

     // php
    add_action("wp_ajax_get_img_post_and_title", "get_img_post_and_title");
         add_action("wp_ajax_nopriv_get_img_post_and_title", "get_img_post_and_title");
        function get_img_post_and_title(){
         $return = array(
        \'post_id\' => $_POST[\'data\'][\'id\'],
\'post_title\' => get_the_title($_POST[\'data\'][\'id\']),
        \'post_thumb\' => wp_get_attachment_image_url(get_post_thumbnail_id($_POST[\'data\'][\'id\']))
        );
        wp_send_json_success($return);    
       }
索引。php

    <?php 
 if( have_posts() ):             
 while( have_posts() ): the_post();                 
   <?php the_post_thumbnail (\'large\', array(\'class\' => \'img-responsive\'));?>
   <button  class="post-link" rel="<?php the_ID(); ?>"> ADD </button>
endwhile;
endif; 
wp_reset_query();
?>

<div id="post-cont"> ...... </div>

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请