Add pagination in a Wordpress Latest Posts widget - php

In a Wordpress Latest Posts widget I want to add pagination
public function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', $instance['title'] );
$count = $instance['count'];
$category = $instance['category'];
echo $before_widget;
$output = '';
if ( $title )
echo $before_title . $title . $after_title;
global $post;
if ( isset( $category ) && $category != '' ) {
$args = array(
'category_name' => $category,
'posts_per_page' => $count,
);
} else {
$args = array(
'posts_per_page' => $count,
);
}
$posts = get_posts( $args );
if(count($posts)>0){
$output .='<div class="sp-latest-posts-widget latest-posts">';
foreach ($posts as $post): setup_postdata($post);
$output .='<div class="media">';
if(has_post_thumbnail()):
$output .='<div class="pull-left">';
$output .=''.get_the_post_thumbnail($post->ID, 'xs-thumb', array('class' => 'img-responsive')).'';
$output .='</div>';
endif;
$output .='<div class="media-body">';
$output .= '<h3 class="entry-title">'. get_the_title() .'</h3>';
$output .= '<div class="entry-meta small"><span class="st-lp-time">'. get_the_time() . '</span> <span clss="st-lp-date">' . get_the_date('d M Y') . '</span></div>';
$output .='</div>';
$output .='</div>';
endforeach;
wp_reset_query();
$output .='</div>';
}
echo $output;
echo $after_widget;
}
I tried with pagination_nav(); after adding code in functions.php and I used WP-PageNavi, but without success.
How to add numbered pagination in this case?
Thanks in advance!

Use below code for numbered pagination,
<?php the_posts_pagination( array(
'mid_size' => 1,
'prev_text' => __( 'Prev', 'textdomain' ),
'next_text' => __( 'Next', 'textdomain' ),
'screen_reader_text' => ' ',
) );
} ?>

Related

Can I show all categories and tag of each post in Wordpress?

I'm currently creating a shortcode for a post list in WordPress. And here's my code
function mgc_post_default() {
global $post;
extract(shortcode_atts(array(
'cat' => '',
'num' => '5',
'order' => 'DESC',
'orderby' => 'post_date',
), $atts));
$args = array(
'cat' => $cat,
'posts_per_page' => $num,
'order' => $order,
'orderby' => $orderby,
);
$output = '';
$posts = get_posts($args);
foreach($posts as $post) {
setup_postdata($post);
$output .= '<div class="movie-poster">' . get_the_post_thumbnail() . '</div>';
$output .= '<div><h2>'. get_the_title() .'</h2></div>';
$output .= '<div>' . get_the_date() . '</div>';
$output .= '<div>' . get_the_content() . '</div>';
$output .= '<div>' . get_field( "status" ) . '</div>';
$output .= '<div>' . get_the_category( $id )[0]->name . '</div>';
$output .= '<div>' . get_the_tags( $id ) [1]->name . '</div>';
}
wp_reset_postdata();
return '<div>'. $output .'</div>';
}
// Register shortcode
add_shortcode('mgc_post', 'mgc_post_default');
It's working, but the category and tag of each post just appeared 1. What I want is it can show all categories and tags info of each post.
So my question is how to show all those categories and tags for each post?
Thank you,
To retrieve all categories for a post, use wp_get_post_categories instead of get_the_category. Then you just loop over the categories inside your foreach.
Same for the terms. But then use wp_get_post_terms.
So something like this:
$posts = get_posts($args);
foreach($posts as $post) {
setup_postdata($post);
$categories = wp_get_post_categories( $post );
$foreach( $categories as $category ) {
$output .= '<div>' . $category->name . '</div>';
}
}
wp_reset_postdata();
Thank you #anotheraccount for the answer, but I've solved my own problem by adding this code 'get_the_tag_list()' and 'get_the_category_list()' to the $output

Styling Related Posts Horizontally

I would like to display related posts horizontally.
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID,'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
$output = '<div class="rp-users">';
foreach ( $authors_posts as $authors_post ) {
$output .= get_the_post_thumbnail($authors_post->ID,array(370, 300, true));
$output .= '</div>';
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
return $output;
}
I have tried the following css, but doesn't works.
.rp-users {display:inline-block;}
.rp-title {font-size:19px; width: 370px;text-align:left;display:block;}
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID,'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
$output = '<div class="rp-users">';
foreach ( $authors_posts as $authors_post ) {
$output .= '<div class="rp-user-entry">';
$output .= get_the_post_thumbnail($authors_post->ID,array(370, 300, true));
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
$output .= '</div>';
}
$output .= '</div>';
return $output;
And the css:
.rp-users {display:block;}
.rp-user-entry { display: inline-block; vertical-align: top; width: 33.333%; }
.rp-title {font-size:19px; width: 370px;text-align:left;display:block;}
I think the code should look like this
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID,'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
$output = '<div class="rp-users">';
foreach ( $authors_posts as $authors_post ) {
$output .= get_the_post_thumbnail($authors_post->ID,array(370, 300, true));
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
$output .= '</div>';
return $output;
}

Writing a function to output WooCommerce's featured products

I am trying to write a function to output featured products so that I can tie it in with Advanced Custom Fields to display more data on the front-end.
function featured_courses_query() {
$args = array(
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
'posts_per_page' => 3
);
$featured_query = new WP_Query( $args );
if ($featured_query->have_posts()) :
$html_out = '<ul class="products">';
while ($featured_query->have_posts()) :
$featured_query->the_post();
$product = get_product( $featured_query->post->ID );
$course_title = get_the_title($post->ID);
$course_level = get_field( "course_level" );
$course_id = get_field( "course_id" );
// Output product information here
$html_out .= '<li class="product type-product status-publish no-post-thumbnail first instock featured taxable shipping-taxable product-type-simple"><div class="entry-product"><div class="entry-wrap"><header class="entry-header">';
$html_out .= '<h4>' . $course_title . '</h4><p>' . $course_level . " - " . $course_id . '</p>';
$html_out .= '</header></div></div></li>';
endwhile;
$html_out .= '</ul>';
else : // No results
$html_out = "No Courses Found.";
endif;
wp_reset_query();
return $html_out;
}
add_shortcode( 'featured_courses', 'featured_courses_query' );
I'm not sure what I'm doing wrong, but when I use the shortcode [featured_courses] it outputs what's in the else. Is it better to write a custom function like this or edit the WooCommerce file that holds the their shortcode?
I looked at how woocommerce created their shortcode to outputting featured products via a shortcode and after researching other posts with queries I have found a solution. Here's an example of how I used it:
function featured_courses_query() {
$meta_query = WC()->query->get_meta_query();
$tax_query = WC()->query->get_tax_query();
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
);
$args = array(
'post_type' => 'product',
'stock' => 1,
'showposts' => 3,
'orderby' => 'rand',
'order' => 'DESC',
'meta_query' => $meta_query,
'tax_query' => $tax_query
);
$featured_query = new WP_Query( $args );
if ($featured_query->have_posts()) :
$html_out = '<ul class="products x-block-grid three-up">';
while ($featured_query->have_posts()) :
$featured_query->the_post();
$product = get_product( $featured_query->post->ID );
$course_title = get_the_title($post->ID);
$course_level = get_field( "course_level" );
$course_id = get_field( "course_id" );
$course_icon = get_field( "course_icon" );
$excerpt = get_the_excerpt($post->ID);
// Output product information here
$html_out .= '<li class="product type-product status-publish no-post-thumbnail first instock featured taxable shipping-taxable product-type-simple"><div class="entry-product">';
if( $course_icon ):
$html_out .= '<div class="course-icon"><img src="' . $course_icon . '" alt="' . $course_title . '"></div>';
endif;
$html_out .= '<h4>' . $course_title . '</h4><p>' . $course_level . " - " . $course_id . '</p><p>' . $excerpt . '</p>';
$html_out .= '</div></li>';
endwhile;
$html_out .= '</ul>';
else : // No results
$html_out = "No Courses Found.";
endif;
wp_reset_query();
return $html_out;
}
add_shortcode( 'featured_courses', 'featured_courses_query' );
function featured_courses_query() {
$html_out = "";
$args = array(
'post_type' => 'product',
'meta_key' => array(
'key' => '_featured',
'value' => 'yes'
),
'posts_per_page' => 3
);
$featured_query = new WP_Query( $args );
if ($featured_query->have_posts()) :
$html_out .= '<ul class="products">';
while ($featured_query->have_posts()) :
$featured_query->the_post();
$product = get_product( $featured_query->post->ID );
$course_title = get_the_title($post->ID);
$course_level = get_field( "course_level",$post->ID);
$course_id = get_field( "course_id",$post->ID);
// Output product information here
$html_out .= '<li class="product type-product status-publish no-post-thumbnail first instock featured taxable shipping-taxable product-type-simple"><div class="entry-product"><div class="entry-wrap"><header class="entry-header">';
$html_out .= '<h4>' . $course_title . '</h4><p>' . $course_level . " - " . $course_id . '</p>';
$html_out .= '</header></div></div></li>';
endwhile;
$html_out .= '</ul>';
else : // No results
$html_out = "No Courses Found.";
endif;
wp_reset_query();
return $html_out;
}
add_shortcode( 'featured_courses', 'featured_courses_query' );

