我正在尝试设置一个WordPress模板,该模板将扫描网站上的帖子,并显示共享的数量。
我使用AddThis,我已经注释了我的ID。
我似乎无法从对象中提取社交计数。
<?php
get_header(); ?>
<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js"></script>
<script>
var addthis_config = {
pubid: 'ra-4f563d934bfbf729'
}
</script>
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
echo "<table class='table'>";
echo "<tr>";
echo "<th>Title</th>";
echo "<th>Facebook</th>";
echo "<th>Twitter</th>";
echo "<th>Reddit</th>";
echo "<th>Delicious</th>";
echo "<th>Linkedin</th>";
echo "</tr>";
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<tr class="id-<?php echo $post->ID; ?>">
<td><?php the_title(); ?></td>
<td class="facebook">
<a class="addthis_counter_facebook"></a>
</td>
<td class="twitter">
<script>
addthis.sharecounters.getShareCounts({service: ['twitter'], countUrl: '<?php the_permalink(); ?>'}, function(obj) {
console.log(obj.count);
});
</script>
</td>
<td class="reddit">
<script>
addthis.sharecounters.getShareCounts({service: ['reddit'], countUrl: '<?php the_permalink(); ?>'}, function(obj) {
console.log(obj)
});
</script>
</td>
<td class="delicious">
<script>
addthis.sharecounters.getShareCounts({service: ['delicious'], countUrl: '<?php the_permalink(); ?>'}, function(obj) {
console.log(obj)
});
</script>
</td>
<td class="linkedin">
<script>
addthis.sharecounters.getShareCounts({service: ['linkedin'], countUrl: '<?php the_permalink(); ?>'}, function(obj) {
console.log(obj)
});
</script>
</td>
</tr>
<?php
endwhile; wp_reset_query(); wp_reset_postdata();
echo "</table>";
endif;
get_footer();
?>
答案 0 :(得分:0)
以下是我使用的解决方案......
<script>
addthis.sharecounters.getShareCounts({service: ['twitter'], countUrl: '<?php the_permalink(); ?>'}, function(obj) {
$count = obj[0].count;
$('tr.id-<?php echo $post->ID; ?> .twitter').text( $count );
});
</script>
<td class="twitter"></td>