Wordpress Media Count and Next Page Navigation - php

Users are allowed to upload and access media files from front end. I am using following code which retrieve media files and show on user front end. I have 40 media files in my wordpress uploads but I want to show e.g. 20 media files at single page and I wonder if someone can help to write previous & next page navigation code for media files. e.g Page 1 2 3 ... 10
<?php
$args = array(
'post_type' => 'attachment',
/* 'posts_per_page' => '2', */
'numberposts' => -1,
'post_status' => null,
'author' => $current_user->ID,
'post_parent' => $post->ID,
'caller_get_posts'=> 1,
);
$attachments = get_posts( $args );
if ($attachments) {
foreach ($attachments as $attachment) {
echo '<tr><td><a href="'.wp_get_attachment_url($attachment->ID).'" rel="shadowbox" title="'.$attachment->post_excerpt.'">';
echo ($attachment->_wp_attached_file);
echo '</a>
</td>
</tr>';
}
?>

You can try to use the standard wordpress navigation.
Try to change your $args like this:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 20,
'paged' => $paged
);
UPDATE:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'attachment',
'posts_per_page' => 20,
'paged' => $paged,
'numberposts' => -1,
'post_status' => null,
'author' => $current_user->ID,
'post_parent' => $post->ID,
'caller_get_posts'=> 1,
);
$attachments = get_posts( $args );
if ($attachments) {
foreach ($attachments as $attachment) {
echo '<tr><td><a href="'.wp_get_attachment_url($attachment->ID).'" rel="shadowbox" title="'.$attachment->post_excerpt.'">';
echo ($attachment->_wp_attached_file);
echo '</a>
</td>
</tr>';
}
?>
UPDATE 2
Also you need to use this function
Post it somewhere below your posts
$big = 999999999; // need an unlikely integer
$navArgs = array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?page=%#%',
'total' => 1,
'current' => max( 1, get_query_var('paged') ),
'show_all' => False,
'end_size' => 1,
'mid_size' => 2,
'prev_next' => True,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
'type' => 'plain',
'add_args' => False,
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => ''
);
echo paginate_links( $navArgs );
As you can see you can set navArgs the way you like.
You need to get a total count of your posts and set the 'total' parameter.

Finally I wrote my solution
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$args = array(
'post_type' => 'attachment',
'posts_per_page'=> '10',
'paged' => $page,
'numberposts'=> -1,
'post_status' => 'any',
'author' => $current_user->ID,
'post_parent' => $post->ID,
'caller_get_posts'=> 1,
'number' => $display_count,
);
// Custom query, display posts by args defined users
$file_query = new WP_Query( $args );
$attachments = get_posts( $args );
if ($attachments) {
foreach ($attachments as $attachment) {
echo '<tr><td><a href="'.wp_get_attachment_url($attachment->ID).'" rel="shadowbox" title="'.$attachment->post_excerpt.'">';
echo ($attachment->_wp_attached_file);
echo '</a>
</td>
</tr>';
}
}
?>
Call query funtion in in your code
<?php previous_posts_link( '<< Previous Page', $file_query->max_num_pages ); ?> | <?php next_posts_link( 'Next Page >>', $file_query->max_num_pages ); ?>

Related

Wordpress Pagination not work in category page

