How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask ATLPROG Your Own Question

ATLPROG
ATLPROG, Computer Software Engineer
Category: Programming
Satisfied Customers: 7677
Experience:  MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
44910485
Type Your Programming Question Here...
ATLPROG is online now
A new question is answered every 9 seconds

I have an edited 'single.php' file in the Child directory of

Customer Question

Hi, I have an edited 'single.php' file in the Child directory of WP named 'single-CPT.php' (CPT = Custom Post Type). I put CPT instead of actual title of my CPT.
The page displays a grid of blog posts of a certain Category and the issue is that I can't get pagination to work (not a unique phenomenon I realise). More specifically, the problem is with 'pretty' permalinks (using %postname%) but pagination does work if I set permalinks to WP default of 'plain'. Using 'plain' permalinks as a solution is not an option, with regard to SEO.
My programming knowledge is a bit limited and I'm having trouble getting my head around the wp_query/custom query and how to 1) fault find and 2) how to manipulate a workable solution in code.
Anyone that might be able to steer me in the right direction or help out with a solution please?
I'm not sure what, and if, fees are involved here, but I guess I'll find that out soon enough.
Submitted: 5 months ago.
Category: Programming
Customer: replied 5 months ago.
The code in use is the following:<!-- Search all posts for those with the category for this trip -->
<?php
// Get current page and append to custom query parameters array
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
//'posts_per_page' => 6,
'order' => 'desc',
'orderby' => 'date',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => get_field('category_to_display'), // category from the custom field
),
),
);$the_query = new WP_Query( $args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $the_query;
?><!-- This line is here for purposes of error checking during process of getting pagination to work -->
<?php
echo "<p style='color: blue;'><strong>";
echo "The total number of pages to display is: " . $the_query->max_num_pages;
echo "</strong></p>";
?>
<div id="fwd-blog-grid">
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="fwd-grid-item">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<h2><a href='<?php the_permalink(); ?>'><?php the_title(); ?></a></h2>
<div class="entry-content">
<a href='<?php the_permalink(); ?>'><?php the_excerpt(); ?></a>
</div>
</div>
<?php endwhile; ?><?php
wp_reset_postdata();
echo "<div style='clear: both;'>";
wp_pagenavi( array( 'query' => $the_query ) );
//echo "<div style='float: right;'>";
// next_posts_link( 'Newer Posts &raquo;', $the_query->max_num_pages );
//echo "</div>";
//echo "<div style='float: left;'>";
// previous_posts_link( '&laquo; Older Posts' );
//echo "</div>";
echo "</div>";
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
<?php else: ?>
<p style="text-align: center;">There are no blog posts to display. Mianne is still sorting this bit of code out! :) </p>
<p style="text-align: center;">We might be out of signal range, but will be sure to add a blog post as soon as we can.</p>
<?php endif; ?>
</div>
Expert:  George Sibiya replied 5 months ago.

Hi,

My name is ***** ***** I will be assisting. Welcome to just answer. Please give me a moment to test you code and give you a feedback.

Regards,

G

Customer: replied 5 months ago.
ok. Thank you
Customer: replied 5 months ago.
There could also be other variables that may be important for you to know, but the problem is that I don't know if they are relevant?For example: I am using Advanced Custom Fields and one if the variables is used in the arguments [$args] for the custom query - the variable is 'category_to_display'. I have however set this to be a fixed value (and not using the variable) but it made no difference.
Expert:  George Sibiya replied 5 months ago.

Hi, I am still on it but in order not to delay you further I'll optout for now to allow other experts to have input. I'll post my response once done

Customer: replied 5 months ago.
Ok, thank you George. I'll look forward to what suggestions you might come up with.
Expert:  George Sibiya replied 5 months ago.

