Wordpress: authors page with custom query - wordpress

On a wordpress site I am coding an authors page. Therefore I use a custom query that I also use in several other parts of the site. Partly it works fine, but as it has a major bug, I cannot use it:
I receive not only a chosen autors post. All posts of all authors are listet.
If I use the main loop, I do not have that problem. But because of the pagination which should be the same everywhere, I would like to use the custom loop. Where is my mistake?
Here my code:
<?php
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
?>
<section class="tk_fullwidth">
<div id="primary" class="content-area">
<h1 class="page-title">Beiträge von: <?php echo $curauth->nickname; ?></h1>
<div id="main" class="site-main" role="main">
<div class="grid" data-masonry='{ "itemSelector": ".grid-item", "columnWidth": 285, "gutter": 20 }'>
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => '50',
'paged' => $paged,
'order' => 'DESC',
'orderby' => 'date',
) );
?>
<!-- begin loop -->
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="grid-item">
<a class="xyz" href="<?php the_permalink(); ?>"></a>
<h3 class="entry-title"><?php the_title(); ?></h3>
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('medium');
}
?>
</div><!-- grid-item -->
<?php endwhile; ?>
<!-- end loop -->
<div class="tk_pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( '<', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( '>', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
</div><!-- #main -->
</div><!-- #primary -->
</section><!--tk_fullwidth-->

Related

"How to fix 'Custom pagination 404 error' in PHP

I'm facing problem in my custom post pagination. The custom pagination is work fine on single page with same code but it is not working on another page.
I have updated the permalink many times but nothing happen. I also update .htaccess file.
My source code listed here...
<div id="home" class="tab-pane fade in active">
<p class="main_dv"><?php
//$count=1;
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$data= new WP_Query(array(
'post_type'=>'software',// your post type name
'category_name' => 'general_software', // your category name
'posts_per_page' => 5, // post per page
'paged' => $paged,));
if($data->have_posts()) :
while($data->have_posts()) : $data->the_post();?>
<div class="testcustompost1">
<div class="pst_div">
<div class="thumb">
<?php the_post_thumbnail();?>
</div>
<div class="title_des">
<div class="title">
<?php the_title();?>
</div>
<div class="description">
<?php the_content();?>
</div>
<div class="downlod_btn">
<button class="btn"> Download </button>
</div>
</div>
</div>
</div>
<?php //$count++;
endwhile;
echo '<div class="paginat_design">';
$total_pages = $data->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' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
echo '</div>'?>
<?php else :?>
<?php _e('404 Error: Not Found', ''); ?>
<?php endif; ?>
<?php wp_reset_postdata();?>
</p>
</div>
404 Not found error
You should try this:
<?php
/**
* Looping through the content
*/
$page = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
$data = new WP_Query (array(
'post_type'=>'software',
'category_name' => 'general_software',
'posts_per_page' => 5,
'page' => $page
)); ?>
<?php while ($data -> have_posts()) : $data -> the_post(); ?>
<!-- Your html code here -->
<?php endwhile; ?><?php wp_reset_query(); ?><?php wp_reset_postdata();
?>
Here is the pagination part:
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $data->max_num_pages,
'current' => max( 1, get_query_var( 'page' ) ),
'format' => '?page=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Previous Page', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Next Page', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
Let me know if it is this help you. If not, try to change the value from paged to page.

Pagination does not work, I consider fixing or new coding

