我正在使用wordpress网站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>
再次感谢您的任何见解!