0

我有一个带有多种内容类型(默认帖子类型除外)的在线商店(http://themeforest.net/item/mayashop-a-flexible-responsive-ecommerce-theme/2189918 )的自定义主题,我需要将某种投票系统添加到这种非默认内容类型之一(特别是产品内容类型,以便用户可以对他们喜欢的我的商店的产品进行投票)。

有没有提供这种功能的插件?

谢谢

4

1 回答 1

3

有很多插件可以做到这一点,你可以用谷歌搜索它们,但是如果你想知道如何在自定义字段的帮助下轻松(并且原始地)完成它,那么你就可以了:

add_action( 'wp_ajax_nopriv_o99__action', 'o99__ajax_cb' );
add_action( 'wp_ajax_o99__action', 'o99__ajax_cb' );

// this fanction is the ajax callback.
function o99__ajax_cb(){

    // Verify the nonce to make sure the request is ours and legael...

    if( !isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'o99__nonce' ) ) die( '-1' );

    $post_id = isset( $_REQUEST['post_id'] ) ? absint( $_REQUEST['post_id'] ) : 0;

    if( ! $post_id ) die( '-1' );

    $counter = get_post_meta( $post_id, 'o99_good_post', true );
    $counter = absint( $counter );
    $cookie = 'o99_post_vote' .$post_id;

    if( $counter){

        if (!isset($_COOKIE[$cookie]) ) {
            $counter++;
        }
        else {$counter=$counter;} 
    }
    else{
        $counter = mt_rand(20,150);
    }

    update_post_meta( $post_id, 'o99_good_post', $counter ); //hidden field
    echo $counter; 
    die();
}


function o99__good_bad(){
    global $post;
    // send a simple cookie
    $cookie = 'o99_post_vote' .$post_id;
    $count = get_post_meta( $post->ID, 'o99_good_post', true ); //hidden field

    if (!$count ){
        $count = '0';
    }

    $icon = '<img src="';
    $icon .= get_bloginfo('template_url') ; // better using get_stylesheet_uri() now 
    if (!isset($_COOKIE[$cookie]) ) {
        $icon .= '/images/dl-star16.png"/>'; // set up your icons according to case
    }
    else {
        $icon .= '/images/dl-v16.png"/>';
    } 

    ?>
    <span id="o99__click" title="Click Here to Vote this Post up"><?php echo $icon; ?> click here to vote up
    This post was voted <span id="o99__count"><?php echo strip_tags( $count );?>
    </span> times</span>
    <?php
}

// just injecting the JS into head --
add_action( 'wp_head', 'o99__head' );

function o99__head()
{
    if( ! is_singular() ) return;
    $post_id = get_queried_object_id();
    ?>
    <script type="text/javascript">
        var o99__data = {
            action: 'o99__action',
            post_id: '<?php echo absint( $post_id ); ?>',
            nonce: '<?php echo wp_create_nonce( 'o99__nonce' ); ?>',
        }

        jQuery(document).ready(function(){
            jQuery( '#o99__click' ).click(function(){

                jQuery.get( 
                    '<?php echo site_url( 'wp-admin/admin-ajax.php' ); ?>', 
                    o99__data, 
                    function( data ){
                    if(jQuery.cookie('o99_post_vote<?php echo absint( $post_id ); ?>') != null) {
                             alert( 'You can only vote once per post!' );
                                    };
                    if(jQuery.cookie('o99_post_vote<?php echo absint( $post_id ); ?>') == null) {   
                        if( '-1' != data )
                        {
                            jQuery( 'span#o99__count' ).html( data );
                            jQuery.cookie('o99_post_vote<?php echo absint( $post_id ); ?>', 'voted', { expires: 7 });
                             alert( 'your vote was accepted!' );
                             };
                        }
                    }
                );
            });
        });
    </script>
    <?php
}
?>

这是一个很老的功能,很久以前就被黑了,它应该仍然可以工作,但可能需要一点润色..

编辑我 更复杂的插件的一些例子:

http://wordpress.org/extend/plugins/post-ratings/ http://wordpress.org/extend/plugins/post-ratings/screenshots/ http://wordpress.org/extend/plugins/gd-star-评级/ 如果您搜索法典,您会发现更多..

上面列出的那些支持自定义帖子类型。

于 2013-05-06T10:06:53.747 回答