So I have looked at every possible solution and read through most similar questions, I just couldn't find the error inside my shortcode for a post query in wordpress.
This is the complete Code:
function common_cats(){
global $posts;
$parts = array();
$title = get_the_title();
$teile = explode("|", $title);
$args = array(
'posts_per_page' => -1,
'no_found_rows' => true,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true,
'tag_slug__in' => $teile
);
$posts = get_posts($args);
$output = "<ul>";
if( $posts->have_posts() ){
while($posts->have_posts()): $post->the_post();
$output .= "<a href=\"".get_permalink()."\">";
$output .= "<li>".get_the_title(). "<br>";
$output .= "<img class=\"sbf-background\" src=\"".get_the_post_thumbnail_url()."\"></img>";
$output .= get_the_excerpt()."</li>"."</a><!-- ends here -->";
endwhile;
}
$output .= "</ul>";
return $output;
}
add_shortcode('commoncats', 'common_cats');
Through testing I could boil the problem down to this part of the code:
if( $posts->have_posts() ){
while($posts->have_posts()): $post->the_post();
$output .= "<a href=\"".get_permalink()."\">";
$output .= "<li>".get_the_title(). "<br>";
$output .= "<img class=\"sbf-background\" src=\"".get_the_post_thumbnail_url()."\"></img>";
$output .= get_the_excerpt()."</li>"."</a><!-- ends here -->";
endwhile;
wp_reset_postdata();
}
But looking at it I just don't see whats causing my site to break additionally the code will work if I comment out these lines
#if( $posts->have_posts() ){
# while($posts->have_posts()): $post->the_post();
$output .= "<a href=\"".get_permalink()."\">";
$output .= "<li>".get_the_title(). "<br>";
$output .= "<img class=\"sbf-background\" src=\"".get_the_post_thumbnail_url()."\"></img>";
$output .= get_the_excerpt()."</li>"."</a><!-- ends here -->";
# endwhile;
wp_reset_postdata();
#}
Related
i have created shortcode to show all testimonial on one page.
i have created custom post type to add each testimonial.
can anybody tell me how to show all testimonial on single page .
this code is used to create sshortcode
below i m mentioning code :-
function fn_testimonials_block()
{
$args = array(
'post_type'=> 'testimonials',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
$pages = $the_query->posts;
//echo "<pre>";
//print_r($pages);
//echo "</pre>";
//exit;
$output = '';
$count = 1;
foreach($pages as $page) {
//-----------------
$author = $page->post_title;
$testimonial = $page->post_content;
$page_url = get_page_link( $page->ID );
$author_image = get_the_post_thumbnail_url( $page->ID, 'thumbnail' );
if ($count%2 == 1)
{
$output .= '<div>';
}
$output .= '<div>
<p>'.$testimonial.'<br>
<img src="'.$author_image .'"> <span>'.$author.', </span></p>
</div>';
if ($count%2 == 0)
{
$output .= '</div>';
}
$count++;
}
if ($count%4 != 1){
$output .= '</div>';
}
return $output;
}
I think you will have to pass 'posts_per_page' key in $args array. So your $args array will look like this
$args = array(
'post_type'=> 'testimonials',
'order' => 'ASC',
'posts_per_page' => '-1'
);
I am not sure about the $count variable you are using. I suppose you are trying to implement logic for pagination there but since your question is about showing all testimonials on single page, I won't get into that part.
Making changes in $args will return all testimonials objects.
i have this code for shortcode
function custom_query_shortcode( $atts ) {
// EXAMPLE USAGE:
// [postcat name="Lorem"]
// Defaults
$atts = shortcode_atts(array(
'name' => ''
), $atts, 'postcat');
// Reset and setup variables
$output = '';
$title = '';
$link = '';
$featImg = '';
// the loop
$argsSC = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page'=> -1,
'category_name' => $atts['name']
);
$querySC = new WP_Query( $argsSC );
if ( $querySC->have_posts() ) {
$output .= '<div class="row" style="margin-top: 30px;">';
while ( $querySC->have_posts() ) {
$querySC->the_post();
$title = get_the_title(get_the_ID());
$link = get_permalink(get_the_ID());
$featImg = ( has_post_thumbnail() ? get_the_post_thumbnail_url(get_the_ID(), 'large') : get_template_directory_uri() . '/imgs/No_Image_Available.gif' );
$id = get_the_ID(get_the_ID());
$chartL = get_field( 'chart' );
$storyL = get_field( 'story' );
// output all findings - CUSTOMIZE TO YOUR LIKING
//$output .= "<div><img src='$featImg'><a href='$link'>$title</a></div>";
$output .= '<article id="post-'.$id.'" class="csseco-tssf-t col-12 col-md-6">';
$output .= ' <div class="standard-featured bg-img-el" style="background-image: url('.$featImg.')">';
$output .= ' <a class="standard-featured-link" href="'.$link.'"></a>';
$output .= ' <div class="cat-tssf">';
if ( !empty($chartL['url']) ) {
$output .= ' Charts';
}
if ( !empty($storyL['url']) ) {
$output .= ' The Story';
}
$output .= ' </div>';
$output .= ' <h3 class="ttl-tssf">';
$output .= ' <span class="dtable">';
$output .= ' <span class="dtable-cell">';
$output .= ' <a href="'.$link.'" rel="bookmark">';
$output .= $title;
$output .= ' </a>';
$output .= ' </span>';
$output .= ' </span>';
$output .= ' </h3>';
$output .= ' </div>';
$output .= '</article>';
}
$output .= '</div>';
} else {
$output .= "nothing found.";
}
return $output;
wp_reset_query();
wp_reset_postdata();
}
add_shortcode("postcat", "custom_query_shortcode");
In the backend, when i press "Add new category" to make a new category(duh!) does not work. The category its added but the page does not refresh. I'm pretty sure it's something with this code, but i really dont know what... if i dont include this file in functions.php categories and tags works perfectly...
Please help and thanks in advance!
Can't comment, so I'll post it as an answer.
Please look at:
return $output;
wp_reset_query();
wp_reset_postdata();
The code after the return will not be executed I believe. So change it to:
wp_reset_query();
wp_reset_postdata();
return $output;
Maybe this will solve your issue?
I am creating a shortcode to display the most recent posts in the homepage, however I cannot get the pagination working.
When you click on 'older posts' the page refreshes but the same 2 original posts are displayed.
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;
$output .= '<div class="post-nav">';
$output .= '<div class="prev-page">' . get_previous_posts_link( "« Newer Entries" ) . '</div>';
$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');
You need to call paginate function.Try following code:
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');
WP_Query with pagination inside a shortcode
Here I will show you how to create a [shortcode] containing a custom new WP_Query with pagination links.
To create a [shortcode] with a new WP_Query post loop with pagination links built in to turn any is_singular() page or single post into a custom archive based on the WP_Query arguments you set. Lets call it [wp_query_pagination_inside_shortcode], so we add with the following add_shortcode() in functions.php.
Or in my case I create separate /functions/ folder for files to stay organized with the appropriates names and have a require_once include file structure.
root/functions.php ->
require_once 'functions/shortcode-wp-query-pagination-inside-shortcode.php';
root/functions/shortcode-wp-query-pagination-inside-shortcode.php->
add_shortcode('wp_query_pagination_inside_shortcode', 'my_shortcode_function_tag');
if (!function_exists('my_shortcode_function_tag')) {
function my_shortcode_function_tag($atts)
{
ob_start(); // Turn output buffering on, helps with complicated template and theme structures
// Note: https://developer.wordpress.org/reference/functions/get_query_var/#used-by
// Custom query_vars translates to Getting an "archive" on any current page that contains a query with corresponding pagination number in the url
// example.com/page-shortcodes-on/page/2/ ...etc see below how to manipulate this
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$atts = shortcode_atts(
array(
'post_type' => 'post',
// Set attributes here you want in your shortcode
// like [shortcode post_types="post"]
),
$atts
);
// set up the default args you wont change and the ones you will by accessing the value of $atts passed into the shortcode function from your WYSIWYG content editor
$args = array(
'posts_per_page' => 3,
'paged' => $paged, // Important to receive page data
'post_type' => $atts['post_type'], // This is how you get the values of your shortcode attributes passed in
'orderby' => 'date',
'order' => 'DESC',
);
$the_query = new WP_Query($args);
// Da loop
// match your themes loop structure this below is just boiler plate stuff ignore or use your choice its not important how you create the post loop, just do it
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
get_template_part('template-parts/loop');
} //end while
} // endif
$big = 999999999; // need an unlikely integer for how many pages are possible
// I've tested example.com/page-shortcodes-on/page/999999999999999999/ and the custom query_var still works
echo paginate_links(
array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))), // referrence the url
'format' => '?paged=%#%', // used for replacing the page number
'current' => max(1, get_query_var('paged')), // grabs the page data
'total' => $the_query->max_num_pages //$the_query is your custom query
)
);
return ob_get_clean(); // Silently discard the buffer contents
wp_reset_query(); // Lets go back to the main_query() after returning our buffered content
}
}
In a WYSIWYG Content editor in pages or posts content editor
[wp_query_pagination_inside_shortcode post_type="posts"]
URL structure for this query_var 'paged'
example.com/page-shortcode-is-on/page/2/
You will need to stylize the pagination links a bit to be acceptable but this should give you a highly useful tool to build custom WP_Query loops with multiple different post_types from anywhere in your site through your WYSIWYG editor. Add more attributes to really make this thing valuable.
// Ignore below, these are just possible search queries for this issue
WP_Query pagination inside a shortcode / add_shortcode with pagination / shortcode with custom attributes / Paginated custom post type wp_query shortcode / custom post type wp_query archive posts per page / page / pagination wont work
I need to change some code from the WP-Bootstrap Wordpress Files.
In their code they remove the standard gallery code and replace it with their own which works fine accept it doesn't properly account for Rows.
function gallery_shortcode_tbs($attr) {
global $post, $wp_locale;
$output = "";
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
$output = '<div class="row-fluid"><ul class="thumbnails">';
foreach ( $attachments as $attachment ) {
$output .= '<li class="span3">';
$att_title = apply_filters( 'the_title' , $attachment->post_title );
$output .= wp_get_attachment_link( $attachment->ID , 'thumbnail', true );
$output .= '</li>';
}
$output .= '</ul></div>';
}
return $output;
}
could anyone advise on how to edit the above code to insert html code to close the row and start another after the 3rd item.. or any number i choose?
thank you in advance.
R
Take a look at the default WP gallery shortcode source, where a break is inserted after the specified number of columns, on lines 837-838:
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />';
Hi I am working within wordpress and have so far been able to display different sizes for my post loop but I keep getting only 1 post in each column. I have tried adding the multiple post loop into our php and have also set the css for the size but still only 1 post per column.
/* returns carousel html and js embed code */
function get_carousel($id, $get_first_post)
{
$id = intval($id);
if ($id <= 0)
die ('tc-oops7');
global $wpdb;
$carousels_table = $wpdb->prefix . 'touchcarousels';
$slider_row = $wpdb->get_row("SELECT * FROM $carousels_table WHERE id = $id", ARRAY_A);
if(!$slider_row) {
return "<p>Oops, TouchCarousel with ID $id not found.</p>";
}
$carousel_html = '';
$skin_name = $slider_row['skin'];
$css_classes = $slider_row['css_classes'];
$carousel_html .= "<div id=\"touchcarousel-$id\" class=\"touchcarousel $skin_name $css_classes\" style=\"width:{$slider_row['width']}; height:{$slider_row['height']}; \">\n";
$carousel_html .= "\t<ul class=\"touchcarousel-container\">\n";
global $post;
$args = array(
'numberposts' => intval($slider_row['max_posts']),
'posts_per_page' => intval($slider_row['max_posts']),
'offset' => 0,
'cat' => $slider_row['post_categories'],
'orderby' => $slider_row['post_orderby'],
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => $slider_row['post_type'],
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish' );
$post_taxonomies_arr = (array)json_decode (stripslashes($slider_row['post_categories']));
$taxonomies_query_arr = array();
$taxonomies_query_arr['relation'] = $slider_row['post_relation'];
$count = 0;
foreach ($post_taxonomies_arr as $key => $taxonomy ) {
$taxonomies_query_arr[$count]['taxonomy'] = $key;
$taxonomies_query_arr[$count]['terms'] = $taxonomy;
$taxonomies_query_arr[$count]['field'] = 'slug';
$count++;
}
$args['tax_query'] = $taxonomies_query_arr;
$the_query = new WP_Query( $args );
if($get_first_post) {
$the_query->have_posts();
$the_query->the_post();
$this->current_loop_post = $post;
return $post;
}
if($the_query->have_posts()) {
$layout_code = stripslashes($slider_row['layout_code']);
while ($the_query->have_posts()) : $the_query->the_post();$this->current_loop_post = $post;
$do_not_duplicate[] = $post->ID;
$carousel_html .= "\t\t<li class=\"touchcarousel-item\">\n";
$carousel_html .= preg_replace_callback ("/\[tco.*?\](.*?)\[\/tco\]/", array($this, 'format_variables'), $layout_code);
$carousel_html .= "\t\t</li>\n";
endwhile;
$carousel_html .= "\t</ul>\n";
$carousel_html .= "</div>";
$slider_settings = stripslashes($slider_row['js_settings']);
$carousel_js = "";
$carousel_js .= "<script type=\"text/javascript\">\n";
$carousel_js .= "jQuery(document).ready(function($) {";
$carousel_js .= "$(\"#touchcarousel-$id\").touchCarousel(";
$carousel_js .= $slider_settings;
$carousel_js .= ");";
$carousel_js .= "});\n";
$carousel_js .= "</script>";
$carousel_html .= $carousel_js;
} else {
$carousel_html = "<p class=\"tc-posts-not-found\">". __('TouchCarousel Warning: No posts found with selected settings.', 'touchcarousel') ."</p>";
}
wp_reset_postdata();
wp_reset_query();
return stripslashes($carousel_html);
}
The example location can be viewed at http://demo.campaignready.com
So far I have tried calling the posts and divs by their ids and classes but it is still only showing 1 post per column after running the "the_query"