I coded a wordpress-site and since some time the pagination worked fine. Then it stopped working.
I tried different plugins, but no one works. I want to put the pagination in a special place within a self-coded loop. Therefore the plugin of bestwebsoft seemed to be the best, since it offers the option of displaying the pagination by the use of a bit of PHP. But also that does not work. The support of bestwebsoft is not helpful, they only repeat what I can read in the documentation.
My questions:
1) Is there eventually a problem in my loop, that stops the pagination from displaying?
2) Is it a problem that I use "Masonry" for showing the excerpts and order the excerpts in a horicontal manner? Maybe that interferes with the pagination?
<div class="grid" data-masonry='{ "itemSelector": ".grid-item",
"columnWidth": 285, "gutter": 20 }'>
<?php
$args = array(
'post_type' => array('post',
'os_buch_review',
'os_review',
'os_classic_review',
'os_versus',
),
'post_status' => 'publish',
'nopaging' => false,
'posts_per_page' => '20',
'order' => 'DESC',
'orderby' => 'date',
'cat' => '-5738,-1705, -5933',
);
$tk_startteaser_querie = new WP_Query( $args );
if( $tk_startteaser_querie->have_posts() ) :
?>
<?php
while( $tk_startteaser_querie->have_posts() ) :
$tk_startteaser_querie->the_post();
?>
<div class="grid-item">
<a class="linkclass" href="<?php the_permalink(); ?>"></a>
<h3 class="entry-title"><?php the_title(); ?></h3>
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('medium');
}
?>
<?php the_excerpt(); ?>
</div><!-- grid-item -->
<?php
endwhile;
if ( function_exists( 'pgntn_display_pagination' ) ) {
pgntn_display_pagination( 'posts' );
}
wp_reset_postdata();
?>
<?php
else :
esc_html_e( 'Derzeit keine Beiträge!', 'text-domain' );
endif;
?>
</div><!--grid-->
I want to have some kind of pagination. I would prefer a self-coded one, but could not do it. Therefore I also would accept a plugin-solution. but nothing works! Most of all i would like to know where the problem is located.
I don't know what is the code in pgntn_display_pagination function but I have modified your code with below one which will work for you.
Kindly check below code.
<div class="grid" data-masonry='{ "itemSelector": ".grid-item",
"columnWidth": 285, "gutter": 20 }'>
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; // get the current page variable and set it
$args = array(
'post_type' => array('post',
'os_buch_review',
'os_review',
'os_classic_review',
'os_versus',
),
'post_status' => 'publish',
'nopaging' => false,
'posts_per_page' => '20',
'order' => 'DESC',
'orderby' => 'date',
'cat' => '-5738,-1705, -5933',
'paged' => $paged // use $paged variable here
);
$tk_startteaser_querie = new WP_Query( $args );
if( $tk_startteaser_querie->have_posts() ) :
?>
<?php
while( $tk_startteaser_querie->have_posts() ) :
$tk_startteaser_querie->the_post();
?>
<div class="grid-item">
<a class="linkclass" href="<?php the_permalink(); ?>"></a>
<h3 class="entry-title"><?php the_title(); ?></h3>
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('medium');
}
?>
<?php the_excerpt(); ?>
</div><!-- grid-item -->
<?php
endwhile;
// Below is full code of pagination
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Next', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Previous', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
wp_reset_postdata();
?>
<?php
else :
esc_html_e( 'Derzeit keine Beiträge!', 'text-domain' );
endif;
?>
</div><!--grid-->

Calling variable inside loop based on post count

