File: /home/txcuakhau/domains/thuexechuyencuakhau.com/public_html/wp-content/themes/k-tech/functions.php
<?php
add_action('wp_enqueue_scripts', 'k_tech_style');
function k_tech_style() {
// wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css');
wp_enqueue_style('child-style', get_stylesheet_directory_uri() . '/style.css', array('parent-style'));
}
function shortcode_danh_muc_thue_xe($atts) {
ob_start();
// Lấy danh sách taxonomy
$terms = get_terms(array(
'taxonomy' => 'danh-muc-thue-xe', // Đảm bảo slug taxonomy đúng
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC'
));
if (!empty($terms) && !is_wp_error($terms)) {
echo '<div class="list-danh-muc-thue-xe">';
foreach ($terms as $term) {
// Lấy dữ liệu ACF từ term
$anh_dai_dien = get_field('anh_dai_dien', 'term_' . $term->term_id);
$gia_danh_muc = get_field('gia_danh_muc', 'term_' . $term->term_id);
$link = get_term_link($term);
// Xử lý link nếu bị lỗi
if (is_wp_error($link)) {
continue;
}
?>
<div class="item">
<div class="imgz imgH" style="padding: 36%">
<a href="<?php echo esc_url($link); ?>">
<?php if ($anh_dai_dien): ?>
<?php if (is_array($anh_dai_dien)): ?>
<img src="<?php echo esc_url($anh_dai_dien['url']); ?>" alt="<?php echo esc_attr($term->name); ?>">
<?php else: ?>
<img src="<?php echo esc_url($anh_dai_dien); ?>" alt="<?php echo esc_attr($term->name); ?>">
<?php endif; ?>
<?php else: ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/placeholder.jpg" alt="<?php echo esc_attr($term->name); ?>">
<?php endif; ?>
</a>
</div>
<div class="over nd">
<div class="center">
<h3 class="td line2">
<a href="<?php echo esc_url($link); ?>"><?php echo esc_html($term->name); ?></a>
</h3>
<?php if ($gia_danh_muc): ?>
<div class="giaxe fs-4">
Giá: <?php echo esc_html($gia_danh_muc); ?>
</div>
<?php endif; ?>
</div>
</div>
<div class="hover nd">
<div class="center">
<h3 class="td line1">
<a href="<?php echo esc_url($link); ?>"><?php echo esc_html($term->name); ?></a>
</h3>
<?php if ($gia_danh_muc): ?>
<div class="giaxe fs-4">
Giá: <?php echo esc_html($gia_danh_muc); ?>
</div>
<?php endif; ?>
<a class="link btn btn-outline-light" href="<?php echo esc_url($link); ?>">Xem chi tiết</a>
</div>
</div>
</div>
<?php
}
echo '</div>';
} else {
echo '<p>Chưa có danh mục thuê xe nào.</p>';
}
return ob_get_clean();
}
add_shortcode('danh_muc_thue_xe', 'shortcode_danh_muc_thue_xe');
// Thêm Bootstrap 5 vào Flatsome
function mytheme_enqueue_bootstrap() {
// CSS
wp_enqueue_style( 'bootstrap-css', 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css' );
// JS (bundle đã có Popper)
wp_enqueue_script( 'bootstrap-js', 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js', array(), null, true );
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_bootstrap' );
function shortcode_list_danh_muc_thue_xe_active() {
$current_term = get_queried_object();
$terms = get_terms(array(
'taxonomy' => 'danh-muc-thue-xe',
'hide_empty' => false,
));
ob_start();
if (!empty($terms) && !is_wp_error($terms)) {
echo '<ul class="list-danh-muc-thue-xe">';
foreach ($terms as $term) {
$active_class = ($current_term && isset($current_term->term_id) && $current_term->term_id === $term->term_id) ? 'active' : '';
echo '<li class="' . esc_attr($active_class) . '">';
echo '<a href="' . esc_url(get_term_link($term)) . '">' . esc_html($term->name) . '</a>';
echo '</li>';
}
echo '</ul>';
}
return ob_get_clean();
}
add_shortcode('list_danh_muc_thue_xe', 'shortcode_list_danh_muc_thue_xe_active');
function qd_override_shortcodes()
{
require get_stylesheet_directory() . '/inc/shortcodes/blog_posts.php';
}
add_action('wp_loaded', 'qd_override_shortcodes', 10);
function related_cat() {
$output = '';
if (is_single()) {
global $post;
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>6,
'ignore_sticky_posts'=>1
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ):
$output .= '<div class="related-box">';
$output .= '<span class="related-head">Bài viết liên quan:</span><div class="row related-post">';
while ($my_query->have_posts()):$my_query->the_post();
$output .=
'<div class="col large-4">
<a href="'.get_the_permalink().'" title="'.get_the_title().'">
<div class="feature">
<div class="image" style="background-image:url('. get_the_post_thumbnail_url() .');"></div>
</div>
</a>
<div class="related-title"><a href="'.get_the_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a></div>
</div>';
endwhile;
$output .= '</div>';
$output .= '</div>';
endif; //End if.
wp_reset_query();
}
return $output;
}
}
add_shortcode('related_cat','related_cat');