In meiner WordPress-Website versuche ich, post_thumbnail
zu div id="post-cont"
dynamisch mit jquery hinzuzufügen. Nach dem erfolgreichen Hinzufügen von post_titel
.
Jquery:
$(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="" />'); for (var i=0; i<names.length; i++) { if ($('#post-cont').find('input[name=' + names[i] + ']').length < 1) { $('#post-cont').append( ''+post_thumb+' '+post_title+'-- REMOVE' ); break; } } }); $('body').on('click', '.close', function() { $(this).closest('.rows').remove(); }); $('.close').click(function(){ $('#post-cont').fadeOut(); }); });
index.php
<?php if( have_posts() ): while( have_posts() ): the_post(); 'img-responsive'));?> <button class="post-link" rel=""> ADD endwhile; endif; wp_reset_query(); ?> ......
Ich muss post_title
und post_thumb
mit Button class="post-link"
hinzufügen.
Wie kann ich post_thumbnail dynamisch hinzufügen?
Jede Hilfe wird geschätzt
js (Bearbeiten 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("ID: "+response.data.post_id+" "+response.data.post_title+"
--X "); } } ); }); });
PHP (Bearbeiten 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); }
index.php
< ?php if( have_posts() ): while( have_posts() ): the_post(); 'img-responsive'));?> endwhile; endif; wp_reset_query(); ?> ......