Wordpress

How to Create a Page that Displays Random Posts

Have you ever been to a site and saw this cool feature? They have a link in their top navigation to something like Stumbe! or Read Random Articles, or some other creative text. When you click on that link, it takes you to a page that displays one random page. Each time you refresh, you are delivered with a new post. Well this trick is just for you then.

You would need to follow the trick #1 in this article to create a custom page template. And simply paste this code in there:

01<?php
02query_posts(array('orderby' => 'rand', 'showposts' => 1));
03if (have_posts()) :
04while (have_posts()) : the_post(); ?>
05
06<h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
07
08<?php the_content(); ?>
09
10<?php endwhile;
11endif; ?>

This is a simple WordPress Loop that is running a query to display random posts and the number 1 in there is telling WordPress to only show 1 post. You can change that number, but most of the time people do it one post a time.

We have a Quick Reading Page on our site as well, so you can see this trick in action.

3. How to Display any External RSS Feed on Your Site

 WordPress

Have you seen other bloggers who display their other blog’s feed on their site. You want to do it too for extra promotion and traffic. Well here is the tutorial for you. Simply paste the following code in anywhere in your theme:

01<?php include_once(ABSPATH.WPINC.'/feed.php');
02$rss = fetch_feed('http://feeds.feedburner.com/wpbeginner');
03$maxitems = $rss->get_item_quantity(5);
04$rss_items = $rss->get_items(0, $maxitems);
05?>
06<ul>
07<?php if ($maxitems == 0) echo '<li>No items.</li>';
08else
09// Loop through each feed item and display each item as a hyperlink.
10foreach ( $rss_items as $item ) : ?>
11<li>
12<a href='<?php echo $item->get_permalink(); ?>'
13title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'>
14<?php echo $item->get_title(); ?></a>
15</li>
16<?php endforeach; ?>
17</ul>

4. How to Display Relative Dates in WordPress

Display Relative Dates in WordPress

Have you ever seen this in blog posts and comments and wondered how did this blogger manage to do this? Actually it is pretty easy.

You would need to download a plugin called WP-Relative Date

Once you have downloaded and activated the plugin, look in your single.php, index.php, and page.php for this code:

1<?php the_date(); ?>

Replace it with:

1<?php relative_post_the_date(); ?>

Mostakim Hossen

I am Mostakim Hossain, a Web designer and developer, 23 years of age. I have been working as a freelance (upwork) for almost 2 years now and I will be glad in helping you create your next awesome project.

I have created lots of public and private websites(some shown below) and always looking for more work to learn from. I am using PHP as my primary programming language. You can see my skill set beside this text.

I am a person who is willing to learn and adapts quickly. I am keen to design and security. I believe in the core principles of Open Source and support it. I have a minimalist approach to design and usability. Simplicity is beauty they say.
Masters of Accountancy : Asian University of Bangladesh
I’m here to answer any questions you might have about our products and services. Feel free to contact me using any of the following methods:

Email : support@smarterdevs.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe
SUBSCRIBE NOW

Coupon : happynewyear

60% OFF

Stay updated with all latest updates,upcoming plugins & much more.
WordPress Themes
Search