- This topic has 13 replies, 3 voices, and was last updated 4 years, 11 months ago by Tom.
-
AuthorPosts
-
March 3, 2019 at 8:24 am #827459epickenyan
Hi. I saw some web hosts like WPEngine have an inbuilt system that caches random posts for some time, e.g. 10 minutes, so as to improve site speed. Can this be achieved with WP show Posts plugin? I want to show random posts at intervals in the sidebar.
March 3, 2019 at 10:06 am #827574LeoStaffCustomer SupportHi there,
Unfortunately WP Show Posts doesn’t this option currently.
I believe it’s on the radar though.
For more questions regarding to WP Show Posts, please use its own forum as it’s a separate plugin:
https://wpshowposts.com/support/area/pro-support/
https://wordpress.org/support/plugin/wp-show-posts/Thanks!
March 22, 2019 at 1:09 am #846288epickenyanI used this solution and then hooked it after sidebar. Working perfectly
March 22, 2019 at 8:34 am #846695LeoStaffCustomer SupportAwesome!
May 2, 2019 at 5:36 pm #887240epickenyan1. Hi. Is there a way to hook such a code provided in the post in this comment to a footer widget? I want to show a subscription box in one widget and the related posts in the second widget. The PHP code does not work on the front end of WordPress.
2. Since the above two widgets are the only ones I have in the entire site, would you recommend I put them in the Sidebar or remove sidebar altogether and put them in the footer? Someone in the Facebook group recommended I remove the sidebar.May 3, 2019 at 8:29 am #888057TomLead DeveloperLead DeveloperHi there,
1. You would have to add it to a shortcode: https://docs.generatepress.com/article/creating-a-shortcode/
2. It depends on your preference/design. A sidebar widget might be more visible than a footer widget, but it might not work with your design.
May 3, 2019 at 12:09 pm #888237epickenyan1. I have asked over at StackExchange how to get the function to create the shortcode. But if you know how, you can assist.
2. Thanks. I think the footer works best. I can reduce the whole site width to 1080px and the single post width to 960px.May 3, 2019 at 2:38 pm #888355TomLead DeveloperLead DeveloperYou could add your code into the shortcode function we provide in that article. Then you should be able to use the shortcode and it will output the posts.
May 4, 2019 at 12:13 am #888546epickenyanI did, but I was getting some errors (syntax etc).
May 4, 2019 at 7:45 am #888945TomLead DeveloperLead DeveloperCan you share the error you were getting and the code you were adding?
May 4, 2019 at 1:41 pm #889146epickenyanI tried this code
add_shortcode( 'my_shortcode', 'tu_my_shortcode' ); function tu_my_shortcode() { ob_start(); <?php // Get any existing copy of our transient data if ( false === ( $special_query_results = get_transient( 'special_query_results' ) ) ) { // It wasn't there, so regenerate the data and save the transient $randargs = array('orderby' => 'rand', 'numberposts' => 20); $special_query_results = get_posts($randargs); set_transient( 'special_query_results', $special_query_results, 60*60*12 ); } // Use the data like you would have normally... $randomposts = get_transient( 'special_query_results' ); $randkey = array_rand( $randomposts, 6 ); $sixposts[0] = $randomposts[$randkey[0]]; $sixposts[1] = $randomposts[$randkey[1]]; $sixposts[2] = $randomposts[$randkey[2]]; $sixposts[3] = $randomposts[$randkey[3]]; $sixposts[4] = $randomposts[$randkey[4]]; $sixposts[5] = $randomposts[$randkey[5]]; global $post; foreach( $sixposts as $post ) : setup_postdata($post); ?> <div class="gridcontainer"> <div class="gridthumb"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div> <div class="gridcontent"> <div class="gridtext"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div> </div> </div> <?php endforeach; ?> $ret = ob_get_contents(); ob_end_clean(); return $ret; }
If I remove the opening php in the code, three errors at the top disappear, but I have an error at the bottom where the closing ‘}’ is.
May 4, 2019 at 4:10 pm #889201TomLead DeveloperLead DeveloperTry this:
add_shortcode( 'my_shortcode', 'tu_my_shortcode' ); function tu_my_shortcode() { ob_start(); // Get any existing copy of our transient data if ( false === ( $special_query_results = get_transient( 'special_query_results' ) ) ) { // It wasn't there, so regenerate the data and save the transient $randargs = array('orderby' => 'rand', 'numberposts' => 20); $special_query_results = get_posts($randargs); set_transient( 'special_query_results', $special_query_results, 60*60*12 ); } // Use the data like you would have normally... $randomposts = get_transient( 'special_query_results' ); $randkey = array_rand( $randomposts, 6 ); $sixposts[0] = $randomposts[$randkey[0]]; $sixposts[1] = $randomposts[$randkey[1]]; $sixposts[2] = $randomposts[$randkey[2]]; $sixposts[3] = $randomposts[$randkey[3]]; $sixposts[4] = $randomposts[$randkey[4]]; $sixposts[5] = $randomposts[$randkey[5]]; global $post; foreach( $sixposts as $post ) : setup_postdata($post); ?> <div class="gridcontainer"> <div class="gridthumb"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div> <div class="gridcontent"> <div class="gridtext"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div> </div> </div> <?php endforeach; $ret = ob_get_contents(); ob_end_clean(); return $ret; }
You’ll want to replace
my_shortcode
with the shortcode name you want to use in your content.May 4, 2019 at 5:53 pm #889228epickenyanThanks a lot, Tom. It worked! I decided to just keep the footer and remove the sidebar.
May 5, 2019 at 7:42 am #889714TomLead DeveloperLead DeveloperGlad I could help 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.