I've created a shortcode that displays my recent posts but I want to have the option to add pagination. I'm trying to use the pagination attribute. Please see my code below. Any help would be greatly appreciated:
add_shortcode( 'list_recent_posts', 'list_recent_posts' );
function list_recent_posts( $atts ) {
ob_start();
// define attributes and their defaults
extract( shortcode_atts( array (
'posts' => 4,
'category' => '',
'ptype' => '',
'class' => '',
), $atts ) );
$class = $atts['class'];
// define query parameters based on attributes
$options = array(
'posts_per_page' => $posts,
'post_type' => $ptype,
'category_name' => $category
);
$query = new WP_Query( $options );
// run the loop based on the query
if ( $query->have_posts() ) { ?>
<ul class="media recent-posts <?php echo $class; ?>">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<li class="media-listitem">
<?php
if(has_post_thumbnail()):
?><a class="pull-left" href="<?php the_permalink(); ?>">
<div class="thumbnail">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('post_thumbnail');
}
?>
</div>
</a>
<?php else: ?>
<?php endif; ?>
<?php
if(has_post_thumbnail()): ?>
<div class="media-content marginlft-90">
<?php else: ?>
<div class="media-content">
<?php endif; ?>
<div class="caption">
<h4 class="media-heading"><?php the_title(); ?></h4>
<p><?php the_excerpt(); ?></p>
</div>
</div>
</li>
<?php endwhile;
wp_reset_postdata(); ?>
</ul>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
I found the answer in another post:
if ( ! function_exists('vpsa_posts_shortcode') ) {
function vpsa_posts_shortcode( $atts ){
$atts = shortcode_atts( array(
'per_page' => 2,
'order' => 'DESC',
'orderby' => 'date'
), $atts );
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => $atts["per_page"],
'order' => $atts["order"],
'orderby' => $atts["orderby"],
'paged' => $paged
);
$query = new WP_Query($args);
if($query->have_posts()) : $output;
while ($query->have_posts()) : $query->the_post();
$output .= '<article id="post-' . get_the_ID() . '" class="' . implode(' ', get_post_class()) . '">';
$output .= '<h4 class="post-title"><span>' . the_title('','',false) . '</span></h4>';
$output .= '<div class="row">';
$output .= '<div class="col-md-3">';
$output .= '<a href="' . get_permalink() . '" title="' . the_title('','',false) . '">';
if ( has_post_thumbnail() ) {
$output .= get_the_post_thumbnail( get_the_id(), 'featured', array('class' => 'img-responsive aligncenter'));
} else {
$output .= '<img class="img-responsive aligncenter" src="' . get_template_directory_uri() . '/images/not-available.png" alt="Not Available" height="150" width="200" />';
}
$output .= '</a>';
$output .= '</div>';
$output .= '<div class="col-md-9">';
$output .= get_the_excerpt();
$output .= '<span class="post-permalink">Read More</span>';
$output .= '</div>';
$output .= '</div>';
$output .= '<div class="post-info">';
$output .= '<ul>';
$output .= '<li>Posted: ' . get_the_time("F j, Y") . '</li>';
$output .= '<li>By: ' . get_the_author() . '</li>';
$output .= '<li>Categories: ' . get_the_category_list(", ") . '</li>';
$output .= '</ul>';
$output .= '</div>';
$output .= '</article>';
endwhile;global $wp_query;
$args_pagi = array(
'base' => add_query_arg( 'paged', '%#%' ),
'total' => $query->max_num_pages,
'current' => $paged
);
$output .= '<div class="post-nav">';
$output .= paginate_links( $args_pagi);
// $output .= '<div class="next-page">' . get_next_posts_link( "Older Entries »", 3 ) . '</div>';
$output .= '</div>';
else:
$output .= '<p>Sorry, there are no posts to display</p>';
endif;
wp_reset_postdata();
return $output;
}
}
add_shortcode('vpsa_posts', 'vpsa_posts_shortcode');
Related
on my Wordpress site, I created CPT shortcodes, I want there to be 6 posts per page. But when I have more posts, I want the most anician to be visible and to set up a pagination.
how can I add that?
Here is my current code:
function diwp_create_shortcode_newprojects_post_type(){
$args = array(
'post_type' => 'projets',
'posts_per_page' => '6',
'category_name' => 'neufs',
'publish_status' => 'published',
);
$query = new WP_Query($args);
if($query->have_posts()) :
while($query->have_posts()) :
$query->the_post() ;
$content = wpautop( get_the_content() );
/* $result .= '<div class="post-item">';
$result .= '<div class="post-poster">' . get_the_post_thumbnail() . '</div>';
$result .= '<div class="post-name">' . get_the_title() . '</div>';
$result .= '<div class="post-desc">' . get_the_content() . '</div>';
$result .= '</div>'; */
$result .= '<div class="block-actu-list"> <div class="img-actu">' . get_the_post_thumbnail( $post->ID, 'large') . '</div> <div class="legend">' . get_the_title() . '</div></div>';
endwhile;
wp_reset_postdata();
endif;
return $result;
}
add_shortcode( 'newprojects-listed', 'diwp_create_shortcode_newprojects_post_type' );
----------------- Edit -----------------
With #Saqib Amin 's answer I'm trying this, is this correct ?
// CPT shortcode HomePage
function diwp_create_shortcode_project_post_type(){
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'projets',
'posts_per_page' => '10',
'paged' => $paged,
'category_name' => '',
'publish_status' => 'published',
);
$query = new WP_Query($args);
if($query->have_posts()) :
while($query->have_posts()) :
$query->the_post() ;
$content = wpautop( get_the_content() );
/* $result .= '<div class="post-item">';
$result .= '<div class="post-poster">' . get_the_post_thumbnail() . '</div>';
$result .= '<div class="post-name">' . get_the_title() . '</div>';
$result .= '<div class="post-desc">' . get_the_content() . '</div>';
$result .= '</div>'; */
$result .= '<div class="block-actu-list"> <div class="img-actu">' . get_the_post_thumbnail( $post->ID, 'large') . '</div> <div class="legend">' . get_the_title() . '</div></div>';
endwhile;
wp_reset_postdata();
endif;
return $result;
}
add_shortcode( 'project-listed', 'diwp_create_shortcode_project_post_type' );
This code working with image and title but post_per_page doesn’t work and trying
number' =>1
is work but if any taxonomy Less than one Or equal to one Info will not be showing.
<?php
$tax = 'studio';
$types = get_terms( array(
'post_per_page' => 1,
)
);
foreach($types as $type) { ?>
<?php
$term_link = get_term_link( $type );
$image = get_field('studio_avatar', 'studio_' . $type->term_id . '' );
if ( has_term( $type->term_id, 'studio')) {
echo '<a class="author author-avt inline" href="' . esc_url( $term_link ) . '">';
echo '<img class="si-user" src="' . $image['url'] . '" /> ';
echo '<span>'. $type->name . '</span>';
echo '</a>';
echo '<span class="posted-on inline">';
echo '<i class="si-clock"></i><time>'. $post_date = get_the_date("j F Y") .'</time>';
echo '</span>';
}
?>
<?php } ?>
I have a custom post type and have a shortcode to make it appear, but I want if there is no post in that custom post, then it should written "No post"...
Here is code. All things are working on but it don't show "No post" when it's empty.
add_shortcode( 'upme_recent_scholarships', 'upme_recent_scholarships',10,2);
function upme_recent_scholarships($display,$id){
global $upme,$upme_options;
$current_options = $upme_options->upme_settings;
$id = $upme->current_view_profile_id;
$post_limit = $current_options['maximum_allowed_posts'];
$feature_image_status = $current_options['show_feature_image_posts'];
$args = array(
'author' => $id,
'order' => 'DESC',
'post_type' => 'scholarship',
'orderby' => 'date',
'posts_per_page' => $post_limit,
);
$query = new WP_Query($args);
if ($query->have_posts()) {
$display .= '<div class="upme-main upme-main-">';
// Display different views based on posts with featured images or posts as text
if ('1' == $feature_image_status) {
while ($query->have_posts()) : $query->the_post();
$image_attributes = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail');
$image_src = upme_url . 'img/default-post-thumbnail.png';
if (is_array($image_attributes) && ('' != $image_attributes[0])) {
$image_src = $image_attributes[0];
}
$display .= '<div class="upme-field upme-post-list-field">
<div class="upme-post-feature-image"><img src="' . $image_src . '" /></div>
<div class="upme-post-feature-value"><span>' . get_the_title() . '</span></div>
</div>';
endwhile;
wp_reset_query();
} else {
while ($query->have_posts()) : $query->the_post();
$display .= '<div class="upme-field ">
<div class="upme-post-field-type"><i class="upme-icon upme-icon-file-text"></i></div>
<div class="upme-post-field-value"><span>' . get_the_title() . '</span></div>
</div>';
endwhile;
wp_reset_query();
}
$display .= '</div>';
}
return $display;
}
Regards
try this modified script, Please check shortcode parameter, here I have updated correct method.
add_shortcode( 'upme_recent_scholarships', 'upme_recent_scholarships',10,2);
function upme_recent_scholarships($atts){
global $upme,$upme_options;
$current_options = $upme_options->upme_settings;
$display = $atts['display'];
$id = $atts['id'];
$id = $upme->current_view_profile_id;
$post_limit = $current_options['maximum_allowed_posts'];
$feature_image_status = $current_options['show_feature_image_posts'];
$args = array(
'author' => $id,
'order' => 'DESC',
'post_type' => 'scholarship',
'orderby' => 'date',
'posts_per_page' => $post_limit,
);
$query = new WP_Query($args);
if ($query->have_posts()) {
$display .= '<div class="upme-main upme-main-">';
// Display different views based on posts with featured images or posts as text
if ('1' == $feature_image_status) {
while ($query->have_posts()) : $query->the_post();
$image_attributes = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail');
$image_src = upme_url . 'img/default-post-thumbnail.png';
if (is_array($image_attributes) && ('' != $image_attributes[0])) {
$image_src = $image_attributes[0];
}
$display .= '<div class="upme-field upme-post-list-field">
<div class="upme-post-feature-image"><img src="' . $image_src . '" /></div>
<div class="upme-post-feature-value"><span>' . get_the_title() . '</span></div>
</div>';
endwhile;
wp_reset_query();
} else {
while ($query->have_posts()) : $query->the_post();
$display .= '<div class="upme-field ">
<div class="upme-post-field-type"><i class="upme-icon upme-icon-file-text"></i></div>
<div class="upme-post-field-value"><span>' . get_the_title() . '</span></div>
</div>';
endwhile;
wp_reset_query();
}
$display .= '</div>';
}else{
/*need to add this*/
$display .= '<p>Sorry!, No Record Found</p>';
}
return $display;
}
Please Try This Code
add_shortcode( 'upme_recent_scholarships', 'upme_recent_scholarships',10,2);
function upme_recent_scholarships($display,$id){
global $upme,$upme_options;
$current_options = $upme_options->upme_settings;
$id = $upme->current_view_profile_id;
$post_limit = $current_options['maximum_allowed_posts'];
$feature_image_status = $current_options['show_feature_image_posts'];
$args = array(
'author' => $id,
'order' => 'DESC',
'post_type' => 'scholarship',
'orderby' => 'date',
'posts_per_page' => $post_limit,
);
$query = new WP_Query($args);
if ($query->have_posts()) {
$display .= '<div class="upme-main upme-main-">';
// Display different views based on posts with featured images or posts as text
if ('1' == $feature_image_status) {
while ($query->have_posts()) : $query->the_post();
$image_attributes = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail');
$image_src = upme_url . 'img/default-post-thumbnail.png';
if (is_array($image_attributes) && ('' != $image_attributes[0])) {
$image_src = $image_attributes[0];
}
$display .= '<div class="upme-field upme-post-list-field">
<div class="upme-post-feature-image"><img src="' . $image_src . '" /></div>
<div class="upme-post-feature-value"><span>' . get_the_title() . '</span></div>
</div>';
endwhile;
wp_reset_query();
} else {
while ($query->have_posts()) : $query->the_post();
$display .= '<div class="upme-field ">
<div class="upme-post-field-type"><i class="upme-icon upme-icon-file-text"></i></div>
<div class="upme-post-field-value"><span>' . get_the_title() . '</span></div>
</div>';
endwhile;
wp_reset_query();
}
$display .= '</div>';
}else{
$display .= '<p>No Post Found</p>';
}
wp_reset_query();
return $display;
}
I'm working on a shortcode and I'd like to target the most recent "Event". Targeting this most recent "Event" I'd like to do two things.
Being able to apply a specific class to it so I can style it differently (I don't want to use first-child).
Add the_excerpt() to it.
Currently the shortcode pulls the most recent 4 "Events". So the most recent would need what I mentioned above. I'd imagine it would have to do something with "count" but I'm not entirely sure, still trying to learn this stuff.
add_shortcode( 'show_events', 'events_query' );
function events_query() {
$args = array(
'posts_per_page' => 4,
'category_name' => 'events',
);
$events_query = new WP_Query( $args );
if ( $events_query->have_posts() ) :
$html_out = '<article>';
while ( $events_query->have_posts() ) :
$events_query->the_post();
// Do stuff with each post here
$html_out .= '<div class="events-item"><div class="meta-date">' . Date('F j, Y') . '</div><div class="meta-info"><div class="meta-title"><h4>' . get_the_title() . '</h4></div>/div></div>';
endwhile;
$html_out .= '</article>';
else : // No results
$html_out = 'No Events Found.';
endif;
wp_reset_query();
return $html_out;
}
EDIT
Updated using code from an answer:
add_shortcode( 'show_events', 'events_query' );
function events_query() {
$args = array(
'posts_per_page' => 4,
'category_name' => 'events',
);
$events_query = new WP_Query( $args );
if ( $events_query->have_posts() ) :
$html_out = '<article>';
$counter = 0;
$event_class = 'events-item';
while ( $events_query->have_posts() ) :
if ( $counter == 0 ) {
$event_class = 'events-item most-recent';
}
$events_query->the_post();
// Do stuff with each post here
$html_out .= '<div class="' . $event_class . '"><div class="meta-date">' . Date('F j, Y') . '</div><div class="meta-info"><div class="meta-title"><h5>' . get_the_title() . '</h5></div></div>';
if ( $counter == 0 ) {
$html_out .= '<div class="meta-info">' . get_the_excerpt() . '</div>';
}
$html_out .= '</div>';
$counter++;
endwhile;
$html_out .= '</article>';
else : // No results
$html_out = 'No Events Found.';
endif;
wp_reset_query();
return $html_out;
}
Hey Darren it's me again :)
You can use additional var $counter to check if it's the first post
Here is the code
add_shortcode( 'show_events', 'events_query' );
function events_query() {
$args = array(
'posts_per_page' => 4,
'category_name' => 'events',
);
$events_query = new WP_Query( $args );
if ( $events_query->have_posts() ) :
$html_out = '<article>';
$counter = 0;
while ( $events_query->have_posts() ) :
$event_class = 'events-item';
if ( $counter == 0 ) {
$event_class = 'events-item first-event, additiona-classes';
}
$events_query->the_post();
// Do stuff with each post here
$html_out .= '<div class="' . $event_class . '"><div class="meta-date">' . Date('F j, Y') . '</div><div class="meta-info"><div class="meta-title"><h4>' . get_the_title() . '</h4></div>/div>';
if ( $counter == 0 ) {
$html_out .= '<div class="meta-info">' . $post->post_excerpt . '</div>';
}
$html_out .= '</div>';
$counter++;
endwhile;
$html_out .= '</article>';
else : // No results
$html_out = 'No Events Found.';
endif;
wp_reset_query();
return $html_out;
}
Hello Dear please use the if codition
add_shortcode( 'show_events', 'events_query' );
function events_query() {
$args = array(
'posts_per_page' => 4,
'category_name' => 'events',
);
$events_query = new WP_Query( $args );
if ( $events_query->have_posts() ) :
$html_out = '<article>';
$i=1;
while ( $events_query->have_posts() ) :
$events_query->the_post();
if($i==1){
$class = "new_class";
$excerpt = the_excerpt();
}
$html_out .= '<div class="events-item'.$class."><div class="meta-date">' . Date('F j, Y') . '</div><div class="meta-info"><div class="meta-title"><h4>' . get_the_title() . '</h4>
<p>'.$excerpt.'</p></div></div></div>';
$i++;
endwhile;
$html_out .= '</article>';
else : // No results
$html_out = 'No Events Found.';
endif;
wp_reset_query();
return $html_out;
}
I have a shortcode in WordPress which shows the last post by author, the problem is that I want to exclude a concrete author(ID=13). The shortcode is the next:
function latest_posts_c( $array ) {
global $post;
$defaults = array(
'show' => 3,
'excerpt' => 'false',
'post_type' => 'post',
);
extract( shortcode_atts( $defaults, $array ) );
$args = array(
'posts_per_page' => $show,
'post_type' => $post_type,
);
// Gets posts form database
$query = new WP_Query( $args );
// Displays posts if available
if( $query ) {
$i = 0;
while ( $query->have_posts()) : $query->the_post();
if ($i == 0)
$html = '<div class="column dt-sc-one-third first">';
else
$html .= '<div class="column dt-sc-one-third">';
$html .= '<article id="post-'.get_the_ID().'" class="'.implode(' ', get_post_class('blog-entry')).'">';
$html .= '<div class="blog-entry-inner">';
$html .= '<div class="entry-meta">';
$html .= ' ';
$html .= ' <div class="date">';
$html .= ' <p>'.get_the_date('M').' '.get_the_date('d').' <span>'. get_the_date('Y') .'</span> </p>';
$html .= ' </div>';
$html .= '</div><!-- .entry-meta -->';
if( has_post_thumbnail() ):
$html .= '<div class="entry-thumb">';
$html .= ' '.get_the_post_thumbnail(get_the_ID(), 'medium').'';
$html .= '</div><!-- .entry-thumb -->';
endif;
$html .= '<div class="entry-details">';
if(is_sticky()):
$html .= ' <div class="featured-post"> <span class="fa fa-trophy"> </span> Destacado</div>';
endif;
$html .= ' <div class="entry-title">';
$html .= ' <h4>'.get_the_title().'</h4>';
$html .= ' </div>';
$html .= ' <div class="entry-metadata">';
$html .= ' <p class="author">';
$html .= '<span class="fa fa-user"> </span>';
$html .= ' '.get_the_author().'</p>';
$categories = 0;/*get_the_category();*/
$separator = ', ';
$output = '';
if($categories){
$j = 0;
foreach($categories as $category) {
$output .= ''.$category->cat_name.'';
$j++;
if ($j < count($categories)) $output .= $separator;
}
$html .= ' <p class="categories"><span class="fa fa-folder-open"> </span>'.$output.'</p>';
}
$html .= ' </div><!-- .entry-metadata-->';
$html .= ' <div class="entry-body">';
$html .= ' '.dttheme_excerpt(50);
$html .= ' <p><a href="'.get_permalink().'" title="'.get_the_title().'" class="dt-sc-button small read-more">';
$html .= ' Leer más <span class="fa fa-angle-double-right"> </span></a></p>';
$html .= ' </div>';
$html .= '</div><!-- .entry-details -->';
$html .= '</div><!-- .blog-entry-inner-->';
$html .= '</article>';
$html .= '</div>';
$i++;
endwhile;
}
$html .= '<div class="dt-sc-clear"></div>';
// Resets Post Data
wp_reset_postdata();
// Returns the results
return $html;
}
add_shortcode('latestposts_c', 'latest_posts_c');
I added an If to block the code if there is a post from author ID=13, but the problem is that the shortcode doesn't show anything. I'm also using "authors_in" to allow certain authors ID inside the array $args and $default but nothing....Any idea?
Try this:
$args = array(
'posts_per_page' => $show,
'post_type' => $post_type,
'author' => '-13'
);
The author parameter can be set to include/exclude specific authors.
Reference http://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters