backup: estado antes de limpieza de defaults
This commit is contained in:
394
admin/theme-options/USAGE-EXAMPLES.php
Normal file
394
admin/theme-options/USAGE-EXAMPLES.php
Normal file
@@ -0,0 +1,394 @@
|
||||
<?php
|
||||
/**
|
||||
* Theme Options Usage Examples
|
||||
*
|
||||
* This file contains examples of how to use theme options throughout the theme.
|
||||
* DO NOT include this file in functions.php - it's for reference only.
|
||||
*
|
||||
* @package Apus_Theme
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
||||
// Exit if accessed directly
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* EXAMPLE 1: Using options in header.php
|
||||
*/
|
||||
function example_display_logo() {
|
||||
$logo_url = apus_get_logo_url();
|
||||
|
||||
if ($logo_url) {
|
||||
?>
|
||||
<a href="<?php echo esc_url(home_url('/')); ?>" class="custom-logo-link">
|
||||
<img src="<?php echo esc_url($logo_url); ?>" alt="<?php bloginfo('name'); ?>" class="custom-logo" />
|
||||
</a>
|
||||
<?php
|
||||
} else {
|
||||
?>
|
||||
<h1 class="site-title">
|
||||
<a href="<?php echo esc_url(home_url('/')); ?>"><?php bloginfo('name'); ?></a>
|
||||
</h1>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* EXAMPLE 2: Displaying breadcrumbs
|
||||
*/
|
||||
function example_show_breadcrumbs() {
|
||||
if (apus_show_breadcrumbs() && !is_front_page()) {
|
||||
$separator = apus_get_breadcrumb_separator();
|
||||
|
||||
echo '<nav class="breadcrumbs">';
|
||||
echo '<a href="' . esc_url(home_url('/')) . '">Home</a>';
|
||||
echo ' ' . esc_html($separator) . ' ';
|
||||
|
||||
if (is_single()) {
|
||||
the_category(' ' . esc_html($separator) . ' ');
|
||||
echo ' ' . esc_html($separator) . ' ';
|
||||
the_title();
|
||||
} elseif (is_category()) {
|
||||
single_cat_title();
|
||||
}
|
||||
|
||||
echo '</nav>';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* EXAMPLE 3: Customizing excerpt
|
||||
*/
|
||||
function example_custom_excerpt_length($length) {
|
||||
return apus_get_excerpt_length();
|
||||
}
|
||||
add_filter('excerpt_length', 'example_custom_excerpt_length');
|
||||
|
||||
function example_custom_excerpt_more($more) {
|
||||
return apus_get_excerpt_more();
|
||||
}
|
||||
add_filter('excerpt_more', 'example_custom_excerpt_more');
|
||||
|
||||
/**
|
||||
* EXAMPLE 4: Displaying related posts in single.php
|
||||
*/
|
||||
function example_display_related_posts() {
|
||||
if (apus_show_related_posts() && is_single()) {
|
||||
$count = apus_get_related_posts_count();
|
||||
$taxonomy = apus_get_related_posts_taxonomy();
|
||||
$title = apus_get_related_posts_title();
|
||||
|
||||
// Get related posts
|
||||
$post_id = get_the_ID();
|
||||
$args = array(
|
||||
'posts_per_page' => $count,
|
||||
'post__not_in' => array($post_id),
|
||||
);
|
||||
|
||||
if ($taxonomy === 'category') {
|
||||
$categories = wp_get_post_categories($post_id);
|
||||
if ($categories) {
|
||||
$args['category__in'] = $categories;
|
||||
}
|
||||
} elseif ($taxonomy === 'tag') {
|
||||
$tags = wp_get_post_tags($post_id, array('fields' => 'ids'));
|
||||
if ($tags) {
|
||||
$args['tag__in'] = $tags;
|
||||
}
|
||||
}
|
||||
|
||||
$related = new WP_Query($args);
|
||||
|
||||
if ($related->have_posts()) {
|
||||
?>
|
||||
<div class="related-posts">
|
||||
<h3><?php echo esc_html($title); ?></h3>
|
||||
<div class="related-posts-grid">
|
||||
<?php
|
||||
while ($related->have_posts()) {
|
||||
$related->the_post();
|
||||
?>
|
||||
<article class="related-post-item">
|
||||
<?php if (has_post_thumbnail()) : ?>
|
||||
<a href="<?php the_permalink(); ?>">
|
||||
<?php the_post_thumbnail('apus-thumbnail'); ?>
|
||||
</a>
|
||||
<?php endif; ?>
|
||||
<h4>
|
||||
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
|
||||
</h4>
|
||||
<div class="post-meta">
|
||||
<time datetime="<?php echo get_the_date('c'); ?>">
|
||||
<?php echo get_the_date(apus_get_date_format()); ?>
|
||||
</time>
|
||||
</div>
|
||||
</article>
|
||||
<?php
|
||||
}
|
||||
wp_reset_postdata();
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* EXAMPLE 5: Conditional comments display
|
||||
*/
|
||||
function example_maybe_show_comments() {
|
||||
if (is_single() && apus_comments_enabled_for_posts()) {
|
||||
comments_template();
|
||||
} elseif (is_page() && apus_comments_enabled_for_pages()) {
|
||||
comments_template();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* EXAMPLE 6: Featured image on single posts
|
||||
*/
|
||||
function example_display_featured_image() {
|
||||
if (is_single() && apus_show_featured_image_single() && has_post_thumbnail()) {
|
||||
?>
|
||||
<div class="post-thumbnail">
|
||||
<?php the_post_thumbnail('apus-featured-large'); ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* EXAMPLE 7: Author box on single posts
|
||||
*/
|
||||
function example_display_author_box() {
|
||||
if (is_single() && apus_show_author_box()) {
|
||||
$author_id = get_the_author_meta('ID');
|
||||
?>
|
||||
<div class="author-box">
|
||||
<div class="author-avatar">
|
||||
<?php echo get_avatar($author_id, 80); ?>
|
||||
</div>
|
||||
<div class="author-info">
|
||||
<h4 class="author-name"><?php the_author(); ?></h4>
|
||||
<p class="author-bio"><?php the_author_meta('description'); ?></p>
|
||||
<a href="<?php echo get_author_posts_url($author_id); ?>" class="author-link">
|
||||
<?php _e('View all posts', 'apus-theme'); ?>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* EXAMPLE 8: Social media links in footer
|
||||
*/
|
||||
function example_display_social_links() {
|
||||
$social_links = apus_get_social_links();
|
||||
|
||||
// Filter out empty links
|
||||
$social_links = array_filter($social_links);
|
||||
|
||||
if (!empty($social_links)) {
|
||||
?>
|
||||
<div class="social-links">
|
||||
<?php foreach ($social_links as $network => $url) : ?>
|
||||
<a href="<?php echo esc_url($url); ?>"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
class="social-link social-<?php echo esc_attr($network); ?>">
|
||||
<span class="screen-reader-text"><?php echo ucfirst($network); ?></span>
|
||||
<i class="icon-<?php echo esc_attr($network); ?>"></i>
|
||||
</a>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* EXAMPLE 9: Copyright text in footer
|
||||
*/
|
||||
function example_display_copyright() {
|
||||
$copyright = apus_get_copyright_text();
|
||||
|
||||
if ($copyright) {
|
||||
echo '<div class="copyright">' . wp_kses_post($copyright) . '</div>';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* EXAMPLE 10: Custom CSS in header
|
||||
*/
|
||||
function example_add_custom_css() {
|
||||
$custom_css = apus_get_custom_css();
|
||||
|
||||
if ($custom_css) {
|
||||
echo '<style type="text/css">' . "\n";
|
||||
echo strip_tags($custom_css);
|
||||
echo "\n</style>\n";
|
||||
}
|
||||
}
|
||||
add_action('wp_head', 'example_add_custom_css', 100);
|
||||
|
||||
/**
|
||||
* EXAMPLE 11: Custom JS in header
|
||||
*/
|
||||
function example_add_custom_js_header() {
|
||||
$custom_js = apus_get_custom_js_header();
|
||||
|
||||
if ($custom_js) {
|
||||
echo '<script type="text/javascript">' . "\n";
|
||||
echo $custom_js;
|
||||
echo "\n</script>\n";
|
||||
}
|
||||
}
|
||||
add_action('wp_head', 'example_add_custom_js_header', 100);
|
||||
|
||||
/**
|
||||
* EXAMPLE 12: Custom JS in footer
|
||||
*/
|
||||
function example_add_custom_js_footer() {
|
||||
$custom_js = apus_get_custom_js_footer();
|
||||
|
||||
if ($custom_js) {
|
||||
echo '<script type="text/javascript">' . "\n";
|
||||
echo $custom_js;
|
||||
echo "\n</script>\n";
|
||||
}
|
||||
}
|
||||
add_action('wp_footer', 'example_add_custom_js_footer', 100);
|
||||
|
||||
/**
|
||||
* EXAMPLE 13: Posts per page for archives
|
||||
*/
|
||||
function example_set_archive_posts_per_page($query) {
|
||||
if ($query->is_archive() && !is_admin() && $query->is_main_query()) {
|
||||
$posts_per_page = apus_get_archive_posts_per_page();
|
||||
$query->set('posts_per_page', $posts_per_page);
|
||||
}
|
||||
}
|
||||
add_action('pre_get_posts', 'example_set_archive_posts_per_page');
|
||||
|
||||
/**
|
||||
* EXAMPLE 14: Performance optimizations
|
||||
*/
|
||||
function example_apply_performance_settings() {
|
||||
// Remove emoji scripts
|
||||
if (apus_is_performance_enabled('remove_emoji')) {
|
||||
remove_action('wp_head', 'print_emoji_detection_script', 7);
|
||||
remove_action('wp_print_styles', 'print_emoji_styles');
|
||||
}
|
||||
|
||||
// Remove embeds
|
||||
if (apus_is_performance_enabled('remove_embeds')) {
|
||||
wp_deregister_script('wp-embed');
|
||||
}
|
||||
|
||||
// Remove Dashicons for non-logged users
|
||||
if (apus_is_performance_enabled('remove_dashicons') && !is_user_logged_in()) {
|
||||
wp_deregister_style('dashicons');
|
||||
}
|
||||
}
|
||||
add_action('wp_enqueue_scripts', 'example_apply_performance_settings', 100);
|
||||
|
||||
/**
|
||||
* EXAMPLE 15: Lazy loading images
|
||||
*/
|
||||
function example_add_lazy_loading($attr, $attachment, $size) {
|
||||
if (apus_is_lazy_loading_enabled()) {
|
||||
$attr['loading'] = 'lazy';
|
||||
}
|
||||
return $attr;
|
||||
}
|
||||
add_filter('wp_get_attachment_image_attributes', 'example_add_lazy_loading', 10, 3);
|
||||
|
||||
/**
|
||||
* EXAMPLE 16: Layout classes based on settings
|
||||
*/
|
||||
function example_get_layout_class() {
|
||||
$layout = 'right-sidebar'; // default
|
||||
|
||||
if (is_single()) {
|
||||
$layout = apus_get_default_post_layout();
|
||||
} elseif (is_page()) {
|
||||
$layout = apus_get_default_page_layout();
|
||||
}
|
||||
|
||||
return 'layout-' . $layout;
|
||||
}
|
||||
|
||||
/**
|
||||
* EXAMPLE 17: Display post meta conditionally
|
||||
*/
|
||||
function example_display_post_meta() {
|
||||
if (!apus_get_option('show_post_meta', true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
?>
|
||||
<div class="post-meta">
|
||||
<span class="post-date">
|
||||
<time datetime="<?php echo get_the_date('c'); ?>">
|
||||
<?php echo get_the_date(apus_get_date_format()); ?>
|
||||
</time>
|
||||
</span>
|
||||
<span class="post-author">
|
||||
<?php the_author(); ?>
|
||||
</span>
|
||||
<?php if (apus_get_option('show_post_categories', true)) : ?>
|
||||
<span class="post-categories">
|
||||
<?php the_category(', '); ?>
|
||||
</span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* EXAMPLE 18: Display post tags conditionally
|
||||
*/
|
||||
function example_display_post_tags() {
|
||||
if (is_single() && apus_get_option('show_post_tags', true)) {
|
||||
the_tags('<div class="post-tags">', ', ', '</div>');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* EXAMPLE 19: Get all options (for debugging)
|
||||
*/
|
||||
function example_debug_all_options() {
|
||||
if (current_user_can('manage_options') && isset($_GET['debug_options'])) {
|
||||
$all_options = apus_get_all_options();
|
||||
echo '<pre>';
|
||||
print_r($all_options);
|
||||
echo '</pre>';
|
||||
}
|
||||
}
|
||||
add_action('wp_footer', 'example_debug_all_options');
|
||||
|
||||
/**
|
||||
* EXAMPLE 20: Check if specific feature is enabled
|
||||
*/
|
||||
function example_check_feature() {
|
||||
// Multiple ways to check boolean options
|
||||
|
||||
// Method 1: Using helper function
|
||||
if (apus_is_option_enabled('enable_breadcrumbs')) {
|
||||
// Breadcrumbs are enabled
|
||||
}
|
||||
|
||||
// Method 2: Using get_option with default
|
||||
if (apus_get_option('enable_related_posts', true)) {
|
||||
// Related posts are enabled
|
||||
}
|
||||
|
||||
// Method 3: Direct check
|
||||
$options = apus_get_all_options();
|
||||
if (isset($options['enable_lazy_loading']) && $options['enable_lazy_loading']) {
|
||||
// Lazy loading is enabled
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user