Skip to content

Instantly share code, notes, and snippets.

@jacobwise
Last active June 1, 2020 22:57
Show Gist options
  • Save jacobwise/10221760c0c1918e36f1 to your computer and use it in GitHub Desktop.
Save jacobwise/10221760c0c1918e36f1 to your computer and use it in GitHub Desktop.
Recent posts for a Custom Post Type
<?php
/**
* Recent Custom Posts
* @author Jacob Wise
* @link http://swellfire.com/code/recent-custom-posts
*
* @param string $post_type
* @param int $numberposts
* @echo string $output
*/
class jw_recent_custom_posts_class {
function jw_recent_custom_posts($post_type, $numberposts)
{
$output = '';
$output .= '<section class="widget widget_recent_entries">';
$output .= '<div class="widget-wrap">';
$output .= '<h4 class="widget-title widgettitle">Recent Posts</h4>';
$output .= '<ul>';
$args = array(
'numberposts' => $numberposts,
'post_type' => $post_type );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
$output .= '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
}
$output .= '</ul>';
$output .= '</div>';
$output .= '</section>';
echo $output;
}
}
function jw_do_recent_custom_posts()
{
if ( is_singular( 'code' ) || is_post_type_archive( 'code' ) || is_tax( 'code_category' ) ) {
$my_class = new jw_recent_custom_posts_class();
$my_class->jw_recent_custom_posts( 'code', '5' );
}
if ( is_singular( 'tutorial' ) || is_post_type_archive( 'tutorial' ) || is_tax( 'tutorial_category' ) ) {
$my_class = new jw_recent_custom_posts_class();
$my_class->jw_recent_custom_posts( 'tutorial', '5' );
}
}
add_action('genesis_sidebar', 'jw_do_recent_custom_posts');
@Mohamed-Slimane
Copy link

can i use it in my WordPress template?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment