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' );
Related
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;
}
My loop returns only one post instead all. I going through it 4th time and i do not see any reason why it act like that. Thank you for all your help.
localStorage has been cleaned, cache also - and yeah i have more then one post published in this category :)
function rtf_custom_grid( ) {
$args = array(
'post_type' => 'apartamenty',
'post_per_page' => -1,
'nopaging' => true,
'order' => 'date',
'orderby' => 'DESC'
);
$rtf_query = new WP_Query ( $args );
while($rtf_query->have_posts() ) : $rtf_query->the_post();
$cena_1 = get_field('cena_1');
$cena_2 = get_field('cena_2');
$short_dec = get_field('short_desc');
$output = '<div class="single-apartament">';
if ( has_post_thumbnail() ) :
$output .= '<div class="rtf-apartament-thumbnail">';
$output .= '' . get_the_post_thumbnail(get_the_id(), 'large') . '</div>';
endif;
$output .= '<div class="rtf-apartament-content">';
$output .= '' . get_the_title() . '';
$output .= '<div class="rtf-apartament-excerpt">' . $short_dec . '</div>';
$output .= '<div class="rtf-apartament-prize">';
$output .= '<span>' . $cena_1 . ' / ' . $cena_2 . '</span></div>';
$output .= 'Zobacz</div></div></div>';
endwhile;
wp_reset_query();
return $output;
}
add_shortcode('apartamenty', 'rtf_custom_grid');
$output = ' <div class="single-apartament"> ';
the above line is overwriting the previous content in the variable. So its showing only the last post.
Solution :
$output.= '<div class="single-apartament">';
This will append the html code into the variable instead of overwriting it.
Hope it works for you
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');
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'm working on a site that has custom author pages and on the author pages it has a recent author posts widget which displays other posts by that author. This site has multiple authors so it is different for every post and I haven't found a plugin that does this. I'm trying to display the post thumbnail, title, and category name.
I'm using a function that is displaying the title and the thumbnail, but it doesn't have the category . I tried to add the category in with: the_category(' ','multiple',$authors_post->ID) unfortunately it displays all of the categories in the first li. Instead of for each post.
Here is what I'm working with:
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 = '<ul>';
foreach ( $authors_posts as $authors_post ) {
$output .= '<li>' . get_the_post_thumbnail( $authors_post->ID, 'xsmall-thumb' )
. '<p>' . the_category(' ','multiple',$authors_post->ID)
. '</p> <a href="' . get_permalink( $authors_post->ID )
. '">' . apply_filters( 'the_title', $authors_post->post_title,
$authors_post->ID ) . '</a></li>';
}
$output .= '</ul>';
return $output;
}
Any help would be greatly appreciated,
Thanks!
Please try this code,
<?php
$cat=1;
$yourcat = get_category($cat);
if ($yourcat)
{
echo '<h2>' . $yourcat->name . '</h2>';
}
?>
To get category name,
try some thing like
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 = '<ul>';
foreach ( $authors_posts as $authors_post ) {
$category = get_the_category($authors_post->ID);
foreach($category as $c){
$cat=$c->cat_name.' '.$cat;
}
$output .= '<li>' . get_the_post_thumbnail( $authors_post->ID, 'xsmall-thumb' ) . '<p>' . $cat . '</p> ' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</li>';
}
$output .= '</ul>';
return $output;
}
hope this work for you..