set post per page using get_post_meta - wordpress

I have code to show get_meta_post per page this my code
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$metas = get_post_meta(get_the_ID(),'value_gallery',false);
//var_dump($metas); ==> will output array(1) { [0]=> string(29) "1434,1402,1434,1435,1398,1434" }
foreach ($metas as $key ) {
$key_val = explode(",", $key);
$page = get_query_var('page');
$page = $page > 1 ? $page - 1 : 0 ;
if (isset($key_val[$page])) {
echo "<div class='col-lg-4'>".
wp_get_attachment_image($key_val[$page],"cherry-thumb-a") ."</div>";
}
}
endwhile;
else :
_e('Sorry, no posts matched your criteria.');
endif;
so this code will show my image using
wp_get_attachment_image($key_val[$page],"cherry-thumb-a")
and will paginate with link
https://yourpage.com/post-name/2
but this code just show image 1 per page , my problem is how to show 4 image per page , and how to change link paginate to
https://yourpage.com/post-name/page/2
i will hapyy if anyone can help me
Update
thankyou for attention , i was try adding array_chunk , and adding pagination , but it still not work , my full code:
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array('posts_per_page' => 1, 'paged' => $paged );
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$metas = get_post_meta(get_the_ID(),'value_gallery',false);
foreach ($metas as $meta) {
$key_val = explode(",", $meta);
$image_chunk = array_chunk($key_val, 4);
$page = get_query_var('page');
$page = $page > 1 ? $page - 1 : 0 ;
if (isset($key_val[$page])) {
foreach ($image_chunk[$page] as $image) {
echo "<div class='col-lg-4'>".
wp_get_attachment_image($image,"cherry-thumb-a") ."</div>";
}
}
}
endwhile;
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
endif;
many thank for attention

use array_chunk()
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$metas = get_post_meta(get_the_ID(),'value_gallery',false);
foreach ($metas as $meta) {
$key_val = explode(",", $meta);
$image_chunk = array_chunk($key_val, 4);
$page = get_query_var('page');
$page = $page > 1 ? $page - 1 : 0 ;
if (isset($key_val[$page])) {
foreach ($image_chunk[$page] as $image) {
echo "<div class='col-lg-4'>".
wp_get_attachment_image($image,"cherry-thumb-a") ."</div>";
}
}
}
endwhile;
endif;
Adding pagination for single post pages
Place this function in your functions.php
add_action('redirect_canonical', 'single_pagination_fix');
public static function single_pagination_fix( $redirect_url ) {
if ( is_paged() && is_singular() ) {
$redirect_url = false;
}
return $redirect_url;
}

Related

Wordpress pagination give duplicate content