Hi, for now what I think might contribute to the issue is the way the WP_Query() is reset. Lets try the standard wp_reset_postdata() explained here (https://codex.wordpress.org/Function_Reference/wp_reset_postdata) and see if it does not change any thing.

Customer: replied 5 months ago.
Hi. I appreciate that you do have a difficult task with this issue because you aren't able to fault find it yourself. I do however already have the wp_reset_postdata() in my code. I can understand that it would be easily missed in the code above though :/
Expert:  George Sibiya replied 5 months ago.

Ok great. We will solve it I just had to refresh my WP knowledge hence the long wait. Thanks for your patience. Please paste your latest version with wp_reset_postdata().

Customer: replied 5 months ago.
No problem. ...it is the same as above (as the original)
Customer: replied 5 months ago.
I'm checking this page at the moment, wondering if it might have the solution?
https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
Expert:  George Sibiya replied 5 months ago.

Ok, you might also look at this video as well. https://www.youtube.com/watch?v=xEA2zviCakw

The author has the example projects for each lesson here https://github.com/Alecaddd/WordPress101

Customer: replied 5 months ago.
sorry, just on the issue of payment ....do you know why payment has been taken out of my account already? ...have you received payment?
Customer: replied 5 months ago.
thank you for those examples, but they actually aren't of any help. My pagination code already works!! ...IF I use 'plain' (default) permalinks. Pagination breaks if I use 'pretty' permalinks
Customer: replied 5 months ago.
There is some issue with my URL/permalink, because of use of a custom post type (I think?) ...that is my hunch, but is somewhat confirmed through the exercise with changing permalinks in WP.
Expert:  George Sibiya replied 5 months ago.

Ok no prob. I have your code in WP I just need to add some dummy blog posts and test. I'll however only be able get back to it in about 9 hours.

As for payments, it only goes to an expert after you will have rated and have accepted an answer that they will have provided.

As it stands I think even if its taken you are still eligible for a refund.

Customer: replied 5 months ago.
I see. Thanks for clarification. No problem about the time frame too. It's ok. Thanks for looking into it. I fear that you might not be able to replicate my issue though. The 'major_trips' part of the URL is the CPT I've created. ...and that is tied in with ACF (advanced custom fields). The problem is that I don't know which bits of information might be relevant, and which might be irrelevant. It could be the best solution is for remote assistance, but let me have a think about it.If the function for the CPT should help, here it is:** remember though that the pagination of posts are *not* for the CPT. They are standard categories created in WP. **function major_trips_cpt() {
$args = array(
'labels' => array(
'name' => 'Major Trips',
'singular_name' => 'Major Trip',
'all_items' => 'All Major Trips',
),
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields' ),
'menu_position' => 4,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'description' => ( 'New Major Trip Entry'),
'public' => true,
'has_archive' => true,
'menu_icon' => get_stylesheet_directory_uri() . '/images/fwdaus-icon.png'
//'rewrite' => array('slug' => '4wdaus-major_trips')
);
register_post_type( 'major_trip', $args );
}
add_action( 'init', 'major_trips_cpt' );
Expert:  George Sibiya replied 5 months ago.

Thanks. I am back on it and will update you on the progress.

Expert:  George Sibiya replied 5 months ago.

Please see attached

Expert:  George Sibiya replied 5 months ago.

The system is rejecting the file please find it here https://dl.dropboxusercontent.com/u/21712482/info

Customer: replied 5 months ago.
I too couldn't submit my response. Am posting this plain text to see if that works at least?...
Customer: replied 5 months ago.
I too have uploaded a response file to dropbox ... https://dl.dropboxusercontent.com/u/8187030/justanswer-mianneresponse-1.txt
Customer: replied 5 months ago.
Just another thought on a [small] bit of information that may be of use? .....When I'm viewing a page that is 'paged' (which is verified by 'echo get_query_var('paged')' ) ....when I hover over the 'next page' pagination link at the bottom of the page, the URL at the bottom left of the browser shows the page to be loaded has current page URL with '/page/2/' added ....when clicked, this page isn't loaded and the first page is loaded again.When using Chrome Dev Tools (Network) it shows the '/page/2/' or any page number '/page/#/' being redirected with 301 redirectI don't know if I sent a site URL to you, but the test site can be viewed here: 4wdaus.com/4wdtest/travel-blog/
Expert:  George Sibiya replied 5 months ago.

Ok great thanks for for the info. Let me trouble shoot it further.

Expert:  George Sibiya replied 5 months ago.

Hi,

It seems your code does not have a problem. I did not change even a single line but its working for all permalinks. The only difference is, my parent theme is the twentysixteen theme. This leaves us with one prime suspect, the web server. The 301 error somehow substantiates this. What webserver are you using? If it is Apache I can share my complete site configuration.

Regards,

George

Customer: replied 5 months ago.
hmmm ....that sounds really interesting. I must say that I'm not sure I'm entirely convinced as yet, because I'm certain that I've done something weird with the way I've put things together! haha :) It certainly sounds promising though.I'm reasonably certain that Apache server is being used, but I'll have to check. This host for the website is GoDaddy. If you're familiar with them, then that is a bonus. Otherwise I will go and check when I can. I may not be able to do this until later today? I'm also not sure where I alter server settings in such a case, but I'm sure GoDaddy support will help with whatever I need doing.
Customer: replied 5 months ago.
Oh .....another suspect could of course be the Divi theme I am using, but changing this would be a bit of an exercise for me. I'll go with the web server adjustments first.
Expert:  George Sibiya replied 5 months ago.

