Skip to main content

Code Snippet Example

/**
* Recent Posts SmartCodes for DNG Community
*
* Adds a "Recent Posts" space to DNG smart codes so we can drop our
* latest blog posts (or the full latest post) into any email.
*
* Smart codes:
* {{recent_posts.list}} 5 latest posts (thumbnail, title, excerpt, date)
* {{recent_posts.list_3}} 3 latest posts
* {{recent_posts.list_10}} 10 latest posts
* {{recent_posts.latest_title}} Latest post title
* {{recent_posts.latest_excerpt}} Latest post excerpt
* {{recent_posts.latest_link}} Latest post URL (without https://)
* {{recent_posts.latest_link_html}} Latest post title as a link
* {{recent_posts.latest_button}} "Read Latest Post" button
* {{recent_posts.latest_image}} Latest post featured image
* {{recent_posts.latest_full}} Latest post as a card
* {{recent_posts.latest_body}} Full content of the latest post
*
* Filter by category: add _category_{slug-or-id} to any code, e.g.
* {{recent_posts.latest_body_category_daily-prayers}}
* {{recent_posts.list_3_category_12}}
* Underscores in the slug are converted to hyphens, so _category_daily_prayers also works.
*
* Shortcode (also works inside FluentCRM emails):
* [upfluent_recent_posts count="5" category="news" show_image="yes" show_excerpt="yes" show_date="yes" post_type="post"]
*
* Install: paste into Child Theme (functions.php) or a code snippets plugin.
*/
defined( 'ABSPATH' ) || exit;
/* -------------------------------------------------------------------------
* Register the smart codes
* ---------------------------------------------------------------------- */
add_action( 'fluent_crm/after_init', function () {
$codes = array(
'list' => 'Recent Posts List (5 posts)',
'list_3' => 'Recent Posts List (3 posts)',
'list_10' => 'Recent Posts List (10 posts)',
'latest_title' => 'Latest Post Title',
'latest_excerpt' => 'Latest Post Excerpt',
'latest_link' => 'Latest Post URL (raw)',
'latest_link_html' => 'Latest Post Link (clickable)',
'latest_button' => 'Latest Post Button',
'latest_image' => 'Latest Post Featured Image',
'latest_full' => 'Latest Post (Full Card)',
'latest_body' => 'Latest Post Full Content',
);
FluentCrmApi( 'extender' )->addSmartCode( 'recent_posts', 'Recent Posts', $codes, function ( $code, $value_key, $default, $subscriber ) {
$category = '';
if ( preg_match( '/^(.+?)_category_(.+)$/', $value_key, $m ) ) {
$value_key = $m[1];
$category = $m[2];
}
switch ( $value_key ) {
case 'list':
return upfluent_list_html( 5, $category );
case 'list_3':
return upfluent_list_html( 3, $category );
case 'list_10':
return upfluent_list_html( 10, $category );
case 'latest_title':
case 'latest_excerpt':
case 'latest_link':
case 'latest_link_html':
case 'latest_button':
case 'latest_image':
return upfluent_latest_field( substr( $value_key, 7 ), $category );
case 'latest_full':
return upfluent_latest_card( $category );
case 'latest_body':
return upfluent_latest_body( $category );
}
return $default;
} );
} ); 
/* -------------------------------------------------------------------------
* Helpers
* ---------------------------------------------------------------------- */
/**
* Fetch posts. $context lets filters target one output (list, latest, card, body, shortcode).
*/
function upfluent_get_posts( $count = 5, $category = '', $context = 'list', $post_type ='post' ) {
$args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'posts_per_page' => max( 1, (int) $count ),
'orderby' => 'date',
'order' => 'DESC',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
);
$category = trim( (string) $category );
if ( '' !== $category ) {
if ( is_numeric( $category ) ) {
$args['cat'] = (int) $category;
} else {
$args['category_name'] = sanitize_title( str_replace( '_', '-', $category ) );
}
}
$args = apply_filters( 'upfluent_query_args', $args, $context );
return get_posts( $args );
}
function upfluent_excerpt( $post, $words ) {
$text = $post->post_excerpt ? $post->post_excerpt : strip_shortcodes( $post->post_content );
return esc_html( wp_trim_words( $text, $words, '…' ) );
}
function upfluent_empty() {
return apply_filters( 'upfluent_empty_html', '<p style="color:#666666;font-style:italic;">No recent posts available.</p>' );
}

/**
* Email-safe list of posts (table layout, inline styles).
*/
function upfluent_list_html( $count = 5, $category = '', $opts = array(), $post_type = 'post' ) {
$opts = wp_parse_args( $opts, array( 'image' => true, 'excerpt' => true, 'date' => true ) );
$posts = upfluent_get_posts( $count, $category, 'list', $post_type );
if ( empty( $posts ) ) {
return upfluent_empty();
}

$html = '<table role="presentation" cellpadding="0" cellspacing="0" border="0" width="100%" style="border-collapse:collapse;margin:20px 0;">';

foreach ( $posts as $post ) {
$url = esc_url( get_permalink( $post ) );
$title = esc_html( get_the_title( $post ) );
$thumb = $opts['image'] ? get_the_post_thumbnail_url( $post, 'thumbnail' ) : '';
$html .= '<tr><td style="padding:15px 0;border-bottom:1px solid #e5e5e5;vertical-align:top;">';
$html .= '<table role="presentation" cellpadding="0" cellspacing="0" border="0" width="100%"><tr>';

if ( $thumb ) {
$html .= '<td width="80" style="padding-right:15px;vertical-align:top;">';
$html .= '<a href="' . $url . '" style="text-decoration:none;"><img src="' . esc_url( $thumb ) . '" alt="' . esc_attr( get_the_title( $post ) ) . '" width="80" height="80" style="display:block;border-radius:4px;object-fit:cover;" /></a>';
$html .= '</td>';
}
$html .= '</table>';
return apply_filters( 'upfluent_list_html', $html, $posts, $count );
}
// } );
etc
etc
etc
etc
...
...
...
😴