rez's avatar
Level 1

Pagination in wordpress

I have been working on a personal blog and I cannot get pagination to work. I have been stressed trying to get it to work after months. watching various training series. I am new to php and wordpress. I have evertything working on the first page (first 5 post show). but when i click page two or any other page there is nothing. Post count shows 14 from the query and it has 14 clickable link pages. i only have around 14 posts total. someone please help.

0 likes
10 replies
jlrdw's avatar

Wordpress doesn't have a forum?

rez's avatar
Level 1

Thank you for you valuable input. The code is php based and does not contain any wordpress shortcode.

zachleigh's avatar

This is a Laravel forum. You would be much better off trying a Wordpress forum. Or, if you really want to pursue you thread here, you at least need to post code or better explain your situation. Right now, we have absolutely no idea what you are doing in your project and can offer no help at all.

rez's avatar
Level 1

I thought it was for PHP skills also. I was trying to get in contact with jeffrey way and the contact form lead me here. Apologies for that. I wanted to learn the php pagination in an agnostic fashion so I can transition to other CMS or stand alones easier. when i click on other pages it stays blank.

code is below but i have to figure out how to make it show up.


<?php
if(isset($_GET['page'])){

$page = $_GET['page'];

} else {
    
    $page = "";
}

if($page == "" || $page == 1) {

    $page_1 = 0;

} 

else {
    $page_1 = ($page * 5) - 5;
}


?>


<?php
$post_query_count = "SELECT * FROM posts";
$find_count = mysqli_query($connection,$post_query_count);
$count = mysqli_num_rows($find_count);

$count = ceil($count / $per_page);
echo $count;


$query = "SELECT * FROM posts LIMIT $page_1, 5";
$select_all_posts_query = mysqli_query($connection,$query);

while($row = mysqli_fetch_assoc($select_all_posts_query)) {
$post_id = $row['ID'];  // is this post_id or ID?
$post_title = $row['post_title']; //
$post_author = $row['post_author']; //
$post_date = $row['post_date']; //
$post_image = $row['post_image']; 
$post_content = substr($row['post_content'],0,400); //
$post_status = $row['post_status']; //

if($post_status == 'published'){ } 


}

?>

<h1><?php echo 'total blog post' . "" . $count ?></h1>
zachleigh's avatar

Yes, you are correct. This forum is for general PHP as well as for Laravel. But we can't help you if you don't show us what you have done so far. What does you current code look like?

rez's avatar
Level 1

Thank you for sticking with me I have to check myself and be thankful I am receiving assistance from people that really don't have to. Do i need to set up a github account first and post the link of the code here? what is the procedure for posting code here?

zachleigh's avatar

Simply post your code surrounded by three backticks (`). Like this:

```

// code

```

Snapey's avatar

As far as my WP skills go, that is not how you interact with wordpress. You don't just grab stuff out of the database, you call the relevant WP API methods and it can then decide if posts are relevant, i.e. published versus draft versus revision etc.

Everything you need is in the wordpress codex

eg https://codex.wordpress.org/Working_with_WordPress

1 like
rez's avatar
Level 1

Thank you snapey. I was hoping that the code above could be used in any environment. So if i decide to extract the code it will still work wherever I take it. My logic was poor I was looking at Various CMS's as the shell and the php inside as long as it was semantic would be able to manipulate what i needed. I have everything working except clicking on the additional pages. The author from PHP beginner to master edwin at udemy is unreachable. I guess I will start from scratch here.

Snapey's avatar

If you are going to use Wordpress just use one of the out of the box themes that already supports pagination and modify that. You cannot just start pulling from the WP database without understanding how it is put together.

1 like

Please or to participate in this conversation.