Show Author post loops - php

So, I have the following to display post loops (wordpress):
METHOD A (works fine)
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args = array(
'post_type' => 'post',
'paged'=>$paged,
'posts_per_page' => 7,
'orderby' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
$id = get_the_ID();
global $paged;
while ( $loop->have_posts() ) : $loop->the_post();
?>
Posts go here
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<nav id="rh_nav_below">
<ul>
<li class="rh_nav_previous"><?php previous_posts_link( '« PREV', $loop->max_num_pages) ?></li>
<li class="rh_nav_next"><?php next_posts_link( 'NEXT »', $loop->max_num_pages) ?></li>
</ul>
</nav>
Now in the author page, following is used to display a post (a single posts):
METHOD B (works fine)
<?php rewind_posts(); while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
I have been trying to change the author post (method b) to method A format, so I can control the number of posts, orderby and etc.
Here is what I have tried:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$rhp_author_profile_id = get_the_author_id();
$args = array(
'post_type' => 'post',
'paged'=>$paged,
'posts_per_page' => 7,
'orderby' => 'date',
'order' => 'DESC',
'author ' => $rhp_author_profile_id
);
$loop = new WP_Query( $args );
$id = get_the_ID();
global $paged;
while ( $loop->have_posts() ) : $loop->the_post();
?>
Posts show here.
However, I am only getting the admin's posts on every other authors.
What am I doing wrong?
Thanks

Try This
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
global $current_user;
get_currentuserinfo();
$args = array(
'post_type' => 'post',
'paged'=>$paged,
'posts_per_page' => 7,
'orderby' => 'date',
'order' => 'DESC',
'author ' => $current_user->ID
);
$loop = new WP_Query( $args );
$id = get_the_ID();
global $paged;
while ( $loop->have_posts() ) : $loop->the_post();
?>

Related

Numeric pagination not appearing inside custom loop - Wordpress

I have been trying to add numeric pagination to my posts page which are filtered by category. The problem is that pagination won't appear when I call it. Pagination works perfectly fine when I use previous_posts_link and next_posts_link but this is not excatly what I want to achieve. What am I missing here?
My filtered category page.
<?php
// Get post ID
$post_type = get_post_type( $post->ID );
// Get category ID
$category_id = get_cat_ID(single_cat_title('', false));
// Wordpress pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// WP_Query arguments
$args_news = array (
'post_type' => array( 'post' ),
'pagination' => true,
'posts_per_page' => '2',
'orderby' => 'date',
'paged' => $paged,
'cat' => $category_id,
);
// The Query
$query = new WP_Query( $args_news );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post(); ?>
<?php get_template_part('categorytwo',get_post_format()); ?>
<?php }
} else {
// no news found
}
?>
<div class="pagination">
<?php my_pagination(); ?>
</div>
<?php
// Reset postdata
wp_reset_postdata();
?>
This pagination function works fine on my index.php page when I call it out.
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
query_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 4
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('catalog',get_post_format()); ?>
<?php endwhile; ?>
<div class="pagination">
<?php my_pagination(); ?>
</div>
<?php else : ?>
<?php // no posts found message goes here ?>
<?php wp_reset_query(); // add this ?>
<?php endif; ?>
Functions.php
if ( ! function_exists( 'my_pagination' ) ) :
function my_pagination() {
global $wp_query;
$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') ),
'prev_text' => __('« PREV'),
'next_text' => __('NEXT »'),
'total' => $wp_query->max_num_pages
) );
}
endif;
When I call it out on my index.php page it works fine.
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
query_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 4
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('catalog',get_post_format()); ?>
<?php endwhile; ?>
<div class="pagination">
<?php my_pagination(); ?>
</div>
<?php else : ?>
<?php // no posts found message goes here ?>
<?php wp_reset_query(); // add this ?>
<?php endif; ?>
Use this code on your loop:
<div class="posts_blog">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post', // Your post type name
'posts_per_page' => 5,
'paged' => $paged,
);
$loop = new WP_Query($args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
?>
<div class="posts_container ">
// Posts format: <?php get_template_part('catalog',get_post_format()); ?>
</div>
<?php
endwhile;
$total_pages = $loop->max_num_pages;
if ($total_pages > 1) {
?>
<div class="pagination">
<?php
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%'.'/#posts-blog',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('<'),
'next_text' => __('>'),
));
?>
</div>
<?php
}
}
wp_reset_postdata();
if (function_exists("pagination")) {
pagination($wp_query->max_num_pages);
}
?>
</div>

Add text when no result