I want to display pagination on category pages of WordPress theme
This is my code
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$posts = get_posts(array(
'numberposts' => 150,
'posts_per_page'=>16,
'offset' => 0,
'category__not_inint' => array($category),
'post_status'=>'publish',
'order'=>'ASC'
));
foreach($posts as $post){
setup_postdata($post);
$city_name = get_field( "city-name" );
$display = '' . $city_name . '';
}
the_posts_pagination();
wp_reset_query();
return $display;
and displays the pagination. But the results of all pages are similar to the first page.
You shouldn't be using get_posts if you need the query to be paginated.
Whilst it can be done, this is a total ball ache to achieve. Instead, you should be looking at WP_Query.
Further reading on WP_Query - WP_Query # wordpress.org
Your code could look something like the following;
<?php
$paged = (get_query_var('page')) ? get_query_var('page') : 1; // explain to wordpress we need this paged
$wp_query = new WP_Query(array( // the query
'post_type' => 'post',
'post_category' => '',
'post_status' => 'publish',
'numberposts' => 150,
'posts_per_page' => 15,
//'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged));
while ($wp_query->have_posts()) : $wp_query->the_post(); // the loop
// some code to make it look pretty
?>
<div class="post-grid">
<a href="<?php the_permalink(); ?>">
<h3 class="card-title"><?php the_title(); ?> </h3>
</a>
</div>
<?php endwhile;
echo ( paginate_links($args = array(
'base' => site_url().'%_%', // site_url prefix is needed for pagination on homepage
'format' => '?page=%#%',
'total' => $wp_query->max_num_pages,
'current' => $paged,
'show_all' => false,
'end_size' => 2,
'mid_size' => 2,
'prev_next' => true,
'prev_text' => 'Prev',
'next_text' => 'Next',
'type' => 'list',
'add_args' => false,
'add_fragment' => ''
)));
wp_reset_query();

Can't paginate a custom post type query

Trying to show custom post type offers one by one, paginating them in a custom page template by 1. But it's still showing all the posts.
Here is the code
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : '1';
$args = array(
'post_type' => 'offers',
'paged' => $paged,
'post_per_page' => 1,
'orderby' => 'meta_value_num',
'meta_key' => 'offer_order',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'campaigns',
'field' => 'slug',
'terms' => array(
'test-campaign'
)
)
)
);
$new = new WP_Query( $args );
if ( have_posts() ) while ($new->have_posts()) : $new->the_post(); ?>
<div>Post Layout</div>
<?php
endwhile;
wp_reset_postdata();
get_footer();
?>
Replace post_per_page with posts_per_pages in your Array args
——
After endwhile and before of wp_reset_postdata insert this code
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $new->max_num_pages
) );

list of all post from the current taxonomy of custom post type

I am creating custom post type(cpt) called circular. and the taxonomy for the cpt is circular_category in wordpress
What I want to achieve is generate a list of the titles of all circular custom post type in current taxonomy page.
the permalink /circular_category/free-circular/
I tried with this code with no luck, any ideas?
Here are some variable that can be add to the query
$cir_cat = $wp_query->get_queried_object();
$cat_name = $cir_cat->name;
$cat_id = $cir_cat->term_id;
$cat_slug = $cir_cat->slug ;
The Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'circular',
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'post_title',
'category__in' => $cat_id,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'circular_category',
)
)
);
$circular_query = new WP_Query( $args );
Now display the list cpt post from current cpt category
<ul id="circulars">
<?php
if($circular_query->have_posts()) :
while($circular_query->have_posts()) : $circular_query->the_post();
?>
<li>
<a href="<?php the_permalink() ?>" title="Link to <?php the_title_attribute() ?>">
<?php get_the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php
$total_pages = $circular_query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/pages/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
?>
<?php else :?>
<h3><?php _e('No Circular found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>
I have changed the query and its works I'm giving the answer here if someone needed
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'circular',
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'post_title',
'paged' => $paged,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'circular_category',
'field' => 'slug',
'terms' => $cat_slug
)
),
);
$circular_query = new WP_Query( $args );

Wordpress WP-PageNavi not working with custom query

