最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

database - How to paginate information obtained from a query to a custom table?

programmeradmin0浏览0评论

I am building a small application which consults a registration group of a bd in wordpress, the code is as follows.

function opt_menu_cactaceas() {
    global $wpdb;
    $tabla_aspirantes = $wpdb->prefix . 'cactaceas2';
    echo '<div class="wrap"><h1>Lista de aspirantes</h1>';
    echo '<table class="wp-list-table widefat fixed striped">';
    echo '<thead><tr><th width="70%">Nombre Cientifico</th><th width="30%">Imagen</th></tr></thead>';
    echo '<tbody id="the-list">';
    $aspirantes = $wpdb->get_results("SELECT * FROM $tabla_aspirantes");
    foreach ( $aspirantes as $aspirante ) {

        $genero = esc_textarea($aspirante->genero);
        $especie = esc_textarea($aspirante->especie);
        $subespecie = esc_textarea($aspirante->subespecie);
        $autor = esc_textarea($aspirante->autor);

        if ( $subespecie == '' ):

            $nombreCactacea = "<i>$genero $especie</i> $autor";
        else: 
            $nombreCactacea = "<i>$genero $especie</i> subsp. <i>$subespecie</i> $autor";

        endif;

        $imagen_cactus = wp_get_attachment_image( 263, 'thumbnail' );

        echo "<tr><td><a href='#' title='$motivacion'>$nombreCactacea</a></td><td>$imagen_cactus</td></tr>";
    }
    echo '</tbody></table></div>';
}

As you can see in the image, the records are loaded but the navigation bar extends so much since it is showing all the records directly.

when obtaining the data these are shown directly on the page but there are 268 records so my query is the way in which I could paginate all these records since I want them to be shown for example on pages of 20 records each.

I am building a small application which consults a registration group of a bd in wordpress, the code is as follows.

function opt_menu_cactaceas() {
    global $wpdb;
    $tabla_aspirantes = $wpdb->prefix . 'cactaceas2';
    echo '<div class="wrap"><h1>Lista de aspirantes</h1>';
    echo '<table class="wp-list-table widefat fixed striped">';
    echo '<thead><tr><th width="70%">Nombre Cientifico</th><th width="30%">Imagen</th></tr></thead>';
    echo '<tbody id="the-list">';
    $aspirantes = $wpdb->get_results("SELECT * FROM $tabla_aspirantes");
    foreach ( $aspirantes as $aspirante ) {

        $genero = esc_textarea($aspirante->genero);
        $especie = esc_textarea($aspirante->especie);
        $subespecie = esc_textarea($aspirante->subespecie);
        $autor = esc_textarea($aspirante->autor);

        if ( $subespecie == '' ):

            $nombreCactacea = "<i>$genero $especie</i> $autor";
        else: 
            $nombreCactacea = "<i>$genero $especie</i> subsp. <i>$subespecie</i> $autor";

        endif;

        $imagen_cactus = wp_get_attachment_image( 263, 'thumbnail' );

        echo "<tr><td><a href='#' title='$motivacion'>$nombreCactacea</a></td><td>$imagen_cactus</td></tr>";
    }
    echo '</tbody></table></div>';
}

As you can see in the image, the records are loaded but the navigation bar extends so much since it is showing all the records directly.

when obtaining the data these are shown directly on the page but there are 268 records so my query is the way in which I could paginate all these records since I want them to be shown for example on pages of 20 records each.

Share Improve this question asked Feb 15, 2020 at 5:37 Diego AaronDiego Aaron 1134 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -1

You can use wordpress query args options. I hope this will help you.

// WP_Query arguments
   $args = array (
     'nopaging'               => false,
     'posts_per_page'         => '5',
);
// The Query
$query = new WP_Query( $args );
发布评论

评论列表(0)

  1. 暂无评论