Files
roi-theme/wp-content/themes/apus-theme/inc/featured-image.php
FrankZamora 7ba9080f57 Agregar estructura completa del tema APUS con Bootstrap 5 y optimizaciones de rendimiento
Se implementa tema WordPress personalizado para Análisis de Precios Unitarios con funcionalidades avanzadas:
- Sistema de templates (front-page, single, archive, page, 404, search)
- Integración de Bootstrap 5.3.8 con estructura modular de assets
- Panel de opciones del tema con Customizer API
- Optimizaciones de rendimiento (Critical CSS, Image Optimization, Performance)
- Funcionalidades SEO y compatibilidad con Rank Math
- Sistema de posts relacionados y tabla de contenidos
- Badge de categorías y manejo de AdSense diferido
- Tipografías Google Fonts configurables
- Documentación completa del tema y guías de uso

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-04 09:31:47 -06:00

151 lines
4.4 KiB
PHP

<?php
/**
* Featured Image Functions
*
* Configurable featured image display with lazy loading support.
*
* @package Apus_Theme
* @since 1.0.0
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
/**
* Get Featured Image HTML
*
* Returns the HTML for the featured image based on theme options.
* Supports lazy loading and configurable display per post type.
*
* @param int $post_id Optional. Post ID. Defaults to current post.
* @param string $size Optional. Image size. Default 'apus-featured-large'.
* @param array $attr Optional. Additional attributes for the image.
* @param bool $force_show Optional. Force display regardless of settings. Default false.
* @return string HTML markup for featured image or empty string.
*/
function apus_get_featured_image($post_id = null, $size = 'apus-featured-large', $attr = array(), $force_show = false) {
// Get post ID if not provided
if (!$post_id) {
$post_id = get_the_ID();
}
// Check if post has thumbnail
if (!has_post_thumbnail($post_id)) {
return '';
}
// Get post type
$post_type = get_post_type($post_id);
// Check if featured images are enabled for this post type
if (!$force_show) {
$option_key = 'apus_featured_image_' . $post_type;
$enabled = get_theme_mod($option_key, true); // Default enabled
if (!$enabled) {
return '';
}
}
// Default attributes
$default_attr = array(
'alt' => get_the_title($post_id),
'loading' => 'lazy',
'class' => 'featured-image',
);
// Merge with custom attributes
$attributes = wp_parse_args($attr, $default_attr);
// Get the thumbnail HTML
$thumbnail = get_the_post_thumbnail($post_id, $size, $attributes);
if (empty($thumbnail)) {
return '';
}
// Wrap in container div
$output = '<div class="post-thumbnail">';
$output .= $thumbnail;
$output .= '</div>';
return $output;
}
/**
* Display Featured Image
*
* Echoes the featured image HTML.
*
* @param int $post_id Optional. Post ID. Defaults to current post.
* @param string $size Optional. Image size. Default 'apus-featured-large'.
* @param array $attr Optional. Additional attributes for the image.
* @param bool $force_show Optional. Force display regardless of settings. Default false.
*/
function apus_the_featured_image($post_id = null, $size = 'apus-featured-large', $attr = array(), $force_show = false) {
echo apus_get_featured_image($post_id, $size, $attr, $force_show);
}
/**
* Check if Featured Images are Enabled for Post Type
*
* @param string $post_type Optional. Post type. Defaults to current post type.
* @return bool True if enabled, false otherwise.
*/
function apus_is_featured_image_enabled($post_type = '') {
if (empty($post_type)) {
$post_type = get_post_type();
}
$option_key = 'apus_featured_image_' . $post_type;
return (bool) get_theme_mod($option_key, true);
}
/**
* Register Featured Image Settings in Customizer
*
* Adds controls to enable/disable featured images per post type.
*
* @param WP_Customize_Manager $wp_customize Theme Customizer object.
*/
function apus_featured_image_customizer($wp_customize) {
// Add section
$wp_customize->add_section('apus_featured_images', array(
'title' => __('Featured Images', 'apus-theme'),
'priority' => 30,
));
// Get public post types
$post_types = get_post_types(array('public' => true), 'objects');
foreach ($post_types as $post_type) {
// Skip attachments
if ($post_type->name === 'attachment') {
continue;
}
$setting_id = 'apus_featured_image_' . $post_type->name;
// Add setting
$wp_customize->add_setting($setting_id, array(
'default' => true,
'sanitize_callback' => 'wp_validate_boolean',
'transport' => 'refresh',
));
// Add control
$wp_customize->add_control($setting_id, array(
'label' => sprintf(
/* translators: %s: post type label */
__('Enable for %s', 'apus-theme'),
$post_type->labels->name
),
'section' => 'apus_featured_images',
'type' => 'checkbox',
));
}
}
add_action('customize_register', 'apus_featured_image_customizer');