I have been trying to solve this for a while now but with no success. I have read other similar posts but they are not working for me.
I have created a custom query with displays the correct results and the pagination shows but when i click on the page 2 etc, the url changes accordingly but the same posts remain.
My custom query is:
$sale_properties = new WP_Query(array(
'post_type' => 'properties',
'meta_key' => $nvr_initial.'_price',
'meta_value' => $nvr_price,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => get_query_var('page'),
'meta_query' => array(
array('key' => $nvr_initial.'_status',
'value' => array('For Sale'),),),));
and my other code is:
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $sale_properties->max_num_pages > 1 ) : ?>
<?php if(function_exists('wp_pagenavi')) { ?>
<?php wp_pagenavi( array( 'query' => $sale_properties ) ); ?>
<?php }else{ ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Previous', THE_LANG ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Next <span class="meta-nav">→</span>', THE_LANG ) ); ?></div>
</div><!-- #nav-below -->
<?php }?>
<?php endif; wp_reset_query();?>
I've tried page, paged and:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
Could someone please help me out as it's driving me crazy
Kind regards
S
Replace page with paged in below code:
$sale_properties = new WP_Query(array(
'post_type' => 'properties',
'meta_key' => $nvr_initial.'_price',
'meta_value' => $nvr_price,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => get_query_var('page'),
'meta_query' => array(
array('key' => $nvr_initial.'_status',
'value' => array('For Sale'),),),));
to
$sale_properties = new WP_Query(array(
'post_type' => 'properties',
'meta_key' => $nvr_initial.'_price',
'meta_value' => $nvr_price,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => get_query_var('paged'),
'meta_query' => array(
array('key' => $nvr_initial.'_status',
'value' => array('For Sale'),),),));
or put
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
above the query and pass $paged variable in query at the place of
get_query_var('page'),
Hopes it will help.
EDIT:
Wordpress standard way:
place on top:
$big = 999999999;
$current_page = get_query_var( 'paged', 1 );
$args = array(
//your query arguments
'paged' => $current_page
);
$my_query = new WP_Query($args);
Use loop like below:
while ( $my_query->have_posts() ) : $my_query->the_post();
// your code
endwhile;
then
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $my_query->max_num_pages
) );
place above where you want to print paging.
I had my custom wp query in the functions.php file. After moving it to my custom page template it worked
$sale_properties = new WP_Query(array(
'post_type' => 'properties',
'meta_key' => $nvr_initial.'_price',
'meta_value' => $nvr_price,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => get_query_var('paged'),
'meta_query' => array(
array('key' => $nvr_initial.'_status',
'value' => array('For Sale'),),),));

Paginate for a simple loop

I've got this code which simply displays all the posts for a particular author:
<?php
$all_active_tasks = get_posts(array(
'numberposts' => -1,
'offset' => 0,
'post_status' => 'publish',
'author' => '1',
'post_type' => 'post'
)
);
foreach($all_active_tasks as $post) :
$category = get_the_category();
setup_postdata($post);
?>
<div class="the-post">
<h2><?php the_title(); ?></h2>
<p><?php echo $category[0]->cat_name; ?></p>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
But what I can't figure out is how to paginate the results to say 10 per page. I've looked at the official codex but nothing I tried seemed to work.
Any help is appreciated.
You can use paged together with posts_per_page parameters. WP_Query. You can grab current page like this:
$paged = get_query_var( 'paged' ) ?: ( get_query_var( 'page' ) ?: 1 );
and then use it in your query:
$all_active_tasks = get_posts(array(
'posts_per_page' => 10,
'post_status' => 'publish',
'author' => '1',
'post_type' => 'post',
'paged' => $paged
));
In this way if you put /page/2/ at the end of your url, the query will return the posts from 11 to 20.
How to create the pagination itself, you can check these articles:
here and here.
Setting 'numberposts' => -1 means to get all records. You have to set for the first 10 records (0-9 records)
'numberposts' => 10,
'offset' => 0,
And for next 10, (10-19 records)
'numberposts' => 10,
'offset' => 10,
And for next 10, (20-29 records)
'numberposts' => 10,
'offset' => 20,
Try this working code
put this function in functions.php file of your active theme
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('<i class="fa fa-angle-double-left"></i>'),
'next_text' => __('<i class="fa fa-angle-double-right"></i>'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<div class='col-md-12'><nav class='custom-pagination pagination'>";
echo $paginate_links;
echo "</nav></div>";
}
}
and here is your modified code to work for pagination
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$all_active_tasks = get_posts(array(
'posts_per_page' => -1,
'paged' => $paged,
'offset' => 0,
'post_status' => 'publish',
'author' => '1',
'post_type' => 'post'
)
);
foreach($all_active_tasks as $post) :
$category = get_the_category();
setup_postdata($post);
?>
<div class="the-post">
<h2><?php the_title(); ?></h2>
<p><?php echo $category[0]->cat_name; ?></p>
</div>
<?php endforeach; ?>
<?php if (function_exists(custom_pagination)) {
custom_pagination(count($all_active_tasks),"",$paged);
}?>
<?php wp_reset_postdata(); ?>

Categories