-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLS-Random-Post.php
More file actions
53 lines (45 loc) · 1.4 KB
/
LS-Random-Post.php
File metadata and controls
53 lines (45 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/*
Plugin Name: Random Post
Plugin URI: https://github.com/liamstewart23/WordPressRandomPost
Description: Shortcode that creates a link to random post
Version: 1.0.0
Author: Liam Stewart
Author URI: https://liamstewart.ca
*/
/**
* @param $atts
*
* @return string
*/
function ls_random_post( $atts ) {
shortcode_atts( array(
'type' => 'post'
), $atts );
query_posts( array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'rand'
) );
$title = 'Random Post';//default title
if ( have_posts() ) :
while ( have_posts() ) : the_post();
if ( get_the_title() ) {//if title is set pick that title
$title = get_the_title();
}
$post = '<a href="' . get_permalink() . '">' . $title . '</a>';
endwhile;
endif;
wp_reset_query();
return $post;
}
function ls_randompost_register_shortcodes() {
add_shortcode( 'random', 'ls_random_post' );
add_shortcode( 'randompost', 'ls_random_post' );
add_shortcode( 'rp', 'ls_random_post' );
add_filter( 'widget_text', 'do_shortcode' );// shortcodes in widgets
add_filter( 'the_excerpt', 'do_shortcode' );// shortcodes in excerpts
add_filter( 'comment_text', 'do_shortcode' );// shortcodes in comments
}
add_action( 'init', 'ls_randompost_register_shortcodes' );// init