-
-
Save JacobLett/65dc1972f230906ac02e to your computer and use it in GitHub Desktop.
WordPress Shortcodes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Enable shortcodes in widgets | |
*/ | |
add_filter('widget_text', 'do_shortcode'); | |
/** | |
* Alternative tagcloud (WPML-ready) | |
*/ | |
function theme_shortcode_tagcloud($atts) { | |
$args = array( | |
'smallest' => 0.857, | |
'largest' => 1.4, | |
'unit' => 'em', | |
'number' => 10 | |
); | |
$tags = wp_generate_tag_cloud(get_tags(), $args); | |
return "<div class='tagcloud'>{$tags}</div>"; | |
} | |
add_shortcode('tagcloud', 'theme_shortcode_tagcloud'); | |
/** | |
* Show specific post | |
*/ | |
function theme_shortcode_post($atts) { | |
extract(shortcode_atts(array( | |
'id' => false | |
), $atts)); | |
if (!$id) return 'No ID specified'; | |
$args = array( | |
'post__in' => array($id), | |
'suppress_filters' => 0 | |
); | |
$posts = get_posts($args); | |
$output = ''; | |
foreach ($posts as $post) { | |
$image = theme_get_featured_image($post->ID); | |
$content = get_the_excerpt(); | |
$permalink = get_permalink($post->ID); | |
$more = theme_get_readmore(); | |
$output .= "<div class='post featured'>" . | |
"<a href='{$permalink}'>{$image}</a>" . | |
"<h2>{$post->post_title}</h2>" . | |
"<div class='text'>{$content}</div>" . | |
"<div class='meta'>{$post->post_date}</div>" . | |
"<a href='{$permalink}' class='more-link'>{$more}</a>" . | |
"</div>"; | |
} | |
return $output; | |
} | |
add_shortcode('post', 'theme_shortcode_post'); | |
/** | |
* Show latest posts (example for single blog) | |
*/ | |
function theme_shortcode_latest($atts) { | |
extract(shortcode_atts(array( | |
'number' => 3, | |
'category' => '' | |
), $atts)); | |
$args = array( | |
'post_status' => 'publish', | |
'posts_per_page' => -1, | |
'order' => 'ASC', | |
'orderby' => 'date', | |
'numberposts' => $number, | |
'category' => $category, | |
'suppress_filters' => 0 | |
); | |
$posts = get_posts($args); | |
$output = '<ul class="postlist">'; | |
foreach ($posts as $post) { | |
$author = get_userdata($post->post_author); | |
$avatar = get_avatar($author->ID, 70, theme_ASSETS_URI.'img/avatar.jpg'); | |
$meta = theme_get_meta($author->user_nicename, $post->post_date); | |
$permalink = get_permalink($post->ID); | |
$output .= "<li>" . | |
"{$avatar}" . | |
"<a href='{$permalink}' class='title'>{$post->post_title}</a>" . | |
"{$meta}" . | |
"</li>"; | |
} | |
$output .= '</ul>'; | |
return $output; | |
} | |
add_shortcode('latest_posts', 'theme_shortcode_latest'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment