I haven't worked much with wordpress, what I am trying to do is have a page that displays random posts.
It would be similar to my main page where the latest posts are shown but it would display random posts every time the page is refreshed.
If the main page is at http://example.com i want my random page to be at http://example.com/random
how to do it?
The orderby argument to get_posts accepts the value rand.
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
More info in the documentation: http://codex.wordpress.org/Template_Tags/get_posts#Random_posts
Enable permalinks for you wordpress installation by visiting http://example.com/wp-admin/options-permalink.php, If you would like the title of the post to be the last url segment you would select Custom Structure and set this value to %post_name%
Create a new page template in your current theme folder as described here: Creating Your Own Page Templates
Create a new page witht the title Random and select your newly created page template under Page Attributes in the pages edit screen.
Then in your page template you would do something like this as Filip suggested:
<ul>
<?php
$args = array('posts_per_page' => 5, 'orderby' => 'rand');
$rand_posts = get_posts($args);
foreach ($rand_posts as $post) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
Try This ...
1.First a create a custom page template. Name it as random post or a name of your choice!
2.Open the page and remove the default wp loop and Paste the code below
3.To change the no of post change the number ‘1’ to your choice!
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile;
endif; ?>
source: http://www.yengkokpam.com/displays-random-posts-in-a-page/
You can use this code to display random posts on your page.
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile;
endif; ?>
Related
I want to show comments of a post on single post page, but in that page I show the other post with same category too. when I use comments_template(); the comments of other post shown too. here is my code
<?php if(have_posts()): while(have_posts()): the_post(); ?>
// show the single post and get the category of post
<?php endwhile;endif; ?>
if(isset($cats_arr) && !empty($cats_arr)){
$args = array(
'category__in' => $cats_arr,
'posts_per_page'=>3,
'post__not_in' => array( $post->ID ) );
$the_query= new wp_query($args);
if($the_query->have_posts()): while($the_query->have_posts()):
$the_query->the_post(); ?>
//// shoe same category post title and image
<?php endwhile;endif ?>
<?php comments_template(); ?>
Can you help me find my problem?
Thanks a lot
add wp_reset_postdata(); to end of second query after if statment
I am looking to feed a custom post type onto a custom template. I basically want a few sub sections with custom (seperate) post types on the backend panel feed out onto a page, like a news, blog, testimonials kind of thing.
I think have the right idea but I believe I am going around this wrong. Please forgive me, as I do not normally work on wordpess. I basically want (let's say the news page) to feed all the posts in the custom post type "news". I have a blog page linked to a template that looks like so -
<?php
/*
Template Name:News*/
?>
<?php get_header(); ?>
<ul id="News">
<?php global $post; query_posts( 'post_type=newst&orderby=ID&order=desc' ); while (have_posts()) : the_post(); ?>
<li>
<div class="fea_del">
<h1><?php the_title(); ?></h1>
<p><?php the_field('post_content',$post->ID); ?></p>
<a <?php $p=get_permalink( $post->ID ); ?> href="<?php echo $p; ?>"class="entire_job">SEE ENTIRE ARTICLE</a>
</div>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
<?php get_footer(); ?>
I have the [display-posts] plugin in my blog page and just have [display-posts] in the body in hopes it would just feed in all the posts in blog. I've been banging my head against this for a while with no success, I don't work in wordpress much so I'm a bit in the dark here.
You have to pass the $args array to WP_Query, try:
$args = array( 'post_type' => 'news', 'order' => 'desc' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
//rest of you code
endwhile;
hope you guys can help me.
I have two very simple custom loops from one category: one sticky, one non-sticky. I want the sticky posts to only display on the first page, and not on the rest of the pages when I paginate. There must also be exactly 8 post (including the Sticky posts on page 1) on every page. All this is working fine with the code I got below.
The problem is this: Page one: 1 sticky & posts #15-9, pages two: post #7-1. WP skips one post (#8) on the second page because of the sticky post. Anyone got a solution for this? I would appreciate the help very much.
<!-- Sticky -->
<?php if ( $paged != True ): ?>
<?php $wp_query = new WP_Query(array('post__in' => get_option('sticky_posts'), 'category_name' => Work)); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php get_template_part( 'loop', 'index' ); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
<?php endif ?>
<?php
$postcount = $wp_query->post_count;
$postnumber = 8;
if ( $paged != True ){ $postnumber = $postnumber - $postcount; }
?>
<!-- Non-Sticky -->
<?php $wp_query = new WP_Query(array('post__not_in' => get_option('sticky_posts'), 'category_name' => Work, 'posts_per_page' => $postnumber, 'paged' => $paged)); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php get_template_part( 'loop', 'index' ); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
take a look at my answer to a similar question here:
Can post_nav_link navigation work with WordPress custom page templates?
the key is offset
How do I get a random post in Wordpress?
I would like to display a button on a page that, when pressed, goes to a random post from the blog. I don't want a random post to be displayed on the page, I just want a link that leads to that post.
I tried searching for a code on Google and here at stackoverflow but no success.
Thanks...
UPDATE:
Here is my template code:
<?php /*Template Name: Random*/ ?>
<?php get_header(); ?>
<nav><?php wp_nav_menu(array('menu' => 'Main Nav Menu')); ?></nav>
<div id="main-content-archive">
<div class="grey-text">Random post</div>
<?php $query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );?>
<?php if (have_posts()) : while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
?>
<?php endwhile; ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
create a page template, and use the following code to get a random post:
//Create WordPress Query with 'orderby' set to 'rand' (Random)
$the_query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );
// output the random post
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
then in a page, just use:
see a random post
I found this post which gave me desired results...
Here's a solution copy/pasted from the wpbeginner blog post. No copyright infringement intended.
Just add the following code to the functions.php file:
add_action('init','random_add_rewrite');
function random_add_rewrite() {
global $wp;
$wp->add_query_var('random');
add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}
add_action('template_redirect','random_template');
function random_template() {
if (get_query_var('random') == 1) {
$posts = get_posts('post_type=post&orderby=rand&numberposts=1');
foreach($posts as $post) {
$link = get_permalink($post);
}
wp_redirect($link,307);
exit;
}
}
Use mydomain.com/random/ as your href for your button that leads to the random post.
Thanks everyone who contributed for your help...
Cheers!
I find it is more useful to have a URL that will redirect to a random post that you can use as link in sidebar or in menus. If it is a single WP site and even on wp.com it's really easy, for a blog at
http://mygroovywpsite.me/
All you need to do is append it with ?random
http://mygroovywpsite.me/?random
I found this did not work (nor the wp_beginner code above) on subsites in my multisite installation, either approach just loaded the home page. Maybe I had some funky cache issues. The way I do this on many sites is a few more steps w/o plugins.
First make a Page in your site called "Random" / with the slug "random" -- it does not need any content in it
Then create a page-random.php template
<?php
/*
Random Post Picker
Use on page to send viewer to random post optionally mod query
*/
// set arguments for WP_Query on published posts to get 1 at random
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'rand'
);
// It's time! Go someplace random
$my_random_post = new WP_Query ( $args );
while ( $my_random_post->have_posts () ) {
$my_random_post->the_post ();
// redirect to the random post
wp_redirect ( get_permalink () );
exit;
}
?>
Then you get the re-direct for any link on your blog ...../random w/o any wrestling with .htaccess
I've done it this way because I've had to modify the query, sometimes for custom post type, sometimes to restrict to category, etc.
I only had one site that was a problem because the hosting suppressed the use of mySQL queries with ORDER BY RAND()
Another Simple solution to display Random Post
1.First a create a custom page template. Name it as random post or a name of your choice!
2.Open the page and remove the default wp loop and Paste the code below
3.To change the no of post change the number ‘1’ to your choice!
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile;
endif; ?>
source: http://www.yengkokpam.com/displays-random-posts-in-a-page/
Check This
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
On the Wordpress site I'm working on, it lists posts by category, but I am also after a page that lists ALL the posts (with pagination, showing 10 per page). How would I go about achieving this?
Thanks
You could create a new page template with this loop in it:
<?php
$paged = get_query_var('paged')? get_query_var('paged') : 1;
$args = [
'post_type' => 'post',
'posts_per_page' => 10,
'paged' => $paged,
];
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ?></h2>
<?php endwhile; ?>
<!-- then the pagination links -->
<?php next_posts_link( '← Older posts', $wp_query ->max_num_pages); ?>
<?php previous_posts_link( 'Newer posts →' ); ?>
For others who might be Googling this... If you have replaced the front page of your site with a static page, but still want your list of posts to appear under a separate link, you need to:
Create an empty page (and specify any URL/slug you like)
Under Settings > Reading, choose this new page as your "Posts page"
Now when you click the link to this page in your menu, it should list all your recent posts (no messing with code required).
A bit more fancy solution based on #Gavins answer
<?php
/*
Template Name: List-all-chronological
*/
function trimStringIfTooLong($s) {
$maxLength = 60;
if (strlen($s) > $maxLength) {
echo substr($s, 0, $maxLength - 5) . ' ...';
} else {
echo $s;
}
}
?>
<ul>
<?php
$query = array( 'posts_per_page' => -1, 'order' => 'ASC' );
$wp_query = new WP_Query($query);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" title="Link to <?php the_title_attribute() ?>">
<?php the_time( 'Y-m-d' ) ?>
<?php trimStringIfTooLong(get_the_title()); ?>
</a>
</li>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts published so far.'); ?></p>
<?php endif; ?>
</ul>