0

我正在开发一个 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; ?>&amp;w=220&amp;h=130&amp;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>

再次感谢您的任何见解!

4

0 回答 0