Code thêm và hiển thị tin tức nổi bật trên theme Flatsome

Code thêm và hiển thị tin tức nổi bật trên theme Flatsome

14/04/2023 - 217

Trước tiên ta cần thêm nút tích chọn tin nổi bật vào chi tiết bài viết như hình bên dưới

Để làm được điều này các bạn thêm đoạn code bên dưới vào file function.php nhé:

// Thêm trường dữ liệu meta "featured_post" cho bài viết
function flatsome_add_featured_post_meta_box() {
    add_meta_box( 'flatsome-featured-post', __( 'Tin nổi bật', 'flatsome' ), 'flatsome_featured_post_meta_box', 'post', 'side' );
}
add_action( 'add_meta_boxes', 'flatsome_add_featured_post_meta_box' );

// Hiển thị nút "Tin nổi bật" cho bài viết
function flatsome_featured_post_meta_box( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'flatsome_featured_post_nonce' );

    $value = get_post_meta( $post->ID, 'featured_post', true );
    $checked = $value ? 'checked="checked"' : '';
    $label = __( 'Đánh dấu bài viết này là tin nổi bật', 'flatsome' );

    echo '<label><input type="checkbox" name="featured_post" value="1" ' . $checked . '> ' . $label . '</label>';
}

// Lưu trường dữ liệu meta "featured_post" khi lưu bài viết
function flatsome_save_featured_post_meta_box( $post_id ) {
    if ( ! isset( $_POST['flatsome_featured_post_nonce'] ) || ! wp_verify_nonce( $_POST['flatsome_featured_post_nonce'], basename( __FILE__ ) ) ) {
        return;
    }

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }

    if ( isset( $_POST['featured_post'] ) && $_POST['featured_post'] ) {
        update_post_meta( $post_id, 'featured_post', 1 );
    } else {
        delete_post_meta( $post_id, 'featured_post' );
    }
}
add_action( 'save_post', 'flatsome_save_featured_post_meta_box' );

Sau đó các bạn muốn lấy tin tức nổi bật ra ngoài web thì các bạn thêm tiếp đoạn code sau vào file function.php

function tintucnoibat(){
    ?>
    <aside class="widget">
        <span class="widget-title "><span>Tin tức nổi bật</span></span>
        <div class="tinnoibat">
            <?php
                $args = array(
                    'post_type' => 'post',
                    'posts_per_page' => 5, // Số bài viết hiển thị
                    'meta_key' => 'featured_post', // Tên trường dữ liệu meta để đánh dấu bài viết nổi bật
                    'meta_value' => '1', // Giá trị của trường dữ liệu meta để đánh dấu bài viết nổi bật
                    'orderby' => 'date', // Sắp xếp theo ngày đăng bài
                    'order' => 'DESC' // Sắp xếp theo thứ tự giảm dần (tức là bài mới nhất sẽ xuất hiện đầu tiên)
                );

                $query = new WP_Query( $args );

                // Kiểm tra nếu có bài viết
                if ( $query->have_posts() ) {
                    // Hiển thị danh sách các bài viết

                    while ( $query->have_posts() ) {
                        $query->the_post();
                        ?>
                        <div class="new-list-post">
                            <div class="post-image">
                                <a href="<?php the_permalink();?>" title="<?php the_title();?>">
                                    <?php if(has_post_thumbnail()) the_post_thumbnail("full",array("alt" => get_the_title()));
                                    else echo "No Image"; ?>
                                </a>
                            </div>
                            <div class="post-content">
                                <div class="orenda_timer3">
                                    <span><?php the_time('d/m/Y'); ?></span> - <span><?php the_author(); ?></span>
                                </div>
                                <h3><a class="title" href="<?php the_permalink();?>" title="<?php the_title();?>"><?php echo the_title();?></a></h3>
                            </div>
                        </div>
                        <?php
                    }

                }

                // Đặt lại truy vấn
                wp_reset_postdata();
            ?>
        </div>
    </aside>
    <?php
}
add_shortcode('show_tintucnoibat', 'tintucnoibat');

Cuối cùng các bạn chỉ cần dán shortcode [show_tintucnoibat] ở nơi các bạn muốn hiển thị tin tức nổi bật, chúc các bạn thành công!

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Bài viết liên quan
Contact Me on Zalo