I think a parent theme would not really be a challenge I'm not sure though but you can try it

Expert:  George Sibiya replied 5 months ago.

This is all I have in my styles sheet:

/*
Theme Name: Twenty Sixteen Child
Theme URI: http://example.com/twenty-fifteen-child/
Description: Twenty Sixteen Child Theme
Author: Me
Author URI: http://example.com
Template: twentysixteen
Version: 1.0.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: light, dark, two-columns, right-sidebar, responsive-layout, accessibility-ready
Text Domain: twenty-sixteen-child
*/

Expert:  George Sibiya replied 5 months ago.

The bold line is all what is required to specify a parent theme and a function in functions.php

Expert:  George Sibiya replied 5 months ago.

Please see https://codex.wordpress.org/Child_Themes

Customer: replied 4 months ago.
Thanks. I'm not quite sure of the relevance of this information though? ....I might not have explained myself well. Changing the theme is not an issue, but having everything work with the new theme will take a bit of fiddling it seems (I did try changing the theme and everything to do with my CPT and ACF didn't work). I didn't bother looking into it at the time.I'm more interested in the server settings you mentioned though.
Expert:  George Sibiya replied 4 months ago.

Ok I do understand completely it was just in case you would want to test with a different theme.

I am also positive once the server is sorted, all will be fine.

Customer: replied 4 months ago.
ok, cool. How do I proceed regarding the server configuration? Will you provide settings that you are using? ...or shall I send you configuration of how mine is set?
Expert:  George Sibiya replied 4 months ago.

You can get it here https://dl.dropboxusercontent.com/u/21712482/000-default.conf

Tell them in an environment where it works we have this configuration on Apache and the module 'rewrite' is enabled.

Customer: replied 4 months ago.
Ok .... I'm not so sure about this. I would be surprised if this wasn't enabled already, but I will check.I did some editing yesterday on the 'live' site and enabled pagination for a different page (through functionality in the theme settings) and it works just fine. This is with permalinks set to %postname% .....so, if 'mod_rewrite' wasn't enabled, would this pagination still be able to work?To put it another way: the fact that pagination works on another page, with 'pretty' permalinks being used, would that not imply the 'mod_rewrite' is already enabled?The page in question (that is working) is this one: http://www.4wdaus.com/expedition-vehicles/
Expert:  George Sibiya replied 4 months ago.

Hi,

In essence, if you have .htaccess in the root directory of your site, there is no need for the module to be enabled. But if the .htaccess gets overwritten every time you update the permalinks, enabling the module and configuring the web server does take care of it.

Please share the contents of your .htaccess so I can have a look.

Regards,

G

Customer: replied 4 months ago.
I have attached a file with the contents of htaccess. ....although I still don't see the need as the pagination is working on one of the pages. It doesn't make sense.My thought is still that I am doing something weird with URL, because of the use of the CPT.
Expert:  George Sibiya replied 4 months ago.

Hi,

I think this should be fine as long as .htaccess does not get overwritten. If there is a page on the same site that allows it then it means the server should be fine.

I have just viewed the site, pagination seems to work on miscellaneous blogs and not on travel blogs where "Older posts" does not take you anywhere. I believe that is what you mean by it working on one page and not on another. Please do correct me if its not the case.

If so, do "Misc Blog" files and "Travel Blog" files reside in different directories or not? If they are, how does the directory structure that involves them look?

Customer: replied 4 months ago.
you are correct on all points.This is what I have explained earlier, the Travel Blog pages are what uses the CPT and you can see the different URL structure in the address bar.As I'm not that familiar with WP file structure, and how it stores pages and posts, I can't tell you if they are in different directories. In fact, I don't even think they exist as files, but are data in the database.I feel we are running around in circles now and I don't feel we are making any headway at all. I was hoping to find someone that is more familiar with WP as it seems you are 'learning' as you go.
Expert:  George Sibiya replied 4 months ago.

I also do feel we have exhausted what I thought might be possible causes given that I found it to be working on my environment with configs sent earlier. I'll opt out to give other experts a chance.

Customer: replied 4 months ago.
Maybe best idea. I very much appreciate your efforts. Thanks George
Expert:  Bhavik Joshi replied 4 months ago.

Hi Mimi

I will go through this and get back to you !!

Customer: replied 4 months ago.
Much appreciated. Thank you. I will wait to hear from you ...