WordPress paginate_links - 如何使用它?

时间:2016-08-14 04:46:52

标签: php wordpress twitter-bootstrap wordpress-4.5.2

我想为包含子页面的页面添加一个数字分页。这是我想要创建的分页(来自bootstrap):

<?php
include("connection.php");
session_start();
//Reset Variables
$message = '';
$location = "/login_system/index.php"; //default location to redirect after logging in
$username = '';
$password = '';

//Check to see if user is newly activated; if he is display a welcome message.
    if(isset($_GET['activated'])){
        if($_GET['activated'] == "true"){
            $message = "Thank you for verifying your account. Please login to continue.";
        }
    }

//Check to see if user is coming from another page; if he is then store that page location to redirect to after logging in.
if(isset($_GET['location'])) {
    $location =  htmlspecialchars($_GET['location']);
}

echo $location;

//Prepare login check statement
    $check_login = $connect->stmt_init();
    $sql = "SELECT id, password FROM $tablename WHERE username=?";
    $check_login->prepare($sql);
    $check_login->bind_param("s", $username);

//Execute Login Check
    if($_SERVER["REQUEST_METHOD"] == "POST") {
        $username = mysqli_escape_string($connect, $_POST['username']);
        $password = $_POST['password'];
        $check_login->execute();
        $check_login->store_result();
        $numrows = $check_login->num_rows;
        $check_login->bind_result($id, $match);
        $check_login->fetch();
        if ($numrows==1 && password_verify($password, $match)) {
            $_SESSION['login_user'] = $id;
            $goto = "localhost".$location;
            header("location: $goto");
            $message = "Success!";  
        } else {
            $message="Username or password is not valid."."<br>".$match."<br>";
        }
    }
    $connect->close();
?>

这是我的代码:

<nav>
  <ul class="pagination">
    <li>
      <a href="#" aria-label="Previous">
        <span aria-hidden="true">&laquo;</span>
      </a>
    </li>
    <li><a href="#">1</a></li>
    <li><a href="#">2</a></li>
    <li><a href="#">3</a></li>
    <li><a href="#">4</a></li>
    <li><a href="#">5</a></li>
    <li>
      <a href="#" aria-label="Next">
        <span aria-hidden="true">&raquo;</span>
      </a>
    </li>
  </ul>
</nav>

但我无法让它发挥作用。我错过了哪些想法?

1 个答案:

答案 0 :(得分:7)

  

分页赞:上一页1 2 3下一页

<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$data= new WP_Query(array(
    'post_type'=>'YOUR_POST_TYPE', // your post type name
    'posts_per_page' => 3, // post per page
    'paged' => $paged,
));

if($data->have_posts()) :
    while($data->have_posts())  : $data->the_post();
            // Your code
    endwhile;

    $total_pages = $data->max_num_pages;

    if ($total_pages > 1){

        $current_page = max(1, get_query_var('paged'));

        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '/page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text'    => __('« prev'),
            'next_text'    => __('next »'),
        ));
    }
    ?>    
<?php else :?>
<h3><?php _e('404 Error&#58; Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>

你可以试试上面的代码吗?我认为这对你有帮助。