Wordpress Category Pagination With Custom Permalinks - php

I have a site with a permalink structure I can't change, which is /blog/%category%/%postname%/ and I'm using previous_posts_link() and next_posts_link() to get pagination links. They work fine on date/all posts but not on category pages.
It's down to those links not having the right permalinks. They use /blog/events/page/2/ which results in a 404 page, whereas if I use /blog/category/events/page/2/, they work.
So, ideally I'd like /blog/events/page/2/ to work but if not then how can I get next_posts_link() to output the right links (/blog/category/events/page/2/)? Mindful of the fact I can't change the permalink structure, for seo purposes, apparently.
Here's the loop query:
<?php
global $query_string;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
parse_str( $query_string, $args );
$args['paged'] = $paged;
query_posts($args); ?>
<?php if (have_posts()) : ?>
<div id="post-results">
<div class="pagination">
<?php if (get_previous_posts_link()): ?>
<span><?php previous_posts_link( 'Recent posts' ); ?></span>
<?php endif; ?>
<?php if (get_next_posts_link()): ?>
<span><?php next_posts_link( 'Previous posts' ); ?></span>
<?php endif; ?>
</div>
... rest of loop
Thanks.

I had the same issue while working with plugin CPT UI custom post page template, and tried a lot to resolve the 404 page not found issue, Strangely the solution was quite simple, I just changes the permalink structure to "Numeric" and Voila it works!
Settings>>Permalinks>>Numeric

Related

Static wordpress page linking to posts

Hi I am trying to making my static wordpress page seen here
Link to the posts that are shown. However they are just linking to the file directory in the browser and not the post.
It was working before where it would just link to the post in a wordpress theme but now that doesn't even work.
I haven't touch the code in a week I left it in the state as mentioned above, where it would link but just to the wordpress theme.
Here is the code I use to get the information to the static page from wordpress -
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query_args = array(
'post_type' => 'post',
'paged' => $paged
);
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<article>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content(); ?>
</div>
<small><?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?></small>
<hr>
</article>
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
</div>
</nav>
<?php } ?>
<?php else: ?>
<article>
<h1>Sorry...</h1>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</article>
<?php endif; ?>
Basically what I am trying to do is get it so when I click on the post it takes me to that post. Not to the file directory.
Any help is appreciated. Thank you.
I'm using xubuntu 16.04_LTS incase that has anything to do with compatibility in wordpress.
I have permalinks setup as shown
If all you want is the links to work . . .
http://auroraservers.org/MainSite/?p=38 ---fails
http://auroraservers.org/MainSite/wordpress/?p=38 ---works
That works and could be adjusted in the permalinks custom area. If you want that URL to be different, you're dealing with htaccess changes.
While not beautiful, you could replace the_permalink(); with
$id = get_the_ID();
$url = 'http://auroraservers.org/MainSite/wordpress/?p='.$id;
try to rename Index.php as index.php. It seems like Wordpress can't find index.php as a template for the post and therefore the system redirects you to the file directory since it can't find any of the possible template files.
Addition: Since your "Index.php" seems not to include code for the display of single posts, you might want to set up a file "single.php" as a template for single posts. But nevertheless, Wordpress will try to use index.php in many situations - always when the actual template file for a particular post type can't be found, like archive.php, page.php and many others...
So it might be a good idea to create a "regular", all-purpose index.php, rename your static page to something else, for example to front-page.php and define t as your starting page.
I seem to have fixed the issue I have been having. Thanks for all the replies.
Incase someone needs to know what I have done.
I went into mysql and dropped the database and recreated it. Must of gotten corrupted somehow.

Pagination not working on custom Wordpress loop

I have been working on creating a custom loop and setting how many posts per page. I needed to make this loop separate to the blog posts per page - hence the custom loop.
Now the loop does work, however I noticed it is conflicting with the pagniation... pagination is showing but pages are not changing when you click 'next'.
Here is my loop:
<?php
$args = array('posts_per_page'=>12, 'post_type' =>'office');
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<div class="office-item-wrapper">
<div class="office-item">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
</article>
</div>
</div>
<?php endwhile; ?>
<?php else: ?>
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
</article><!--/ Article -->
<?php endif; ?>
Thanks in advance to anyone who might be able to help me solve this issue :-)
In WP, the pagination is sent by a GET variable under page, just add it to the query args:
$page = get_query_var('paged');
$args = array('posts_per_page'=>12, 'post_type' =>'office', 'paged'=>$page);
I recommend you to use get_query_var('paged') because if it's empty it will be send you to the first page.
Note: In some themes, the page number is sent in the page key, so try with page or paged.

WordPress broken pagination

