Pagination, Not sure what to do
-
I have set my page up like this. Perhaps it sucks in terms of code layout, but it’s working great.
Website:
https://www.jamestrusler.co.ukCode:
https://jamestrusler.co.uk/wp-content/themes/jamestrusler/helpMe.txtI have been struggling to add a pagination feature.
In the code I have written “<!–PAGINATE THESE–>” At that point, can anyone tell me what must be done to paginate those posts. I want to not show more than 5 posts within those loops. and have something like page 1 out of 3.
I have been trying for ultra long now.
Thanks in advance.
-
Here is the latest version of the site:
https://wordpress.pastebin.com/kJjFB6SEHere is the site:
https://www.jamestrusler.co.ukAlright, to select only posts from a given category, your sql needs to look like this:
$cat_name = 'blog'; $sql = "SELECT p.* FROM $wpdb->posts p JOIN $wpdb->term_relationships tr ON (tr.object_id = p.ID) JOIN $wpdb->term_taxonomy tt ON (tt.term_taxonomy_id = tr.term_taxonomy_id AND tt.taxonomy = 'category') JOIN $wpdb->terms t ON (t.term_id = tt.term_id AND t.name = '$cat_name') WHERE p.post_type = 'post' AND p.post_status = 'publish' ORDER BY p.post_title ASC";
Please try this and see if it works to select only the ‘blog’ posts.
The tricky part is knowing which category name to assign to $cat_name.
Do you have a way to do that?
<?php $cat_name = 'Blog'; $sql = "SELECT p.* FROM $wpdb->posts p JOIN $wpdb->term_relationships tr ON (tr.object_id = p.ID) JOIN $wpdb->term_taxonomy tt ON (tt.term_taxonomy_id = tr.term_taxonomy_id AND tt.taxonomy = 'category') JOIN $wpdb->terms t ON (t.term_id = tt.term_id AND t.name = '$cat_name') WHERE p.post_type = 'post' AND p.post_status = 'publish' ORDER BY p.post_title ASC"; if ($mypages) : $limit = 3; // The number of posts per page $range = 5; // The number of page links to show in the middle $mypage = (isset($_GET['mypage'])) ? $mypage = $_GET['mypage'] : 1; $start = ($mypage - 1) * $limit; for ($i=$start;$i<($start + $limit);++$i) { if ($i < sizeof($mypages)) { // Process each element of the result array here $post = $mypages[$i]; setup_postdata($post);?> <div class = "leftSideWrap"> <h2><?php the_title(); echo " <small>"; the_time(get_option('date_format')); ?></small></h2> <div class = "top"></div> <div class = "middle"> <div class = "cornerImage"></div> <?php the_content(); ?> <div class = "commentSeperateLine dotted main"></div><br /> <p> <a href="<?php comments_link(); ?>"> Add / View Comments to this post</a> - <?php comments_number('There are currently no comments', 'There is 1 comment', 'There are % Responses' );?> </p> </div> <div class = "bottom"></div> </div> <?php } } echo _mam_paginate(sizeof($mypages),$limit,$range); else: echo '<h2>Sorry, There are no Pages to list</h2>'; endif;?> </div>
I have 4 categories:
Blog: name: Blog, slug: blog, id: 3
Music: name: Music, slug: music, id: 4
Poetry: name: Poetry, slug: poetry, id: 5
Pictures: name: Pictures, slug: pictures, id: 6I could do a for loop through it and use my array at top of page. But that code above is returning “Sorry, There are no Pages to list” so if that works i can figure out how to cycle through my pages.
Looks like you left out this line:
$mypages = $wpdb->get_results($sql);
Now, if you are using the same template for all, you need a way to select the single one you want based on which tab you click.
We will work on that next.
Once you get it working with
$cat_name = 'Blog';
, replace that line with the following:$request = $_SERVER['REQUEST_URI']; if (strpos($request,'/music/')) { $cat_name = 'Music'; } elseif (strpos($request,'/poetry/')) { $cat_name = 'Poetry'; } elseif (strpos($request,'/pictures/')) { $cat_name = 'Pictures'; } else { $cat_name = 'Blog'; }
I can’t manage to get that part to work.
<?php $request = $_SERVER['REQUEST_URI']; if (strpos($request,'/music/')) { $cat_name = 'Music'; } elseif (strpos($request,'/poetry/')) { $cat_name = 'Poetry'; } elseif (strpos($request,'/pictures/')) { $cat_name = 'Pictures'; } else { $cat_name = 'Blog'; } $sql = "SELECT p.* FROM $wpdb->posts p JOIN $wpdb->term_relationships tr ON (tr.object_id = p.ID) JOIN $wpdb->term_taxonomy tt ON (tt.term_taxonomy_id = tr.term_taxonomy_id AND tt.taxonomy = 'category') JOIN $wpdb->terms t ON (t.term_id = tt.term_id AND t.name = '$cat_name') WHERE p.post_type = 'post' AND p.post_status = 'publish' ORDER BY p.post_title ASC"; $mypages = $wpdb->get_results($sql); if ($mypages) : $limit = 3; // The number of posts per page $range = 5; // The number of page links to show in the middle $mypage = (isset($_GET['mypage'])) ? $mypage = $_GET['mypage'] : 1; $start = ($mypage - 1) * $limit; for ($i=$start;$i<($start + $limit);++$i) { if ($i < sizeof($mypages)) { // Process each element of the result array here $post = $mypages[$i]; setup_postdata($post);?> <div class = "leftSideWrap"> <h2><?php the_title(); echo " <small>"; the_time(get_option('date_format')); ?></small></h2> <div class = "top"></div> <div class = "middle"> <div class = "cornerImage"></div> <?php the_content(); ?> <div class = "commentSeperateLine dotted main"></div><br /> <p> <a href="<?php comments_link(); ?>"> Add / View Comments to this post</a> - <?php comments_number('There are currently no comments', 'There is 1 comment', 'There are % Responses' );?> </p> </div> <div class = "bottom"></div> </div> <?php } } echo _mam_paginate(sizeof($mypages),$limit,$range); else: echo '<h2>Sorry, There are no Pages to list</h2>'; endif;?> </div>
Also, my contact page and confirmation page no longer work because the original get or query posts has gone. Please refer to https://www.jamestrusler.co.uk/blog from now on, I had to put a underconstruction page up as visitors were getting lost (i love GAnalytics)
OK. I need to see what the REQUEST_URI looks like. Please change this:
echo _mam_paginate(sizeof($mypages),$limit,$range);
to this:
echo _mam_paginate(sizeof($mypages),$limit,$range); echo "<p>REQUEST:$request</p>";
Firstly, I got the pages listing correctly.
Instead of $request,/pagename/, it needed to be $request,pagename.
I’m sure i can sort the other if loops out.
Last question I guess is how do i list things via post date rather than post title ??
P.s, how can i thankyou for your sterling efforts and mega kind manner… It’s working exaclty how I imagined it. Beautiful.. damn damn beautiful. Thankyou so much.
To sort by date, change this:
ORDER BY p.post_title ASC";
to this:
ORDER BY p.post_date ASC";
and, you may need to change ASC to DESC if the order is backward.
If this is working, please use the dropdown at top right to mark this topic ‘Resolved’.
You are.. BRILLIANT. absolutely brilliant. Thankyou. Thankyou. Thankyou. Im so happy. Im so so damn happy. YESSS !!!!! THSNKSSSNAKNASANSKANSS
You are quite welcome!
- The topic ‘Pagination, Not sure what to do’ is closed to new replies.