Pagination not working on custom Wordpress loop - php

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.

Related

The Loop doesnt display posts

I am trying to have two different queries in my Loop, which is located in index.php. I am working with WP codex, but it isnt working. I want to have every post in its own special DIV later, so this is just start of my work.
The problem is, that the second part of code doesnt work, and I have no idea why. As far as I have read the codex, everything should be fine. Please help me.
<div class="col1">
<?php
$my_query = new WP_Query('category_A tym=featured&posts_per_page=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
?>
<!-- Do stuff... -->
<?php get_template_part('content', get_post_format()); ?>
<?php endwhile; ?>
<!--Over here everything works fine!-->
<!--This code doesnt show up. It is supossed to show 1 post, only heading and date with author. But it doesnt show nothing at all.-->
<?php
$my_queryOne = new WP_Query('posts_per_page=1');
while ($my_queryOne->have_posts()) : $my_queryOne->the_post();
if ($post->ID == $do_not_duplicate)
continue;
?>
<!-- Do stuff... -->
<h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<?php
endwhile;
?>
</div>
WP_Query takes an array as a parameter - i.e.
$query = new WP_Query( array( 'category_name' => 'featured','posts_per_page' => 1 ));
Also, when running multiple queries - use wp_reset_postdata() after the first loop.
Great examples here:
https://codex.wordpress.org/Class_Reference/WP_Query
Its probably best to review the coding guidelines that WordPress has laid out. You need to ensure that every open and close tag for inline PHP is on its own line.
Check here for reference:
https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#opening-and-closing-php-tags

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.

Preventing Single.php from showing all blog posts in wordpress

I have a single.php template but it shows all blogs posts. How do I prevent it from showing all posts:
Below is my code:
<?php $args = array('post_type' => 'realweddings');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1 class="post-heading entry-title"><span class="left-hanger"><?php the_title(); ?> </span></h1>
<section class="overview">
<?php the_content(); ?>
</section>
<?php endif;?>
<?php comments_template(); ?>
</article>
<?php endwhile;?>
I think the correct question is, why am I using WP_Query to construct my loop in single.php. Frankly, I can't answer both.
Your problem is your custom query and the straight forward answer is, delete your custom query. You should never use a custom query in place of the main query. The main query is very specific on templates as it uses the URL to set the arguments in the main query, which is in fact also just a normal WP_Query
Just use the normal loop, that should fix your issue. A custom query is not the way to solve an issue with the main query

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

Categories