Code thêm kiểu hiển thị riêng cho 1 chuyên mục bài viết đặc biệt

Có thể bạn quan tâmx
-30%
Giá gốc là: 1,000,000 ₫.Giá hiện tại là: 700,000 ₫.
-30%
Giá gốc là: 1,000,000 ₫.Giá hiện tại là: 700,000 ₫.
-30%
Giá gốc là: 1,000,000 ₫.Giá hiện tại là: 700,000 ₫.

Đôi khi bạn cần thêm 1 layout hiển thị riêng cho 1 chuyên mục , thì đoạn code sau sẽ giúp bạn làm điều đó. Trong ví dụ trên, mình chọn giao diện mặc định là 3 col. Do đó các bạn sẽ chỉnh sửa file flatsome/templates/parts/post/archive-3-col.php (các bạn nhớ copy qua child theme để update không bị mất).

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
/**
* Posts archive 3 column.
*
* @package Flatsome\Templates
* @flatsome-version 3.18.0
*/
if ( have_posts() ) :
$parent_cat_id = 1;
$child_categories = get_term_children($parent_cat_id, 'category');
if (has_category(1) || has_category($child_categories)) {
?>
<div id="post-list">
<?php
$ids = array();
while ( have_posts() ) : the_post();
array_push( $ids, get_the_ID() );
endwhile; // end of the loop.
$ids = implode( ',', $ids );
?>
<?php
echo flatsome_apply_shortcode( 'blog_posts', array(
'type'=>'grid',
'grid'=>'6',
'col_spacing'=>'small',
'grid_height'=>'500px',
'image_hover'=>'zoom',
'text_align' =>'left',
'style'=>'shade',
'image_size'=>'original',
'depth' => get_theme_mod( 'blog_posts_depth', 0 ),
'depth_hover' => get_theme_mod( 'blog_posts_depth_hover', 0 ),
'text_align' => get_theme_mod( 'blog_posts_title_align', 'center' ),
'columns' => '3',
'show_date' => 'false',
'ids' => $ids,
) );
?>
<?php flatsome_posts_pagination(); ?>
</div>
<?php
}else{?>
<div id="post-list-tin">
<?php
$ids = array();
while ( have_posts() ) : the_post();
array_push( $ids, get_the_ID() );
endwhile; // end of the loop.
$ids = implode( ',', $ids );
?>
<?php
echo flatsome_apply_shortcode( 'blog_posts', array(
'type' => get_theme_mod( 'blog_style_type', 'masonry' ),
'depth' => get_theme_mod( 'blog_posts_depth', 0 ),
'depth_hover' => get_theme_mod( 'blog_posts_depth_hover', 0 ),
'text_align' => get_theme_mod( 'blog_posts_title_align', 'center' ),
'columns' => '4',
'show_date' => 'text',
'ids' => $ids,
'excerpt_length' => '100'
) );
?>
<?php flatsome_posts_pagination(); ?>
</div>
<?php
}
else : ?>
<?php get_template_part( 'template-parts/posts/content', 'none' ); ?>
<?php endif; ?>
<?php /** * Posts archive 3 column. * * @package Flatsome\Templates * @flatsome-version 3.18.0 */ if ( have_posts() ) : $parent_cat_id = 1; $child_categories = get_term_children($parent_cat_id, 'category'); if (has_category(1) || has_category($child_categories)) { ?> <div id="post-list"> <?php $ids = array(); while ( have_posts() ) : the_post(); array_push( $ids, get_the_ID() ); endwhile; // end of the loop. $ids = implode( ',', $ids ); ?> <?php echo flatsome_apply_shortcode( 'blog_posts', array( 'type'=>'grid', 'grid'=>'6', 'col_spacing'=>'small', 'grid_height'=>'500px', 'image_hover'=>'zoom', 'text_align' =>'left', 'style'=>'shade', 'image_size'=>'original', 'depth' => get_theme_mod( 'blog_posts_depth', 0 ), 'depth_hover' => get_theme_mod( 'blog_posts_depth_hover', 0 ), 'text_align' => get_theme_mod( 'blog_posts_title_align', 'center' ), 'columns' => '3', 'show_date' => 'false', 'ids' => $ids, ) ); ?> <?php flatsome_posts_pagination(); ?> </div> <?php }else{?> <div id="post-list-tin"> <?php $ids = array(); while ( have_posts() ) : the_post(); array_push( $ids, get_the_ID() ); endwhile; // end of the loop. $ids = implode( ',', $ids ); ?> <?php echo flatsome_apply_shortcode( 'blog_posts', array( 'type' => get_theme_mod( 'blog_style_type', 'masonry' ), 'depth' => get_theme_mod( 'blog_posts_depth', 0 ), 'depth_hover' => get_theme_mod( 'blog_posts_depth_hover', 0 ), 'text_align' => get_theme_mod( 'blog_posts_title_align', 'center' ), 'columns' => '4', 'show_date' => 'text', 'ids' => $ids, 'excerpt_length' => '100' ) ); ?> <?php flatsome_posts_pagination(); ?> </div> <?php } else : ?> <?php get_template_part( 'template-parts/posts/content', 'none' ); ?> <?php endif; ?>
<?php
/**
 * Posts archive 3 column.
 *
 * @package          Flatsome\Templates
 * @flatsome-version 3.18.0
 */

if ( have_posts() ) : 

$parent_cat_id = 1;
$child_categories = get_term_children($parent_cat_id, 'category');
if (has_category(1) || has_category($child_categories)) {
?>

    <div id="post-list">
        <?php
        $ids = array();
        while ( have_posts() ) : the_post();
            array_push( $ids, get_the_ID() );
        endwhile; // end of the loop.
        $ids = implode( ',', $ids );
        ?>

        <?php
        
        echo flatsome_apply_shortcode( 'blog_posts', array(
            'type'=>'grid',
            'grid'=>'6',
            'col_spacing'=>'small',
            'grid_height'=>'500px',
            'image_hover'=>'zoom',
            'text_align' =>'left',
            'style'=>'shade',
        'image_size'=>'original',
            'depth'       => get_theme_mod( 'blog_posts_depth', 0 ),
            'depth_hover' => get_theme_mod( 'blog_posts_depth_hover', 0 ),
            'text_align'  => get_theme_mod( 'blog_posts_title_align', 'center' ),
            'columns'     => '3',
            'show_date'   => 'false',
            'ids'         => $ids,
        
        ) );
        ?>

        <?php flatsome_posts_pagination(); ?>
    </div>
<?php




}else{?>



<div id="post-list-tin">

        <?php
        $ids = array();
        while ( have_posts() ) : the_post();
            array_push( $ids, get_the_ID() );
        endwhile; // end of the loop.
        $ids = implode( ',', $ids );
        ?>

        <?php
        echo flatsome_apply_shortcode( 'blog_posts', array(
            'type'        => get_theme_mod( 'blog_style_type', 'masonry' ),
            'depth'       => get_theme_mod( 'blog_posts_depth', 0 ),
            'depth_hover' => get_theme_mod( 'blog_posts_depth_hover', 0 ),
            'text_align'  => get_theme_mod( 'blog_posts_title_align', 'center' ),
            'columns'     => '4',
            'show_date'   => 'text',
            'ids'         => $ids,
            'excerpt_length' => '100'
        ) );
        ?>

        <?php flatsome_posts_pagination(); ?>
    </div>

<?php
}
 else : ?>

    <?php get_template_part( 'template-parts/posts/content', 'none' ); ?>

<?php endif; ?>

Chỗ $parent_cat_id = 1 : các bạn thay lại thành ID của chuyên mục cần layout đặc biệt . Lúc này các bạn có thể chỉnh sửa layout hiển thị của chuyên mục này ở mục <div id=”post-list”> . Còn lại sẽ hiện thị theo kiểu mặc định phía dưới chỗ post-list-tin.

Bài viết liên quan