MYID - CHANGE
SHORTCODE - [related_posts_by_tax posts_per_page="4" format="images" taxonomies="post_tag"]
Functions.php
#######
add_filter( 'the_content', 'add_related_posts_after_post_content' );
function add_related_posts_after_post_content( $content ) {
//check if it's a single post page.
if ( is_single() ) {
// check if we're inside the main loop
if ( in_the_loop() && is_main_query() ) {
// add your own attributes here (between the brackets [ ... ])
$shortcode = '[related_posts_by_tax posts_per_page="4" format="images" taxonomies="post_tag"]';
// add the shortcode after the content
$content = $content . $shortcode;
}
}
return $content;
}
// Adds the 'Post Images' format to the widget
// after the plugin's defaults are set (priority 11).
add_action( 'wp_loaded', 'rpbt_add_post_images_format_widget', 11 );
function rpbt_add_post_images_format_widget() {
if ( !class_exists( 'Related_Posts_By_Taxonomy_Defaults' ) ) {
return;
}
$defaults = Related_Posts_By_Taxonomy_Defaults::get_instance();
// Add the new format.
$defaults->formats['images'] = __( 'Post Images' );
}
// Sets the format to thumbnails if the Post Images format is used.
// Adds filters to remove the sql used to only find posts with post thumbnails.
add_filter( 'related_posts_by_taxonomy_shortcode_atts', 'rpbt_post_images_format' );
add_filter( 'related_posts_by_taxonomy_widget_args', 'rpbt_post_images_format' );
function rpbt_post_images_format( $args ) {
// Check if the new images format is used.
if ( 'images' === $args['format'] ) {
// Set format to thumbnails if Post Images format is used.
$args['format'] = 'thumbnails';
// Removes the sql to search for posts with post thumbnails only.
add_filter( 'get_meta_sql', '__return_empty_string', 23 );
// Removes the filter after related posts are retrieved.
add_filter( 'related_posts_by_taxonomy', 'rpbt_remove_meta_sql_filter' );
// Removes the filter if no related posts were found.
add_action( 'related_posts_by_taxonomy_after_display', 'rpbt_remove_meta_sql_filter' );
}
return $args;
}
// Remove the filter
function rpbt_remove_meta_sql_filter( $results ) {
remove_filter( 'get_meta_sql', '__return_empty_string', 23 );
return $results;
}
// Use a fallback image if a post has no post thumbnail.
add_filter( 'related_posts_by_taxonomy_post_thumbnail_link', 'rpbt_related_post_fallback_image', 10, 4 );
function rpbt_related_post_fallback_image( $image, $attr, $related, $args ) {
if ( !empty( $image ) ) {
// Post thumbnail found.
return $image;
}
$image_args = array(
'numberposts' => 1,
'order' => 'ASC',
'post_mime_type' => 'image',
'post_parent' => $related->ID,
'post_status' => null,
'post_type' => 'attachment',
);
$attachments = get_children( $image_args );
if ( !empty( $attachments ) ) {
// Post attachment image found.
$attachments = array_values( $attachments );
$attachment_id = (int) $attachments[0]->ID;
// Use the first attachment image.
$image = wp_get_attachment_image( $attachment_id , $args['size'], false, $attr['describedby'] );
} else {
/**
* Use your own fallback image here.
*
* Example of an image uploaded with the media editor.
* Change the attachment ID (123) below to your uploaded fallback image ID.
* And remove the two foreward slashes at the beginning.
*/
$image = wp_get_attachment_image( MYID, $args['size'], false, $attr['describedby'] );
}
$image = $image ? "<a>{$image}</a>" : '';
return $image;
}