自定义wpdb查询,每页发布一次?

时间:2017-02-17 13:04:41

标签: php wordpress

我需要这段代码的帮助:

<?php 
 global $wpdb;
 $id = get_the_ID();
 $querystring = "SELECT * FROM wp_candle WHERE id = $id";
 $results = $wpdb->get_results( $querystring ,OBJECT);
 foreach($results as $result)
 {
  echo
  '<div class="kerzenbox clearfix">
  <article class="post boxrechts" id="">
  <h2> "'.$result->contactName.'" </h2>
  <p> "'.$result->worte.'" </p>
  <img src="myImagePath" width="50" height="70"/>
  </article>
  </div>';

 }
?>

我需要实现每页帖子选项,但我不知道如何。如果没有针对此特定查询的每页帖子,该页面看起来很奇怪。

1 个答案:

答案 0 :(得分:0)

    <?php

    global $wpdb;

    $rows_per_page = 8;

    $current = (intval(get_query_var('paged'))) ? intval(get_query_var('paged')) : 1;

    $rows = $wpdb->get_results("SELECT * FROM wp_candle WHERE id = $id ");

    global $wp_rewrite;

    $pagination_args = array(

    'base' => @add_query_arg('paged','%#%'),

    'format' => '',

    'total' => ceil(sizeof($rows)/$rows_per_page),

    'current' => $current,

    'show_all' => false,

    'type' => 'plain',

    );

    if( $wp_rewrite->using_permalinks() )
    {   
    $pagination_args['base'] = user_trailingslashit( trailingslashit( remove_query_arg('s',get_pagenum_link(1) ) ) . 'page/%#%/', 'paged');
    }


    if( !empty($wp_query->query_vars['s']) )
    {
    $pagination_args['add_args'] = array('s'=>get_query_var('s'));
    }

    echo paginate_links($pagination_args);

    ?>