Sorting wordpress posts by date published

im using the following code to display my posts on a page in wordpress that use a featured image:
$mypages = get_pages( array() );
if ( !empty( $mypages ) ) {
echo '<ul>';
foreach ( $mypages as $mypage ) {
if ( get_the_post_thumbnail( $mypage->ID ) ) {
echo '<div class="featured-container">';
echo '<div class="featured-image">';
echo '<li><a class="feat-hover" href="' . get_permalink( $mypage->ID ) . '">' . get_the_post_thumbnail( $mypage->ID ) . '</a></li>';
echo '</div>';
echo '<div class="featured-text">';
echo '' . get_the_title($mypage->ID ) . '';
echo '</div>';
echo '</div>';
}
}
echo '</ul>';
}
but before printing this information out i want to sort the $mypages array so that they display by date published. ive tried this code:
<?php $args = array(
'sort_order' => 'ASC',
'sort_column' => 'post_date',
);
$mypages = get_pages($args);
?>
but it doesnt seem to work, am i missing something or doing this the wrong way?
thanks in advance.
FULL CODE BEING USED:
$args = array(
'sort_order' => 'ASC',
'sort_column' => 'post_date'
);
$mypages = get_pages( array($args) );
if ( !empty( $mypages ) ) {
echo '<ul>';
foreach ( $mypages as $mypage ) {
if ( get_the_post_thumbnail( $mypage->ID ) ) {
echo '<div class="featured-container">';
echo '<div class="featured-image">';
echo '<li><a class="feat-hover" href="' . get_permalink( $mypage->ID ) . '">' . get_the_post_thumbnail( $mypage->ID ) . '</a></li>';
echo '</div>';
echo '<div class="featured-text">';
echo '' . get_the_title($mypage->ID ) . '';
echo '</div>';
echo '</div>';
}
}
echo '</ul>';
}
The issue is that you're passing $args into another array(). You only need to pass in the $args directly, since it's already an array(). Change the top block of code to:
$args = array(
'sort_order' => 'ASC',
'sort_column' => 'post_date'
);
$mypages = get_pages( $args );

Function paginate_links within a short code not showing where it should be

