In Wordpress, how can I display a separate random post of a custom post type in a side widget?

I created a custom post type - reviews - for the WP site I'm working on. I want to display one random review in the sidebar - without the possibility of using a plugin. Do I need to create a text widget with the correct email request? If so, what does it look like?

Many thanks,

Cynthia

+6
source share
1 answer

If you want, you can directly paste the following code snippet into your sidebar.php where you want to show Testimonials (make sure it's testimonials/Testimonials )

 <?php $args = array( 'post_type'=>'testimonials', 'orderby'=>'rand', 'posts_per_page'=>'1' ); $testimonials=new WP_Query($args); while ($testimonials->have_posts()) : $testimonials->the_post(); ?> <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> <p><?php the_excerpt(); // or the_content(); ?></p> <?php endwhile; wp_reset_postdata(); ?> 
+13
source

Source: https://habr.com/ru/post/921076/


All Articles