2014-09-01 57 views
0

我似乎无法找出以下内容: 在我正在开发的Wordpress主题中,索引会一次显示在索引上屏幕。当然,如果有20个帖子,则最新的10个帖子将显示在索引屏幕的第1页和第2页的最旧的10个帖子上。WordPress的:找出一个帖子会在索引屏幕上有什么页码

当查看任何单个帖子时,我想有一个功能,可以在查看索引屏幕时打印当前帖子的页码。

我试着做WP_queries,如:

$args = array(
    'date_query' => array(
     array(
      'after'  => get_the_date, 

... 
$query = new WP_Query($args); 

,但没有运气。

有没有人有任何建议,我应该去哪些方向来找出这个问题!?!任何帮助都非常有义务!

回答

0

你可以做一个查询页面上,并找出其中你的文章将....

$id = get_the_ID(); // set within your loop 

$args = array(
'date_query' => array(
    array(
     'after'  => get_the_date, 

$query = new WP_Query($args); 

$i=1; 
foreach($query as $post=>$key){ 
    if($key->ID == $id) {//not tested have a look at the object to get correct location 
     $postnumber= $i; 
    } 
$i++; 
} 

$page= $postnumber/10; 
$pagenumber= round($page, 0, PHP_ROUND_HALF_DOWN); 

似乎对资源的沉重,但!

0
I would probably opt for counting posts newer than the current one. 

    // This is not tested code, just a wild guess really. Sorry. 
    global $wpdb; 
    $query = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type='post' AND post_status='publish' AND post_date > %s"; 
    $count = $wpdb->get_var( 
     $wpdb->prepare( 
      $query, 
      get_the_date(/* probably needs a better date format here */) 
     ) 
    ); 

我能想到的另一个选择是在您的首页的链接上附加GET参数,因为您无论如何都会收到帖子。

<?php $count++ // in the loop right? ?> 
    <a href="<?php echo the_permalink();?>?index=<?php echo $count?>"?> 

This would of course not work unless everyone uses the frontpage to go to your pages, but then you could count newer posts as a fallback. 
相关问题