I created a short code to display a list of Projects, which they are in a custom post type "Projects". The short code is simply displaying the projects and I want to include the pagination within the short code. Everything is working well, except that the pagination is showing BEFORE the list of projects, it should output after the list.
Everything will work if it's built-in in a custom page template. But of I use the short code, it won't output the way it should.
Here is the short code PHP code :
if (!function_exists('shortcode_projects_list')) {
function shortcode_projects_list($atts, $content = null) {
global $wp_query;
global $options;
$args = array(
"type" => "standard",
"order_by" => "date",
"order" => "DESC",
"number" => "-1",
"category" => "",
"selected_projects" => ""
);
extract(shortcode_atts($args, $atts));
$html = "";
$_type_class = '';
$_portfolio_space_class = '';
if ($type == "standard"){
$_type_class = " standard";
$_portfolio_space_class = "portfolio_with_space";
} elseif ($type == "standard_no_space"){
$_type_class = " standard_no_space";
$_portfolio_space_class = "portfolio_no_space";
}
if($type != 'standard_no_space') {
$html .= "<div class='block-grid $_portfolio_space_class cf'>\n";
if (get_query_var('paged')) {
$paged = get_query_var('paged');
} elseif (get_query_var('page')) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
if ($category == "") {
$args = array(
'post_type' => 'projects',
'orderby' => $order_by,
'order' => $order,
'posts_per_page' => $number,
'paged' => $paged
);
} else {
$args = array(
'post_type' => 'projects',
'projects_cat' => $category,
'orderby' => $order_by,
'order' => $order,
'posts_per_page' => $number,
'paged' => $paged
);
}
$project_ids = null;
if ($selected_projects != "") {
$project_ids = explode(",", $selected_projects);
$args['post__in'] = $project_ids;
}
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post();
$terms = wp_get_post_terms(get_the_ID(), 'projects_cat');
$project_bg_color = get_post_meta(get_the_ID(), 'sbwp_project_bg_color', true);
$project_text_color = get_post_meta(get_the_ID(), 'sbwp_project_text_color', true);
$html .= "<article class='block-item third-width third-height cf";
foreach ($terms as $term) {
$html .= " portfolio_category_$term->term_id";
}
$title = get_the_title();
$excerpt = get_the_excerpt();
$post_featured_image = get_post_thumbnail_id(get_the_ID());
if ($post_featured_image) {
$project_thumbnail = wp_get_attachment_image_src( $post_featured_image, 'full', false);
if ($project_thumbnail) (string)$project_thumbnail = $project_thumbnail[0];
}
$custom_portfolio_link = get_post_meta(get_the_ID(), 'qode_portfolio-external-link', true);
$portfolio_link = $custom_portfolio_link != "" ? $custom_portfolio_link : get_permalink();
$target = $custom_portfolio_link != "" ? '_blank' : '_self';
$html .="' style='background-color: ".$project_bg_color."'>";
$html .= "<a href='".$portfolio_link."' rel='bookmark' title='".$title."'>";
$html .= "<div class='image' style='background-image: url(".$project_thumbnail.");'></div>";
$html .= "<div class='text ".$project_text_color."'>";
$html .= "<p>".$excerpt."</p>";
$html .= "<span class='line ".$project_text_color."'></span>";
$html .= "<h1>".$title."</h1>";
$html .= "</div>";
$html .= "</a>";
$html .= "</article>\n";
endwhile;
$html .= "</div>";
$html .= "<div class='m-all t-all d-all last_col cf'>";
$html .= bones_page_navi();
$html .= "</div>";
else:
?>
<p><?php _e('Sorry, no posts matched your criteria.', 'sbwp'); ?></p>
<?php
endif;
$html .= "</div>";
wp_reset_query();
}
return $html;
}
}
Here is the bones_page_navi() function :
function bones_page_navi() {
global $wp_query;
$bignum = 999999999;
if ( $wp_query->max_num_pages <= 1 )
return;
echo '<nav class="pagination">';
echo paginate_links( array(
'base' => str_replace( $bignum, '%#%', esc_url( get_pagenum_link($bignum) ) ),
'format' => '',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3
) );
echo '</nav>';
} /* end page navi */
The HTML output when using the short code can been seen here.
The pagination links (nav) should output within the div under the div with the block-grid class.
Thanks in advance !
That's because the navigation is echoed, not returned.
function bones_page_navi() {
global $wp_query;
$bignum = 999999999;
if ( $wp_query->max_num_pages <= 1 )
return;
$output = '';
$output .= '<nav class="pagination">';
$output .= paginate_links( array(
'base' => str_replace( $bignum, '%#%', esc_url( get_pagenum_link($bignum) ) ),
'format' => '',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3
) );
$output .= '</nav>';
return $output;
} /* end page navi */

Categories