I am creating a shortcode in a wordpress theme which needs to show properties on area based it was working fine. I have tried to keep 10 area title which was custom one. For that i called varable as $area_title1 to 10 in the wordpress loop also i have added a counter and called the counter with the variable for getting $area_title1 i called variable as $area_title.$counter which was without counting it shows exactly the value for example 1 , 2, etc my code is below.
function home_category_with_image_session($atts){
ob_start();
$atts = shortcode_atts(
array(
'propertytype' => '',
'area' => '',
'area1' => '',
'area2' => '',
'area3' => '',
'area4' => '',
'area5' => '',
'area6' => '',
'area7' => '',
'area8' => '',
'area9' => '',
'area_title' => '',
'area1_title' => '',
'area2_title' => '',
'area3_title' => '',
'area4_title' => '',
'area5_title' => '',
'area6_title' => '',
'area7_title' => '',
'area8_title' => '',
'area9_title' => '',
),
$atts,
'home_category_type'
);
$area=$atts['area'];
$area1=$atts['area1'];
$area2=$atts['area2'];
$area3=$atts['area3'];
$area4=$atts['area4'];
$area5=$atts['area5'];
$area6=$atts['area6'];
$area7=$atts['area7'];
$area8=$atts['area8'];
$area9=$atts['area9'];
$area_title=$atts['area_title'];
$area_title1=$atts['area1_title'];
$area_title2=$atts['area2_title'];
$area_title3=$atts['area3_title'];
$area_title4=$atts['area4_title'];
$area_title5=$atts['area5_title'];
$area_title6=$atts['area6_title'];
$area_title7=$atts['area7_title'];
$area_title8=$atts['area8_title'];
$area_title9=$atts['area9_title'];
$property_type=$atts['propertytype'];
$custom_terms = get_terms(array ('taxonomy' => 'property_city_taxonomy',
'orderby' => 'count',
'order' => 'ASC',
'name' => array ($area, $area1, $area2, $area3, $area4, $area5, $area6, $area7, $area8, $area9 ),
) );
$counter = -1;
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => $property_type,
'tax_query' => array(
array(
'taxonomy' => 'property_city_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts())
{
$counter++ ?>
<div class="col-md-3 col-sm-4 col-xs-6"><a href="<?php echo get_term_link($custom_term); ?>?property-type=<?php echo "$property_type"; ?>"><div class="top-locations"><div class="image-holder">
<?php $custom_term_id = $custom_term->term_id;
$custom_term_meta = get_term_meta( $custom_term_id, 'uploadimage_61032', true );
$num = $loop->post_count;
$area_title_display = $area_title.$counter;
?>
<img src="<?php echo $custom_term_meta; ?>" alt="<?php echo $term->name; ?>" class="img-responsive" />
<?php echo $area_title_display; ?>
<div class="home-banner-overlay"></div>
<div class="area-name"><?php echo $custom_term->name; ?></div>
</div><div class="city-text-container text-center"><?php echo $num; ?> Properties Listed</div></div></a></div>
<?php
} }
// Restore original post data.
wp_reset_postdata();
return ob_get_clean(); }
add_shortcode('home_category_type', 'home_category_with_image_session');
//Featured Post Home
function home_featured_posts($atts){
ob_start();
$atts = shortcode_atts(
array(
'no' => '',
'posttype' => '',
),
$atts,
'home-featured'
);
?>
<div class="session-featured-title">
<h5>Featured Property <?php echo $atts['posttype']; ?></h5>
<div class="related-control"><a class="btn prev"><i class="fa fa-arrow-left"></i></a><a class="btn next"><i class="fa fa-arrow-right"></i></a></div>
</div>
<div id="featured" class="owl-carousel owl-theme">
<?php
$args_1= array(
'post_type' => $atts['posttype'],
'posts_per_page' => $atts['no'],
'meta_query' => array(
array(
'key' => 'we_recommend_make-featured-property',
'value' => '1'
)
)
);
query_posts($args_1); if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="item"><a href="<?php the_permalink(); ?> " title="<?php the_title_attribute(); ?>">
<div class="featured-image">
<?php the_post_thumbnail('full', array('class' => 'img-responsive') ); ?></div>
<div class="featured-text-holder">
<div class="recent-post-title"><?php the_title(); ?></div>
<div class="featured-meta-detail">
<div class="py-list-price"><?php ro_price(); ?></div>
<div class="py-list-bed"><?php ro_bedroom(); ?></div>
<div class="py-list-bath"><?php ro_bathroom(); ?></div>
</div>
</div>
</a></div>
<?php endwhile; endif; wp_reset_query(); ?>
</div>
Here is your questions answer in php Refer This
And modified code of yours is below.
function home_category_with_image_session($atts){
ob_start();
$atts = shortcode_atts(
array(
'propertytype' => '',
'area' => '',
'area1' => '',
'area2' => '',
'area3' => '',
'area4' => '',
'area5' => '',
'area6' => '',
'area7' => '',
'area8' => '',
'area9' => '',
'area_title' => '',
'area1_title' => '',
'area2_title' => '',
'area3_title' => '',
'area4_title' => '',
'area5_title' => '',
'area6_title' => '',
'area7_title' => '',
'area8_title' => '',
'area9_title' => '',
),
$atts,
'home_category_type'
);
$area=$atts['area'];
$area1=$atts['area1'];
$area2=$atts['area2'];
$area3=$atts['area3'];
$area4=$atts['area4'];
$area5=$atts['area5'];
$area6=$atts['area6'];
$area7=$atts['area7'];
$area8=$atts['area8'];
$area9=$atts['area9'];
$area_title[0]=$atts['area_title'];
$area_title[1]=$atts['area1_title'];
$area_title[2]=$atts['area2_title'];
$area_title[3]=$atts['area3_title'];
$area_title[4]=$atts['area4_title'];
$area_title[5]=$atts['area5_title'];
$area_title[6]=$atts['area6_title'];
$area_title[7]=$atts['area7_title'];
$area_title[8]=$atts['area8_title'];
$area_title[9]=$atts['area9_title'];
$property_type=$atts['propertytype'];
$custom_terms = get_terms(array ('taxonomy' => 'property_city_taxonomy',
'orderby' => 'count',
'order' => 'ASC',
'name' => array ($area, $area1, $area2, $area3, $area4, $area5, $area6, $area7, $area8, $area9 ),
) );
$counter = -1;
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => $property_type,
'tax_query' => array(
array(
'taxonomy' => 'property_city_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts())
{
$counter++ ?>
<div class="col-md-3 col-sm-4 col-xs-6"><a href="<?php echo get_term_link($custom_term); ?>?property-type=<?php echo "$property_type"; ?>"><div class="top-locations"><div class="image-holder">
<?php $custom_term_id = $custom_term->term_id;
$custom_term_meta = get_term_meta( $custom_term_id, 'uploadimage_61032', true );
$num = $loop->post_count;
$area_title_display = '$area_title'.$counter;
echo $area_title[$counter];
?>
<img src="<?php echo $custom_term_meta; ?>" alt="<?php echo $term->name; ?>" class="img-responsive" />
<?php echo $area_title_display; ?>
<div class="home-banner-overlay"></div>
<div class="area-name"><?php echo $custom_term->name; ?></div>
</div><div class="city-text-container text-center"><?php echo $num; ?> Properties Listed</div></div></a></div>
<?php
}
}
// Restore original post data.
wp_reset_postdata();
return ob_get_clean(); }
add_shortcode('home_category_type', 'home_category_with_image_session');
//Featured Post Home
function home_featured_posts($atts){
ob_start();
$atts = shortcode_atts(
array(
'no' => '',
'posttype' => '',
),
$atts,
'home-featured'
);
?>
<div class="session-featured-title">
<h5>Featured Property <?php echo $atts['posttype']; ?></h5>
<div class="related-control"><a class="btn prev"><i class="fa fa-arrow-left"></i></a><a class="btn next"><i class="fa fa-arrow-right"></i></a></div>
</div>
<div id="featured" class="owl-carousel owl-theme">
<?php
$args_1= array(
'post_type' => $atts['posttype'],
'posts_per_page' => $atts['no'],
'meta_query' => array(
array(
'key' => 'we_recommend_make-featured-property',
'value' => '1'
)
)
);
query_posts($args_1); if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="item"><a href="<?php the_permalink(); ?> " title="<?php the_title_attribute(); ?>">
<div class="featured-image">
<?php the_post_thumbnail('full', array('class' => 'img-responsive') ); ?></div>
<div class="featured-text-holder">
<div class="recent-post-title"><?php the_title(); ?></div>
<div class="featured-meta-detail">
<div class="py-list-price"><?php ro_price(); ?></div>
<div class="py-list-bed"><?php ro_bedroom(); ?></div>
<div class="py-list-bath"><?php ro_bathroom(); ?></div>
</div>
</div>
</a></div>
<?php endwhile; endif; wp_reset_query(); ?>
</div>
I am sure this will works for you.

Wordpress pagination links show same posts as main page why?

I am using the following code it works and gives the pagination links, unfortunately each pagination page has same content and main page why?
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array( 'posts_per_page' => 3 ,'paged'=>$paged);
$the_query = new WP_Query( $args );
while ($the_query ->have_posts()) : $the_query -> the_post();
?>
<div id="pbox">
<div id="pthumb"><?php the_post_thumbnail('thumbnail', array('class' => 'mythumbnail')); ?></div>
<div id="pcontent">
<?php the_title(); ?>
<?php the_excerpt(); ?><br />
Post Category: <b><?php the_category( ', ' ); ?></b>
</div>
</div>
<?php
endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '/page/%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
echo $paged;
wp_reset_query(); ?>

WordPress - custom post type pagination

I have a custom post type (question) related to a custom taxonomy (support)
In my-theme/taxonomy.php I have this code:
<?php
$taxonomy = get_queried_object()->taxonomy;
if ($taxonomy == 'support')
{
get_template_part('template/support/categories');
exit;
}
wp_safe_redirect(site_url('/'));
exit;
?>
Means I target a specific template file for "support" taxonomy.
In my taxonomy template file I make a custom query:
<?php $current_category = get_term_by('id', get_queried_object()->term_id, 'support'); ?>
<?php $questions = new WP_Query(array(
'post_type' => array('question'),
'post_status' => 'publish',
'posts_per_page' => 5,
'posts_per_archive_page' => 5,
'paged' => ((get_query_var('page')) ? get_query_var('page') : 1),
'nopaging' => false,
'tax_query' => array(
array(
'taxonomy' => 'support',
'terms' => array($current_category->term_id)
)
),
'orderby' => 'menu_order',
'order' => 'ASC'
)); ?>
<?php if ($questions->have_posts()): ?>
<ul class="list normalize">
<?php while ($questions->have_posts()) : $questions->the_post(); ?>
<li>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<div class="nav-previous">
<?php next_posts_link( __('Previous', 'my-theme'); ?>
</div>
<div class="nav-next">
<?php previous_posts_link( __('Next', 'my-theme'); ?>
</div>
<?php wp_reset_postdata(); ?>
I have about 11 posts, page 1 shows the 5 first posts, but the problem is that no pagination is shown.
Any idea?
Thank you
You can try this pagination plugin
http://wordpress.org/plugins/wp-pagenavi/
And add this code in your template
<?php if (!function_exists('wp-pagenavi')) { wp_pagenavi(); } else { ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
</div><?php } ?>
I tried Your code and with my taxonomy
<?php $questions = new WP_Query(array(
'post_type' => array('studies'),
'post_status' => 'publish',
'posts_per_page' => 15,
'paged' => get_query_var('paged'),
'nopaging' => false,
'tax_query' => array(
array(
'taxonomy' => 'studies',
'terms' => 11
)
),
'orderby' => 'menu_order',
'order' => 'ASC'
));
?>
<div class="grid9">
<div class="entry-content">
<?php if ($questions->have_posts()): ?>
<ul class="list normalize">
<?php while ($questions->have_posts()) : $questions->the_post(); ?>
<li>
<h3><?php echo get_the_title(); ?></h3>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php
wp_pagenavi( array( 'query' => $questions ) );
?>
This works fine ,Don't forget to use wp-pagenavi plugin.
Put this code above the "endif;" and just below the end of while loop.
<div class="nav-previous">
<?php next_posts_link( __('Previous', 'my-theme'); ?>
</div>
<div class="nav-next">
<?php previous_posts_link( __('Next', 'my-theme'); ?>
</div>
<?php wp_reset_postdata(); ?>
Hope this helps you.
Ussually this works for me:
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $questions->max_num_pages
) );
Notice the question variable. :-) After you use this, go to Settings->Permalink and Save twice.
You can find a bigger tutorial here with the same example.

Resources