Pagination on my custom post page was working fine but after adding few posts it got broken - the older post link is not working any more.
Please suggest how can I fix it? I have tried disabling plugins, changing permalinks and almost anything i could find easy on the WordPress Codex.
Here's my query with pagination:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('post_type' => 'press' , 'posts_per_page' => 50 , 'paged' => $paged);
query_posts( $args );
/*Setting up our custom query (In here we are setting it to show 12 posts per page and eliminate all sticky posts) */
//query_posts ( $args );//query_posts($query_string . '&caller_get_posts=6&posts_per_page=12');
?>
<ul class="griditemleft clear">
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<?php if (has_post_thumbnail() ) : ?>
<li>
<?php the_post_thumbnail('category-thumbnail'); ?>
<h2 class="press-title"><?php the_title(); ?></h2>
</li>
<?php endif; ?>
<?php endwhile; ?>
</ul>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Its not entirely clear on what you are trying to achieve, but I think you want to look closely at http://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
Specifying hard-coded offsets in queries can and will break pagination since offset is used by WordPress internally to calculate and handle pagination.
To get around this limitation, you will need to write some additional code to manually handle pagination; you need to detect whether a loop has additional pages and then dynamically calculate the appropriate offset for the current page.
The code for controlling custom pagination will all occur within your functions.php file and not within the template page.php You can set an initial offset, as well as redefine the number of posts per page. There are specific samples displayed on the codex link above.
You will be adding the action before the query is run, via
add_action('pre_get_posts', 'myprefix_query_offset', 1 );
and you will have to account for the customization via
add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );

wordpress custom post type pagination -- previous posts link showing when no previous posts exist

Lots of questions about custom post types and pagination, but as far as I can find, no-one else with this problem:
Post type created -- check
new query for custom archive page -- check
page one totally loads correctly -- check
page two shows posts it should -- check. BUT: it also still has the previous posts link.
which point to page three, even though there are no posts to display
The problem: post navigation still shows on page two, allowing for a click to page 3 (4, 5, 6 etc) -- where there are no posts. No 404, just a blank page, as though the loop is still looping through an infinite sea of nothingness.
Code:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query( array(
'post_type' => 'portfolio',
'posts_per_page' => 10,
'paged'=>$paged
) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<article >
/*stuff in here */
</article>
<?php endwhile; ?>
<div id="post-navigation">
<div class="nav-previous"><?php next_posts_link(__( 'Previous Projects' )) ?></div>
<div class="nav-next"><?php previous_posts_link(__( 'Next Projects' )) ?></div>
</div><!-- #post-navigation -->
Other info: I'm displaying this with archive-portfolio.php -- no blank page to muddy up permalinks.
On page 1, there's no option for next post -- so that half seems to be working, but previous just lets me go back in time forever, where there are no posts to display.
Many thanks.
Ok -- asked too soon. Here's the code that fixes the problem. If anyone can tell me why, that would be awesome. Otherwise it's all cargo cult....
<?php if (have_posts()) : ?>
<?php query_posts('post_type=portfolio&posts_per_page=10&caller_get_posts=1&paged='. $paged ); ?>
<?php while (have_posts()) : the_post(); ?>
<article >
/*stuff in here */
</article>
<?php endwhile; ?>
<div id="post-navigation">
<div class="nav-previous"><?php next_posts_link(__( 'Previous Projects' )) ?></div>
<div class="nav-next"><?php previous_posts_link(__( 'Next Projects' )) ?></div>
</div><!-- #post-navigation -->
<?php else : endif; ?>
Try this:
You can refer from here on wordpress codex:
--
Thanks

Retrieve posts by category name issue in WP Query

So I've created a custom page template for my "Topics" Page.
What I want to do is add in some PHP to the custom page template my Topics page uses, to retrieve permalinks for the 3 most recent posts from a chosen category.
E.g.
(From post category 1)
--> Permalink for post 1
--> Permalink for post 2
--> Permalink for post 3
My code so far is as follows:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<ul>
<?php
$category_posts = new WP_Query('cat=consumer-trust&showposts=3');
while ($category_posts ->have_posts()) : $category_posts->the_post();?>
<li>
<a class="yourclass" href="<?php the_permalink(); ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?> </a></li>
<?php endwhile; ?>
The problem, however, is that changing the cat in the WP_Query doesn't seem to make any difference. I've tried numbers and category names, and neither works.
Can anybody advise? This code will appear three times on the intended page, for three different categories.
use
query_posts( array ( 'category_name' => 'cat_name','meta_key'=>'_pr_date', 'orderby' => 'meta_value','order'=>'DESC') );
Thanks for all the help, I've managed to find an answer:
<?php global $post; // required
$args = array('category' => 18); // include category 18
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);
// put here what you want to appear for each post like:
//the title:
the_title();
endforeach;
?>

Categories