I have to customize a function.
That fonction display custom posts on a (custom) category page.
The problem of this function concerns the absence of text when there is no results.
For instance, on any category page, if there is no available post to show in the grid, I would like to display a text such as "There is no post in this category. Please try another category."
Here is the code:
global $paged, $wp_query, $wp;
$args = wp_parse_args($wp->matched_query);
if ( !empty ( $args['paged'] ) && 0 == $paged ) {
$wp_query->set('paged', $args['paged']);
$paged = $args['paged'];
}
$cat_id = get_queried_object_id();
$temp = $wp_query;
$featuredPosts = array();
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $classieraFeaturedAdsCounter,
'paged' => $paged,
'cat' => $cat_id,
'meta_query' => array(
array(
'key' => 'featured_post',
'value' => '1',
'compare' => '=='
)
),
);
$wp_query= null;
$wp_query = new WP_Query($args);
while ($wp_query->have_posts()) : $wp_query->the_post();
$featuredPosts[] = $post->ID;
get_template_part( 'templates/classiera-loops/loop-ivy');
endwhile;
wp_reset_postdata();
wp_reset_query();
Does anyone has an idea to include this option?
Thanks.
SOLUTION:
wp_query = new WP_Query($args);
if ( have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post();
$featuredPosts[] = $post->ID;
get_template_part( 'templates/classiera-loops/loop-ivy');
endwhile;
else :
echo wpautop('No result');
endif;
I'm not a WP developer, but it seems to be
if ($wp_query->have_posts()) {
while ($wp_query->have_posts()) : $wp_query->the_post();
} else {
echo 'No post here';
}
Reference link : https://codex.wordpress.org/Function_Reference/have_posts

Not getting paging links when using WP_Query in Wordpress

I am not getting paging links when using WP_Query in WordPress. I am trying to fetch all the products on the page and show them in page by page manner. For this I want to show the paging links on the bottom. But paging links are not showing.
Below is the link - here
And the code for this is:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 3,
'paged' => $paged
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile;
?>
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
<? wp_reset_query(); ?>
next_posts_link(); and previous_posts_link(); and not showing links.
Please help.
Use this code instead of of above code.
<?php
$temp = $wp_query;
$wp_query= null;
$postsPerPage = 3;
$argsev = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => $postsPerPage,
'paged' => $paged
);
$wp_query = new WP_Query($argsev);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile; ?>
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
<?php
$wp_query = null; $wp_query = $temp;
wp_reset_query();
?>

wordpress pagination not showing

I have custom query on my wordpress page. Query looks like this:
$args = array(
'post_type' => array( 'tworca' ),
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page'=>12,
'post_parent' => 0
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args['paged'] = $paged;
// The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
... this is content of my query.....
}
?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
<?php wp_pagenavi(); ?>
</nav>
<?php
} else {
echo 'no results';
}
/* Restore original Post Data */
wp_reset_postdata();
?>
But my pagination doesn't show. Any idea why? I have read many posts with similar problem, but solution always was to add 'paged' parameter to wp query. And I have this parameter in my query and it doesn't help.
Thanks in advance for your help!
Try This..
`$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 12,
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC',
'post_parent' => 0,
'post_status' => 'publish',
);
query_posts( $args );
if ( have_posts() ) {
$i = 0;
while ( have_posts() ) {
the_post();
Your data here you want display like title
the_title();
}
wp_pagenavi();
}
wp_reset_query();
?>`
Please try by adding your query object to the wp_pagenavi(), which would be like,
wp_pagenavi( array( 'query' => $the_query ) );
Reference: http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html
Change wp_pagenavi() to wp_pagenavi( array( 'query' => $the_query) );

Trying to display posts only from one category in wordpress

I am using the magnet theme and am trying to display posts from a single category on the homepage. I have read through several post but have had no luck using the suggested methods I found.
Here is the snippet of code from the home-page-grid.php file that appears to be adding posts to the homepage
<!-- Start content -->
<div class="grid_8" id="content">
<div class="widget_container content_page">
<div class="post_list_medium_widget">
<div class="post_list_medium_style1">
<?php
global $paged;
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$query = new WP_Query( array ( 'paged' => $paged, 'orderby' => 'date', 'order' => 'DESC' ) );
$row_count=0;
while ( $query->have_posts() ) {
$row_count++;
$query->the_post();
$post_id = get_the_ID();
?>
Any thoughts as to what needs to be done to get this to display a single category and all its sub categories?
Try adding this array item:
'cat' => '14' . 14 is the category id
$query = new WP_Query( array ( 'paged' => $paged, 'cat' => '14', 'orderby' => 'date', 'order' => 'DESC' ) );
<?php
$catPost = get_posts(get_cat_ID("your_category_name")); //change this with your category
foreach ($catPost as $post) : setup_postdata($post); ?>
<div>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
</div>
<?php endforeach;?>

Categories