我创建了一个插件,可以调用帖子列表并获取外部API数据。我正在使用get\\u posts获取列表(因为我需要将链接数组提供给API查询)
无论出于何种原因,分页的任何尝试要么完全失败,要么加载相同的帖子。由于我所做的一切都不起作用,所以现在我删除了所有分页。
add_action(\'admin_menu\' , \'blgs_dashboard_snap\');
function blgs_dashboard_snap() {
add_dashboard_page( \'Social Metrics\', \'Social Metrics\', \'read\', \'blgs-social-metrics-snap\', \'blgs_metrics_page\');
}
function blgs_metrics_page() {
echo \'<div class="wrap">\';
echo \'<div id="icon-blgs-metrics" class="icon32">\';
echo \'<br />\';
echo \'</div>\';
echo \'<h2>Social Metrics Dashboard</h2>\';
echo \'<div id="bg_social_page">\';
echo \'
<ul class="headers">
<li class="post_date">Publish Date</th>
<li class="post_title">Post Title</th>
<li class="social_icon social_total"><img src="\'.plugins_url( \'/img/twitter.png\', __FILE__ ).\'" title="Twitter" alt="Twitter"></li>
<li class="social_icon social_total"><img src="\'.plugins_url( \'/img/gplus.png\', __FILE__ ).\'" title="Google Plus" alt="Google Plus"></li>
<li class="social_icon social_total"><img src="\'.plugins_url( \'/img/stumble.png\', __FILE__ ).\'" title="StumbleUpon" alt="StumbleUpon"></li>
<li class="social_icon social_total"><img src="\'.plugins_url( \'/img/linkedin.png\', __FILE__ ).\'" title="LinkedIn" alt="LinkedIn"></li>
<li class="social_icon social_total"><img src="\'.plugins_url( \'/img/delicious.png\', __FILE__ ).\'" title="Delicious" alt="Delicious"></li>
<li class="social_icon social_total"><img src="\'.plugins_url( \'/img/reddit.png\', __FILE__ ).\'" title="Reddit" alt="Reddit"></li>
<li class="social_icon social_total"><img src="\'.plugins_url( \'/img/digg.png\', __FILE__ ).\'" title="Digg" alt="Digg"></li>
<li class="social_icon social_total"><img src="\'.plugins_url( \'/img/facebook.png\', __FILE__ ).\'" title="Facebook" alt="Facebook"></li>
<li class="fb_total fb_comm">Comments</li>
<li class="fb_total fb_like">Likes</li>
<li class="fb_total fb_share">Shares</li>
</ul>
\';
global $post;
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'order\' => \'DESC\',
\'orderby\' => \'date\',
\'posts_per_page\' => 15,
);
$dash_posts = get_posts( $args );
// build out table
foreach( $dash_posts as $post ) : setup_postdata($post);
$link = get_permalink();
$grab = urlencode($link);
$request = new WP_Http;
$url = \'http://api.sharedcount.com/?url=\'.$grab.\'\';
$response = wp_remote_get ( $url );
if( is_wp_error( $response ) ) {
echo \'<p>There was an error getting the data. Please try again later.</p>\';
} else {
$output = json_decode( $response[\'body\'] );
}
$title = get_the_title();
$edit = get_edit_post_link( $post->ID, $title );
$date = get_the_date(\'m/d/y\');
// get my numbers
$stumble = $output->StumbleUpon;
$reddit = $output->Reddit;
$delicious = $output->Delicious;
$gplus = $output->GooglePlusOne;
$twitter = $output->Twitter;
$digg = $output->Diggs;
$linkedin = $output->LinkedIn;
// facebook sub counts
$fb_total = $output->Facebook->total_count;
$fb_cmbox = $output->Facebook->commentsbox_count;
$fb_click = $output->Facebook->click_count;
$fb_comms = $output->Facebook->comment_count;
$fb_like = $output->Facebook->like_count;
$fb_share = $output->Facebook->share_count;
// set even odd for some fancy
$altrows = ( \' altrow\' != $altrows ) ? \' altrow\' : \'\';
// now output the data
echo \'
<ul class="numbers\'.$altrows.\'">
<li class="post_date">\'.$date.\'</li>
<li class="post_title"><a href="\'.$edit.\'">\'.$title.\'</a></li>
<li class="social_total">\'.$twitter.\'</li>
<li class="social_total">\'.$gplus.\'</li>
<li class="social_total">\'.$stumble.\'</li>
<li class="social_total">\'.$linkedin.\'</li>
<li class="social_total">\'.$delicious.\'</li>
<li class="social_total">\'.$reddit.\'</li>
<li class="social_total">\'.$digg.\'</li>
<li class="social_total">\'.$fb_total.\'</li>
<li class="fb_total fb_comm">\'.$fb_comms.\'</li>
<li class="fb_total fb_like">\'.$fb_like.\'</li>
<li class="fb_total fb_share">\'.$fb_share.\'</li>
</ul>
\';
endforeach;
wp_reset_postdata();
echo \'</div></div>\';
}
有没有关于(a)让它发挥作用和(b)它应该去哪里的想法?
最合适的回答,由SO网友:Norcross 整理而成
这就是最终解决它的原因(我将包括相关部分)
$total = wp_count_posts()->publish;
$perpage = 10;
$curpage = isset( $_GET[\'pagenum\'] ) ? intval($_GET[\'pagenum\']) : 1;
global $post;
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'order\' => \'DESC\',
\'orderby\' => \'date\',
\'posts_per_page\' => $perpage,
\'offset\' => $perpage*($curpage-1)
);
$pages = ceil($total/$perpage);
$dash_posts = get_posts( $args );
处理查询,然后
$pagin = array();
for( $i = 1; $i <= $pages; $i++ ) {
$url = $_SERVER[\'REQUEST_URI\'] . "&pagenum=$i";
$link = "<li><a href=\'$url\'>$i</a></li>";
if ($curpage != $i) $link = str_replace( \'~\', \'\', $link );
$pagin[] = $link;
}
echo \'<div id="blgs_post_loader"><ul>\'. implode( \'\', $pagin ) .\'</ul></div>\';
负责分页本身。无论出于何种原因,标准的“paged”变量在管理端无法正确启动。