私はワードプレスサイトhttp://taste.fourseasons.com/ingredients/に取り組んでいます
メイン コンテンツ セクションの下部に、さらに投稿を呼び出すためのオプションがありますが、それらが呼び出されると、ユーザーがそれらに投票できるようにするスクリプトは、新しい投稿では有効になりません。投票ボタンをクリックすると、ユーザーは <"a href="#"> があるかのようにページのトップに移動します
ロード時に投票スクリプトが開始されていると思いますが、元のドキュメントがロードされた後にロードされた投稿で投票ボタンをトリガーしていませんか?
何かご意見は?
ありがとう!
さて、これが相対コードだと思います。私はjsfiddleを設定しようとしましたが、現時点ではまだ頭の中に少し残っていると思います. 私が収集したものから、コールバック関数を作成して、新しい投稿が DOM に追加されると元の投票スクリプトがリロードされるようにする必要があります。それが理にかなっていることを願っています。意欲的なスクリプト作成者と一緒に時間を割いてくれてありがとう!
function.php から:
add_action("wp_ajax_add_votes_options", "add_votes_options");
add_action("wp_ajax_nopriv_add_votes_options", "add_votes_options");
function add_votes_options() {
$postid = $_POST['postid'];
$ip = $_POST['ip'];
if (!wp_verify_nonce($_POST['nonce'], 'voting_nonce_'.$postid))
return;
$voter_ips = get_post_meta($postid, "voter_ips", true);
if(!empty($voter_ips) && in_array($ip, $voter_ips)) {
echo "null";
die(0);
} else {
$voter_ips[] = $ip;
update_post_meta($postid, "voter_ips", $voter_ips);
}
$current_votes = get_post_meta($postid, "votes", true);
$new_votes = intval($current_votes) + 1;
update_post_meta($postid, "votes", $new_votes);
$return = $new_votes>1 ? $new_votes : $new_votes;
echo $return;
die(0);
}
そして、これがどのようにページに取り込まれているかです:
<div class="grid_row_1">
<div class="grid_col">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div class="clear"></div>
</div>
<div class="grid_row_1">
<div class="grid_col ingredients post-holder" data-direction="DESC" data-
order="date">
<?php $count = 0; ?>
<?php if (have_posts()) : ?>
<?php query_posts('posts_per_page=15&post_type=ingredient&paged='.$paged);
while ( have_posts() ) : the_post(); ?>
<?php
foreach($ingredient_images as $meta_box) {
$data = get_post_meta($post->ID, 'ingredient-images', true);
switch($meta_box['name']){
case 'ingredient_thumb': $feature_thumb = $data[ $meta_box[ 'name' ]
]; break;
case 'ingredient_status': $feature_thumb_show = $data[ $meta_box[
'name' ] ]; break;
}
}
switch($feature_thumb_show){
case 1: $type='suggested'; break;
case 2: $type='accepted'; break;
case 3: $type='featured'; break;
}
$theDate= get_the_date( 'o-n-j' );
$time = strtotime($theDate);
$one_week_ago = strtotime('-1 week');
switch($count){
case 0: echo '<div class="post one-quarter-sub-col-above-480 one-half-sub-col-at-480 first '.$type.'">'; $count++; break;
case 1: echo '<div class="post one-quarter-sub-col-above-480 one-half-sub-col-at-480 last-at-480 '.$type.'">'; $count++; break;
case 2: echo '<div class="post one-quarter-sub-col-above-480 one-half-sub-col-at-480 first-at-480 '.$type.'">'; $count++; break;
case 3: echo '<div class="post one-quarter-sub-col-above-480 one-half-sub-col-at-480 last '.$type.'">'; $count = 0; break;
}
$votes = get_post_meta($post->ID, "votes", true);
$votes = !empty($votes) ? $votes : "0";
$hasvoted = $_COOKIE['better_votes_'.$post->ID];
$hasvoted = explode(",", $hasvoted);
if(in_array($post->ID, $hasvoted)) {
$vtext = "VOTED";
$class = 'unvote-sm';
} else {
$ip = $_SERVER['REMOTE_ADDR'];
$voter_ips = get_post_meta($post->ID, "voter_ips", true);
if(!empty($voter_ips) && in_array($ip, $voter_ips)) {
$vtext = "VOTED";
$class = 'unvote-sm';
} else {
$vtext = "VOTE";
$class = 'vote-sm';
}
}
?>
<?php if(function_exists('wp_nonce_field')) wp_nonce_field('voting_nonce_'.$post->ID.'', 'voting_nonce_'.$post->ID.''); ?>
<div class="bg-white pad-lr10 pad-t10 border-lightgrey margin-b10">
<div class="photo-wrapper ratio-16-9 text-white">
<?php if($feature_thumb_show!=3): ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/images/blank_landscape.gif"></a>
<div class="image-overlay full-width full-height align-center bg-lightblue">
<table class="layout-vert-center full-width full-height">
<tr><td><a class="header font-30 tk3 leading-tight" href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></td></tr>
</table>
</div>
<?php else: ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/includes/timthumb.php?src=<?php echo $feature_thumb; ?>&w=220&h=130&a=t"></a>
<div class="image-overlay bottom bg-black bg-opaque-50 pad-5">
<a class="header font-30 tk3 leading-tight" href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</div>
<?php endif; ?>
<?php if( $time > $one_week_ago ) {?>
<div class="image-overlay"><img src="<?php bloginfo('template_directory'); ?>/images/icon_new.png"></div>
<?php } ?>
</div>
<div class="clear"></div>
<div class="font-16 leading-medium">
<div class="one-half">
<a href="#" class="vote icon-text-link disp-block border-right-lightgrey pad-tb5 pad-r5 tk3" data-post="<?php echo $post->ID ?>">
<div class="vote-text pad-t5 float-left"><?php echo $vtext; ?></div>
<div class="float-right">
<div class="vote-count float-left text-medgrey pad-t5"><?php echo $votes; ?></div>
<span class="icon-holder float-left <?php echo $class; ?>"></span>
</div>
<div class="clear"></div>
</a>
</div>
<div class="one-half">
<a href="#" class="icon-text-link disp-block pad-tb5 pad-l5 tk3">
<div class="pad-t5 float-left hide-at-768">COMMENT</div>
<div class="float-right"><div class="float-left text-medgrey pad-t5"></div><span class="icon-holder comment-sm float-left pad-t5"><?php comments_number( '0', '1', '%' ); ?></span></div>
<div class="clear"></div>
</a>
</div>
<div class="clear"></div>
</div>
</div>
</div>
<?php endwhile;?>
<?php wp_reset_query(); ?>
<?php endif; ?>
次に、新しい投稿が次のように追加されます。
<div class="grid_row_1">
<div class="grid_col">
<div class="border-top-black margin-tb20">
<div class="align-center"><a class="action-btn pad-lr50 tk3 view-more" href="#">SHOW MORE</a></div>
</div>
</div>
</div>
洞察力をありがとう!