• Resolved VNConsort

    (@vnconsort)


    I am trying to get one of the sliders to show random posts. I thought I could make a page/post that queried 4 random posts in it and use that as the slider but it wouldn’t show the posts at all.

    So I saw I could overlay a shortcode on the page instead. So I made a function of the exact same query and gave it a shortcode. I used that shortcode on an empty page and put that page into my slider. It showed the 4 random posts but it broke the slider because the content was not actually in the slide.

    I was wondering how to fix this?

    The function I am using is this:

    function randompost_sc(){
    	$random = new WP_Query (array(
    		'orderby' => 'rand',
    		'post_type' => array('animereviews', 'animerants', 'gamereviews', 'gamerants'),
    		'posts_per_page' => 4) );
    
    		if ($random->have_posts()) :
    			while ($random->have_posts()) : $random->the_post(); ?>
    		<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(64,36)); ?></a>
    		<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    		<?php wp_reset_postdata(); ?>
    		<?php endwhile ?>
    		<?php endif;
    }
    add_shortcode( 'randomshort', 'randompost_sc');

    The result looks like this now:

    https://45.55.62.181/wordpress/

    Is this even doable? Thanks

    https://www.ads-software.com/plugins/smooth-slider/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter VNConsort

    (@vnconsort)

    Actually I tried out another widget but it was for Recent posts and used it’s short code. It didn’t break the slider, but fit nicely inside instead. So it is how I’ve written my function. Unfortunately, I can’t find a good plugin that will work to get random posts from multiple post types so I was forced to make my own query instead.

    I’ll put it as resolved for now since it doesn’t have to do with the slider.

    If you think of anything I can do to help get this shortcode/function/query to show up in the slider, I would definitely appreciate any advice! Thanks

    Hello @vnconsort

    Glad that it worked for you and thanks for writing back.

    Let us know in case of any further issues or suggestions.

    Thank you,

    Best Regards,
    Ravi.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Shortcode and Sliders’ is closed to new replies.