So i change my wordpress permalink with custom structure "/%category%/%postname%/".
However, if i type manually "/poem/page/2", then it works. But the content of every page is the same. Wether it was "/page/3" or "/page/4", it was all the same.
at first, when i access "/page/2/", my page was giving "404 error". But i got this code from the internet, and when i use the code, it started to work. But, the content of every page is the same. The code goes on like this :
on function.php:
function vipx_filter_category_rewrite_rules( $rules ) {
$categories = get_categories( array( 'hide_empty' => false ) );
if ( is_array( $categories ) && ! empty( $categories ) ) {
$slugs = array();
foreach ( $categories as $category ) {
if ( is_object( $category ) && ! is_wp_error( $category ) ) {
if ( 0 == $category->category_parent ) {
$slugs[] = $category->slug;
} else {
$slugs[] = trim( get_category_parents( $category->term_id, false, '/', true ), '/' );
}
}
}
if ( ! empty( $slugs ) ) {
$rules = array();
foreach ( $slugs as $slug ) {
$rules[ '(' . $slug . ')/feed/(feed|rdf|rss|rss2|atom)?/?$' ] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
$rules[ '(' . $slug . ')/(feed|rdf|rss|rss2|atom)/?$' ] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
$rules[ '(' . $slug . ')(/page/(\d)+/?)?$' ] = 'index.php?category_name=$matches[1]&paged=$matches[3]';
}
}
}
return $rules;
}```
this is my loop post :
?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
echo $paged;
$term = get_queried_object();
$data = array( 'posts_per_page' => 2, 'category_name' => $term->slug, 'page'=>$paged );
$get_category_post = new WP_Query( $data );
?>
<?php if ( $get_category_post->have_posts() ) : while ( $get_category_post->have_posts() ) : $get_category_post->the_post(); ?>
<div class="post__list">
<?php if($term->slug === 'analect' || $term->slug === 'collection') { ?>
<span>for other categories</span>
<?php } else {?>
<span> <?php the_content(); ?> </span>
<?php } ?>
</div>
<?php endwhile; ?>
<?php
next_posts_link();
previous_posts_link();
?>
<?php endif; ?>

create pagination post meta_post

I am trying to make a pagination from meta_post (meta_key,meta_value) ==> (name_student,array(john,mike,natasha,joan,buck)) but i dont know how to make it, from codex just make pagination post , not a meta_post , and my meta_post in 1 array , i was find a similar question 1 , 2 , but I still haven't found the answer for my problem , if anyone can help me to find sollution for my problem I will be greatly helped , thankyou for read my problem , hopefully can help me
this my code :
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$ids = get_the_ID();
$args = array('paged' => $paged , 'post__in' => array($ids));
$the_query = new WP_Query($args);
if (have_posts()) :
while (have_posts()) : the_post();
$metas = get_post_meta(get_the_ID(),'name_student',false);
foreach ($metas as $meta) {
$key_val = explode(",", $meta);
$image_chunk = array_chunk($key_val, 3);
$page = get_query_var('page');
$page = $page > 1 ? $page - 1 : 0 ;
if (isset($key_val[$page])) {
foreach ($image_chunk[$page] as $image) {
echo "<div class='col-lg-4'>".
wp_get_attachment_image($image,"thumbnail") ."</div>";
}
}
}
endwhile;
$big = 9999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
wp_reset_postdata();
endif;

How do I change the default PHP sort order to alphabetic in a WP Loop?

I have a page that is creating a WP Loop that creates a list of pages, and I'd like to change the default sort to be alphabetical. This is the WordPress loop.
<?php
$i = 1;
while ($wp_query->have_posts()) : $wp_query->the_post();
$postid = get_the_ID();
$terms = get_the_terms($postid, 'program_categories' );
$terms2 = get_the_terms($postid, 'program_type' );
$permalink = get_permalink( $postid );
$title = get_the_title( $postid );
$raw_date = the_modified_date('F j, Y','','',false);
$d = strtotime($raw_date);
$all_terms = '';
if ( !empty( $terms ) ) { foreach ($terms as $term) { $all_terms .= strtolower(preg_replace('/\s+/', '-', $term->name)). ' '; } }
if ( !empty( $terms2 ) ) { foreach ($terms2 as $term2) { $all_terms .= strtolower(preg_replace('/\s+/', '-', $term2->name)). ' '; } }
?>
These are the buttons for changing the sort after the page loads, in case this helps provide info.
<li><?php _e('Default','swmtranslate'); ?></li>
<li><?php _e('Alphabetical','swmtranslate'); ?></li>
<li><?php _e('Last Updated','swmtranslate'); ?></li>
Is it as simple as adding something like this to the beginning of the loop?
while ($wp_query->have_posts()) : $wp_query->the_post("SELECT * FROM $title ORDER BY ASC");
This is covered right in the Codex. You need to define an orderby and order parameter in your query args:
$args = array(
'orderby' => 'title',
'order' => 'ASC',
);
$wp_query = new WP_Query( $args );
I think this link will be helpfull for your question. You need to set your custom query to short by title something like this.
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
(Alphabetizing Posts)
Your full code will be like this:
<?php
$i = 1;
$args = array(
'orderby' => 'title',
'order' => 'ASC',
);
$wp_query = new WP_Query( $args );
while ($wp_query->have_posts()) : $wp_query->the_post();
$postid = get_the_ID();
$terms = get_the_terms($postid, 'program_categories' );
$terms2 = get_the_terms($postid, 'program_type' );
$permalink = get_permalink( $postid );
$title = get_the_title( $postid );
$raw_date = the_modified_date('F j, Y','','',false);
$d = strtotime($raw_date);
$all_terms = '';
if ( !empty( $terms ) ) { foreach ($terms as $term) { $all_terms .= strtolower(preg_replace('/\s+/', '-', $term->name)). ' '; } }
if ( !empty( $terms2 ) ) { foreach ($terms2 as $term2) { $all_terms .= strtolower(preg_replace('/\s+/', '-', $term2->name)). ' '; } }
?>

How to paginate the user result page in Wordpress

This is my code anyone can please help me how to add pagination in user results page?
if (!empty($users)){
foreach($users as $user){
$user = get_userdata($user->ID);
<div class="wg_rec_dashboard_img">
<?php $wg_front_dp = get_user_meta($user->ID, 'wg_dp', true);?>
<img src="<?php echo $wg_front_dp; ?>">
</div>
<div class="wg_rec_username">
<?php echo $user->first_name; ?>
</div>
I have tried Wordpress' built-in pagination but its not working here.
You can use following;
<?php
$number = 50; // Update this according to your needs. Users perpage
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$offset = ($paged - 1) * $number;
$users = get_users();
$query = get_users('&offset='.$offset.'&number='.$number);
$total_users = count($users);
$total_query = count($query);
$total_pages = intval($total_users / $number) + 1;
// Iterate perpage users
foreach($query as $q){
$user = get_userdata($q->ID);
?>
<div class="wg_rec_dashboard_img">
<?php $wg_front_dp = get_user_meta($user->ID, 'wg_dp', true);?>
<img src="<?php echo $wg_front_dp; ?>">
</div>
<div class="wg_rec_username">
<?php echo $user->first_name; ?>
</div>
<?php } ?>
// Pagination part
<?php
if ($total_users > $total_query) {
?>
<div id="pagination" class="clearfix">
<span class="pages">Pages:</span>
<?php
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%/',
'current' => $current_page,
'total' => $total_pages,
'prev_next' => false,
'type' => 'list',
));
?>
</div>
Place this code in your theme's function.php (you can style the page navigation with CSS as you prefer):
The PHP:
if (!function_exists('_numeric_posts_nav')) {
function _numeric_posts_nav() {
global $wp_query;
/** Stop execution if there's if it is single past/page or only 1 page */
if( is_singular() ) return;
if( $wp_query->max_num_pages <= 1 ) return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li>%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n";
}
}
The CSS:
div.navigation ul li{list-style:none;list-style-type:none;display:inline;padding:4px 4px;margin:4px 4px;border:1px solid #ccc;background-color:#eaeaea}
div.navigation ul li.active{background-color:#FC0;border:1px solid #C60}
Now, use the function in your template file like this:
if (function_exists('_numeric_posts_nav')) { _numeric_posts_nav(); }
It will display pagination if number of posts are greater that 10

Wordpress: How to add pagination to page

I have 6 posts in Wordpress. I'm trying to display last 5 posts on page. Here is my code:
<?php
/*
Template Name: Posts Template
*/
?>
//header...
<?php $the_query = new WP_Query( array('showposts' => 5, 'post_type' => 'post')); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<h1><?php echo the_title(); ?></h1>
<?php endwhile; ?>
<?php posts_nav_link(); ?>
//footer
Pagination shows me that there is 4 pages, but like I said I have only 6 posts, so how it is possible? Also Pagination seems to not work correctly, doesn't matter on what page I am, it shows always last 5 posts.
Any idea what am I doing wrong?
Try adding a paged parameter to your query. You can also have a read of the documentation on the topic, there are some examples. Oh, and it's probably more forward-looking to use posts_per_page instead of showposts.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$the_query = new WP_Query( array(
'posts_per_page' => 5,
'post_type' => 'post',
'paged' => $paged
));
Add this code to functions.php
function posts_nav_link( $query_object, $show_previous_posts_link = false, $show_next_posts_link = false ) {
if( is_singular() )
return;
/** Stop execution if there's only 1 page */
if( $query_object->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $query_object->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() && $show_previous_posts_link)
printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() && $show_next_posts_link )
printf( '<li>%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n";
}
And use posts_nav_link($the_query):
<?php
/*
Template Name: Posts Template
*/
?>
//header...
<?php $the_query = new WP_Query( array('showposts' => 5, 'post_type' => 'post')); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<h1><?php echo the_title(); ?></h1>
<?php endwhile; ?>
<?php posts_nav_link($the_query); ?>
//footer

Resources