-
AuthorPosts
-
May 5, 2017 at 11:28 pm #788667
I am using the enfold theme for the first time, and I really love this theme.
I see that this theme comes with a feature to display the testimonials. I really like the way the testimonials are being displayed in the default grid. But they are all static content.On my website though, I want to have testimonials as a custom post type. I want to display every individual testimonial on its own page when I click on the person’s name, or picture or a Read More link.
I created a custom post type called “Testimonials” using “CPT UI” plugin.
In order to use exactly the same markup and styling that comes with the default testimonials in Enfold theme, I tried creating a shortcode for my custom post type “Testimonials” in function.php.
I want to dynamically pull 4 testimonials that belong to the category “Home”, and display them on the homepage using the shortcode.I wrote the following function in functions.php to create the shortcode:
/* SHORT CODE FOR TESTIMONIALS GRID ON HOME PAGE */ function home_testimonials_shortcode() { $loop = new WP_Query( array( 'post_type' => 'testimonial', 'category_name' => 'home', 'posts_per_page' => '4' ) ); if ( $loop->have_posts() ) { $output = ''; while ( $loop->have_posts() ) { $loop->the_post(); $post_thumbnail_id = get_post_thumbnail_id( $post_id ); $testimonial_permalink = get_permalink( $post_id ); $excerpt = get_the_excerpt( $post_id ); $testimonialTitle = get_the_title(); $output .= "[av_testimonial_single href=" . $testimonial_permalink . " src=". $post_thumbnail_id ." name=". $testimonialTitle ." subtitle='Product Owner' link='http://' linktext='']"; $output .= $excerpt . "... <a href=" . $testimonial_permalink . ">Read More</a>"; $output .= "[/av_testimonial_single]"; } echo do_shortcode("[av_testimonials style='grid' columns='2' grid_style='' interval='5' font_color='' custom_title='' custom_content='' admin_preview_bg='']". $output ."[/av_testimonials]"); } }; add_shortcode('testihome', 'home_testimonials_shortcode');
I see that the testimonials (from my custom post type) are now being dynamically pulled and are displaying perfectly fine on the home page.
For some reason though, I see all the Testimonials appear on top of the page.
I am putting my custom shortcode to display the testimonials on the bottom of my home page. I am putting my shortcode [testihome] in a “Text Block” element inside a “Full-Width Layout” element, on the bottom of my homepage. But for some reason, the testimonials are displayed on top of the page.
Is there something I am missing somewhere. Please help. ThanksMay 7, 2017 at 8:27 pm #789168Hey seedboxtech,
That is happening because the function is been executed and not returned, so it can be used on
You need to replace your do_shortcode echo, with a variable most probably and return that variable after.IT will fix your problem!
Best regards,
BasilisMay 20, 2017 at 8:02 am #796942Thank you for your reply. I see that instead of echo, if I use return It fixes the issue.
I have another issue though. I can modify the same function to display all the testimonials on a different page. All I have to do is to modify the query parameter and set it to ‘posts_per_page’ => ‘-1’. Doing this will make sure that the query is going to return all the custom post types (in my case – testimonials) and display them on the page. How do I make sure to display lets say 10 testimonials on a page, and if there are more than 10 testimonials (custom post types) then display a pagination.
How can I modify the function above to display the default pagination that comes with Enfold. Thanks
May 23, 2017 at 9:48 pm #798529Hi,
That would require a more advanced coding, to add the pagination of Avia inside the query.
That would need to better take a look at the existing shortcodes, how they are loading pagination ( example the Blog template ) so it can help you
solve your issues.Please let us know if there is anything else we can do for you.
Best regards,
Basilis -
AuthorPosts
- You must be logged in to reply to this topic.