| Server IP : 65.108.144.40 / Your IP : 216.73.217.165 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ubuntu-8gb-hel1-1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64 User : dev ( 1000) PHP Version : 8.2.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/html/successkpi/wp-content/themes/successkpi/blocks/featured-resource-block/ |
Upload File : |
<?php
/**
* Featured Resource Block: Displays a selected post or the latest blog post as a featured tile.
*/
if (function_exists('render_block_preview_image') && render_block_preview_image($block, 'Featured Resource block preview')) {
return;
}
$featured_post_selection = get_field('featured_post');
$custom_cta_label = get_field('cta_label');
$external_url = get_field('external_url');
$target_post_id = false;
if ($featured_post_selection) {
if (is_array($featured_post_selection)) {
$first_item = $featured_post_selection[0];
if (is_object($first_item)) {
$target_post_id = isset($first_item->ID) ? $first_item->ID : false;
} else {
$target_post_id = $first_item;
}
} elseif (is_object($featured_post_selection)) {
$target_post_id = isset($featured_post_selection->ID) ? $featured_post_selection->ID : false;
} else {
$target_post_id = $featured_post_selection;
}
}
if (!$target_post_id) {
$fallback_post_type = 'post';
$current_page_id = get_the_ID();
if ($current_page_id) {
$current_slug = get_post_field('post_name', $current_page_id);
if ($current_slug && post_type_exists($current_slug)) {
$fallback_post_type = $current_slug;
}
}
$latest_post_query = get_posts(array(
'post_type' => $fallback_post_type,
'posts_per_page' => 1,
'post_status' => 'publish',
));
if (!empty($latest_post_query) && isset($latest_post_query[0]->ID)) {
$target_post_id = $latest_post_query[0]->ID;
}
}
if ($target_post_id):
$cta_heading = get_the_title($target_post_id);
$raw_excerpt = trim(
get_post_field('post_excerpt', $target_post_id)
);
if (isset($raw_excerpt) && !empty($raw_excerpt)) {
$cta_description = wp_trim_words(
wp_strip_all_tags($raw_excerpt),
25,
'.'
);
} else {
$raw_content = get_post_field(
'post_content',
$target_post_id
);
$cta_description = wp_trim_words(
wp_strip_all_tags(
strip_shortcodes($raw_content)
),
25,
'.'
);
}
if (isset($external_url) && !empty($external_url)) {
$card_url = $external_url;
} else {
$card_url = get_permalink($target_post_id);
}
$categories = get_the_terms($target_post_id, 'resource_category');
if (!empty($categories) && !is_wp_error($categories)) :
$first_category = $categories[0];
$type_label_text = !empty($first_category->name) ? esc_html($first_category->name) : 'N/A';
endif;
if (isset($custom_cta_label) && !empty($custom_cta_label)) {
$cta_link_title = $custom_cta_label;
} else {
$cta_link_title = 'Read More';
}
$image_id = get_post_thumbnail_id($target_post_id);
$image_url = '';
$image_alt = $cta_heading;
if (isset($image_id) && !empty($image_id)) {
$image_src = wp_get_attachment_image_src(
$image_id,
'large'
);
if (
isset($image_src[0]) &&
!empty($image_src[0])
) {
$image_url = $image_src[0];
}
$alt_text = get_post_meta(
$image_id,
'_wp_attachment_image_alt',
true
);
if (isset($alt_text) && !empty($alt_text)) {
$image_alt = $alt_text;
}
}
$base_class = 'featured-resource sk-section-spacing';
$custom_class = isset($block['className']) ? $block['className'] : '';
$className = trim("$base_class $custom_class");
?>
<section class="<?php echo esc_attr(trim($className)); ?>" data-aos="fade-up">
<div class="container">
<a href="<?php echo esc_url($card_url); ?>" class="featured-resource-tile">
<div class="featured-resource-image">
<div class="featured-image">
<?php if (isset($image_url) && !empty($image_url)): ?>
<img src="<?php echo esc_url($image_url); ?>" alt="<?php echo esc_attr($image_alt); ?>">
<?php endif; ?>
</div>
</div>
<div class="featured-resource-content">
<?php if (isset($type_label_text) && !empty($type_label_text)): ?>
<span class="resource-type-tag">
<?php echo esc_html($type_label_text); ?>
</span>
<?php endif; ?>
<?php if (isset($cta_heading) && !empty($cta_heading)): ?>
<h2>
<?php echo esc_html($cta_heading); ?>
</h2>
<?php endif; ?>
<?php if (isset($cta_description) && !empty($cta_description)): ?>
<p>
<?php echo esc_html($cta_description); ?>
</p>
<?php endif; ?>
<span class="read-more">
<?php echo esc_html($cta_link_title); ?>
</span>
</div>
</a>
</div>
</section>
<?php elseif (is_admin()): ?>
<div style="padding:20px;border:1px dashed #ccc;">
<strong>Featured Resource Block:</strong>
No post found. Please select a post or ensure a blog post exists.
</div>
<?php endif; ?>