Update
Tested my script outside Wordpress and it works. So the problem with this pagination is inside Wordpress. I did some research and I added this rewrite code to my functions file, but still it doesn't work
function my_rewrite_globe_pagination()
{
add_rewrite_rule('^newspaper/page/([0-9]{1,})/?', 'index.php?paged=$matches[1]', 'top');
}
add_action('init', 'my_rewrite_globe_pagination');
The next button url looks like this: domain/page-name/?page=2
URL in browser after clicking next: domain/page-name/page/2/
Before update
first of all I would like to say I read all the similar questions on stackoverflow but I din't find the solution fitting to my case.
What I'm trying to achieve is a globe numeric pagination at my wordpress page. In my page template I have the code listed below and I have with this code two problems:
one is that I don't know how to change the prev and next buttons to numeric pagination
second whenever I click on the next button I see the same two images as on first page, despite the fact that my url changes to domain/page-name/2/
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 1;
}
$base_dir = trailingslashit(wp_upload_dir()['basedir']);
$base_url = wp_upload_dir()['baseurl'];
$dir_jpg = '/newspaper/jpg/';
$dir_pdf = '/newspaper/pdf/';
$images = glob($base_dir.$dir_jpg.'*.*');
$limit = 2;
$total =count($images);
$total_pages = ceil($total/$limit);
$offset = ($page-1) * $limit;
$images = array_slice($images, $limit);
foreach($images as $image) {
$url = $base_url.$dir_jpg.basename($image);
$filename = substr(basename($image), 0, -4);
$pdfs = $base_url.$dir_pdf.$filename.".pdf";
printf('<img src="%s" alt="'.$filename.'.pdf"><div class="newspaper-hover"></div>', esc_url($url));
}
?>
<ul class="pagination">
<li class="<?php if($page <= 1){ echo 'disabled'; } ?>">
Prev
</li>
<li class="<?php if($page >= $total_pages){ echo 'disabled'; } ?>">
Next
</li>
</ul>
For the first problem you simply have to print $page - 1 and $page + 1 instead "Prev" and "Next" (and check if the current page is the first or the last):
<ul class="pagination">
<li class="<?php if($page <= 1){ echo 'disabled'; } ?>">
<?php echo ($page <= 1) ? "X" : ($page - 1)?>
</li>
<li class="<?php if($page >= $total_pages){ echo 'disabled'; } ?>">
<?php echo ($page >= $total_pages) ? "X" : ($page + 1)?>
</li>
</ul>
The second problem is caused by your incorrect call to array_slice.
According to the documentation, the second parameter must be the start offset of the array ($offset in your case) and the third the length of the array you want ($limit), so this should fix it:
$images = array_slice($images, $offset, $limit);
Related
I'm trying to hide the "next page" button when I get to the last page, but I can't figure out how to do this. The other buttons work as expected.
I was following a tutorial and I came across the pagination, in the tutorial the person left the buttons always showing, but I don't think it's a good idea, so I hide some buttons depending on the situation on the page.
When there is no post on the page no button should appear - OK that worked
If there is at least one post the 'next page' button should appear alone - OK that worked
If you go to page 2 or beyond the 'Go to first page' and 'Back page' buttons should appear - OK that worked
The 'next page' button should disappear when I get to the last page, but it keeps going on and on
How i can do that?
Thanks
<?php
//pagination offset limit
$limit = 10;
$offset = ($PAGE['page_number'] - 1) * $limit;
$query = "select * from categories order by id desc limit $limit offset $offset";
?>
<!--my buttons-->
<div class="col-md-12 button-pos-x">
<?php if($offset>1):?>
<a href="<?=$PAGE['first_link']?>">
<button class="custom-btn btn-12"><span>Go to first page</span></button>
</a>
<a href="<?=$PAGE['prev_link']?>">
<button class="custom-btn btn-12 button2-pos-x"><span>Back page</span></button>
</a>
<?php endif;?>
<?php if(($offset>=1) || ($offset<$limit)):?>
<a href="<?=$PAGE['next_link']?>">
<button class="custom-btn btn-12 button2-pos-x"><span>Next page</span></button>
</a>
<?php endif;?>
</div>
//my pagination function
function get_pagination_vars()
{
/** set pagination vars **/
$page_number = $_GET['page'] ?? 1;
$page_number = empty($page_number) ? 1 : (int)$page_number;
$page_number = $page_number < 1 ? 1 : $page_number;
$current_link = $_GET['url'] ?? 'home';
$current_link = ROOT . "/" . $current_link;
$query_string = "";
foreach ($_GET as $key => $value)
{
if($key != 'url')
$query_string .= "&".$key."=".$value;
}
if(!strstr($query_string, "page="))
{
$query_string .= "&page=".$page_number;
}
$query_string = trim($query_string,"&");
$current_link .= "?".$query_string;
$current_link = preg_replace("/page=.*/", "page=".$page_number, $current_link);
$next_link = preg_replace("/page=.*/", "page=".($page_number+1), $current_link);
$first_link = preg_replace("/page=.*/", "page=1", $current_link);
$prev_page_number = $page_number < 2 ? 1 : $page_number - 1;
$prev_link = preg_replace("/page=.*/", "page=".$prev_page_number, $current_link);
$result = [
'current_link' =>$current_link,
'next_link' =>$next_link,
'prev_link' =>$prev_link,
'first_link' =>$first_link,
'page_number' =>$page_number,
];
return $result;
}
//my init function
$PAGE = get_pagination_vars();
I can't understand how to make the next page button hide when I get to the last page
Ive been building a data driven website displaying general info about countries in the world. Its been made so that 1 country is displayed per page and you can move the next country by clicking a pagination link. Only problem that I am having is I cannot limit the amount of visible links. I did try this for loop with the first line as this: for ($i = $Page; $i <= min($Page + 9, $TotalRecords); $i++) { which does reduce it to the 10 records however this does result in the website breaking when I test the web address by entering index.php?page=aa.
<nav class="mt-5">
<ul class="pagination pagination-lg justify-content-center">
<?php
if( isset($Page) ) {
if ($Page > 1 ) {
?>
<li class="page-item">
«
</li>
<?php
}
}
?>
<?php
global $ConnectingDB;
$sql = "SELECT COUNT(*) FROM countriesinfo";
$stmt = $ConnectingDB->query($sql);
$RowPagination = $stmt->fetch();
$TotalRecords = array_shift($RowPagination);
$RecordPagination = $TotalRecords / 1;
$RecordPagination = ceil($RecordPagination);
for ($i = $Page; $i <= $RecordPagination; $i++) {
if( isset($Page) ) {
if ($i == $Page)
{
?>
<li class="page-item active">
<?php echo $i; ?>
</li>
<?php
} else {
?>
<li class="page-item">
<?php echo $i; ?>
</li>
<?php
}
}
}
?>
<?php if (isset($Page) && !empty($Page) ) {
if ($Page+1 <= $RecordPagination) {
?>
<li class="page-item">
»
</li>
<?php
}
}
?>
</ul>
</nav>
I test the web address by entering index.php?page=aa.
You can check to make sure the get variable is a number by using php is_numeric() bulit in function
and if it isn't force it to number 1.
This will stop the website from breaking when someone manipulate the page varible.
Here is a code to help you out, put at the beginning of the page.
if(is_numeric($page)){
$page = $page; // if $page is a number do nothing
}else{
$page = 1; //else set $page to 1 which will fetch data from beginning
}
I have this code in header.php:
<?php
$page = "";
?>
<ul class="nav navbar-nav navbar-right nav-pos">
<li <?php if ($page == "home") { ?> class="active"<?php } ?>>Home</li>
<li <?php if($page == 'about') { ?> class="active"<?php } ?>>About us</li>
<li <?php if($page == 'contact'){ ?> class="active"<?php } ?>>Contact us</li>
<li class="hidden-xs"><i class="fa fa-search fa-rotate-90"></i></li>
</ul>
and on every page i put this code(and the name change depends on current page):
<?php include 'header.php';
$page = 'about' ?>
and in css i made this:
.active{color:red }
But it is not working... any idea why?
Thank you for time, I really appreciate it
The problem is that in your header.php you explicitly set $page = "" - which means none of the checks for $page == 'about'(etc) are passing.
Although you do set $page = 'about' in your main script, this is after the code in header.php has run, and its html output produced.
The solution is simple:
1) remove the $page = "" from header.php
2) set $page = "about" (or equivalent) before the include
You can do by using jQuery just adding this script in the footer section.
$(document).ready(function() {
// get current URL path and assign 'active' class
var pathname = window.location.pathname;
$('.nav > li > a[href="'+pathname+'"]').parent().addClass('active');
});
In your case, this will work.
$(function(){
var current = location.pathname;
$('.nav li a').each(function(){
var $this = $(this);
// if the current path is like this link, make it active
if($this.attr('href').indexOf(current) !== -1){
$this.addClass('active');
}
})
});
first you should remove $page = ""; from header
<?php
$page = "";//remove this line in header file
?>
<ul class="nav navbar-nav navbar-right nav-pos">
Second in every page swap these two lines
<?php
$page = 'about';//swap these lines
include 'header.php' ;
?>
some details: when header load then $page is empty and your conditions false in li tag. so, before header load you should assign value to $page
the best way: you should used $_SERVER['REQUEST_URI'] instead of hard coded $page. Example:
$url= explode('/',$_SERVER['REQUEST_URI']);
$page = end($request_array);//this show your_page.php
$page=preg_replace('"\.php$"', '', $page )//this will remove .php extension
I am a little stuck on how to show the Angellist API records on the page with pagination.
I succesfully got the first 50 records of the Angellist API showing on the page. When i change the number of $page to 2 and print $data, i do get the 50 records of page 2.
But now i don't know how i can do that automatically so i can get the pagination going.
This is the code i have until now:
<?php
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$jsonurl = 'https://api.angel.co/1/jobs/?page=' . $page;
$url = file_get_contents($jsonurl);
$data = json_decode($url,true);
$total = $data['total'];
$per_page = $data['per_page'];
$current = $data['page'];
$last_page = $data['last_page'];
?>
<div class="col-md-12" style="padding:0;">
<div class="col-md-6">
<div class="pagination">
<?php
$i = 0;
while($page < $last_page) :?>
<?php echo "<a href=\"/jobs?page=".$page++."\"\>Next 50</a>";
endwhile;
?>
</div>
</div>
I hope this is sufficient information for some help, but if you need more info i'm happy to give it.
You could a ternary with $_GET -
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
and then your url would look like
yourpage.php?page=2
And you can create a next link -
echo "Next 50";
<?php
// Code not directly relevant omitted (including lots of vars)
//Create pagination links
$first = "First";
$prev = "Prev";
$next = "Next";
$last = "Last";
if($current_page>1)
{
$prevPage = $current_page - 1;
$first = "First";
$prev = "Prev";
}
if($current_page<$total_pages)
{
$nextPage = $current_page + 1;
$next = "Next";
$last = "Last";
}
?>
<html>
<title></title>
<body>
<h2>Here are the records for page <?php echo $current_page; ?></h2>
<ul>
<?php echo $slots; ?>
</ul>
Page <?php echo $current_page; ?> of <?php echo $total_pages; ?>
<br />
<?php echo "{$first} | {$prev} | {$next} | {$last}"; ?>
</body>
</html>
EDIT/UPDATE:
I just realized I had a file called test.php a while back. I deleted it but I guess it's still in my site somehow... Nevertheless, I changed the word test with works. Now when I click next, it brings me to mydomain.com/works.php?page=2. But it shows a 404 error :/
Can somebody please tell me where I screwed up? Thanks!
The code certainly looks good; are you sure the problem is in the links being generated? Maybe you are being redirected from the "correct" page for some other reason?