In meiner Wordpress-Website erstelle ich eine Ajax Suche und suche post_title
. Der Code ist hier ...Titel dynamisch hinzufügen und entfernen
Funktion und Skript:
<?php
add_action('wp_footer', 'ajax_fetch');
function ajax_fetch() {
?>
<script type="text/javascript">
function fetch(){
jQuery.ajax({
url: ajaxwpse.ajaxurl,
type: 'post',
data: { action: 'data_fetch', keyword: jQuery('#keyword').val() },
success: function(data) {
jQuery('#datafetch').html(data);
}
});
}
</script>
<?php
}
add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
function data_fetch(){
if ( esc_attr($_POST['keyword']) == null) { die(); }
$the_query = new WP_Query(array('posts_per_page' => -1, 's' => esc_attr($_POST['keyword']), 'post_type' => array('mobile','tablet')));
if($the_query->have_posts()) :
while($the_query->have_posts()): $the_query->the_post(); ?>
<div>
<button class="post-link" rel="<?php the_ID(); ?>"> ADD </button>
<li><a href="#" id="<?php the_ID(); ?>"><?php the_title();?></a></li>
</div>
<?php endwhile;
wp_reset_postdata();
endif;
die();
}
Nach erfolgreich Jetzt mit Ajax Suche i post_title
von Suche hinzufügen müssen id="post-container"
dynamisch div.
HTML
<input type="text" name="keyword" id="keyword" onkeyup="fetch()" placeholder="Search to add"></input>
<div id="datafetch"></div> // successfuly ajax Search Result Here
<div id="post-container"> </div> // Trying to add post title here
ich eine ADD
Schaltfläche in Suchergebnis POST_TITLE in div hinzuzufügen.
Wie dynamisch post_title vom Suchfeld zu Div mit Button hinzufügen?
Statt nur den Inhalt von '# datafetch' zu ersetzen, fügen Sie warum nicht die' # post-container' div auf den erzeugten HTML und fügen Sie beide in einem übergeordneten div ? –
@ChrisG bitte posten Antwort über die Idee, es ist gut. Erkläre mit Antwort wie? – FRQ6692