Posted On October 7, 2021

WordPress Plugin to Customize How Posts are Displayed

kimconnect 0 comments
blog.KimConnect.com >> Codes >> WordPress Plugin to Customize How Posts are Displayed

A. Install the ‘Display Posts’ plugin

B. Install the ‘Code Snippets’ plugin

C. Add this code snippet

/**
* Display Posts, use first attached image
* @link https://displayposts.com/2019/10/16/display-image-from-post-content-if-no-featured-image/
*/
function be_dps_first_image( $output, $original_atts, $image, $title, $date, $excerpt, $inner_wrapper, $content, $class, $author, $category_display_text ) {

// Only run if image_size is set and no featured image
if( empty( $original_atts['image_size'] ) || !empty( $image ) )
return $output;

$images = new WP_Query( array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'posts_per_page' => 1,
'order' => 'ASC',
'fields' => 'ids',
));

if( !empty( $images->posts ) ) {
$image = '<a href="' . get_permalink() . '" class="image">' . wp_get_attachment_image( $images->posts[0], $original_atts['image_size'] ) . '</a>';
$output = '<' . $inner_wrapper . ' class="' . implode( ' ', $class ) . '">' . $image . $title . $date . $author . $category_display_text . $excerpt . $content . '</' . $inner_wrapper . '>';
}

return $output;
}
add_filter( 'display_posts_shortcode_output', 'be_dps_first_image', 10, 11 );

D. Add this CSS into Appearance > Customize > Additional CSS

.display-posts-listing.image-left .listing-item {
overflow: hidden;
margin-bottom: 32px;
width: 100%;
}

.display-posts-listing.image-left .image {
float: left;
margin: 0 16px 0 0;
}

.display-posts-listing.image-left .title {
display: block;
}

.display-posts-listing.image-left .excerpt-dash {
display: none;
}

Leave a Reply

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

Related Post

PowerShell: Disable Forticlient Web Filtering

# disableForticlientWebfiltering.ps1 # Note: untested script - use as sample as this snippet is incomplete…

PowerShell: Alternative to Test-NetConnection for Legacy Windows

This little snippet is useful as a helper function to quickly determine port connectivity between…

PowerShell: Set Virtual Machine Default Paths on Hyper-V Host of a Cluster

$newVirtualMachinePath='D:\VirtualMachines' $newVirtualHardDiskPath='D:\VirtualMachines' function getHyperVHostsInForest{ function includeRSAT{ $ErrorActionPreference='stop' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #$rsatWindows7x32='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x86-RefreshPkg.msu' $rsatWindows7x64='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x64-RefreshPkg.msu' $rsatWindows81='https://download.microsoft.com/download/1/8/E/18EA4843-C596-4542-9236-DE46F780806E/Windows8.1-KB2693643-x64.msu' $rsat1709 =…