Sắp xếp giá trị bài viết theo thứ tự bảng chữ cái cho mỗi chữ cái Wordpress

Thảo luận trong 'Mã nguồn mở Wordpress' bắt đầu bởi seolagi, 17/11/21.

  1. seolagi
    Tham gia ngày:
    16/4/14
    Bài viết:
    1,024
    Đã được thích:
    80
    Điểm thành tích:
    48
    Sắp xếp giá trị bài viết theo thứ tự bảng chữ cái cho mỗi chữ cái Wordpress, Wordpress posts ordering by alphabetical for each letter, Order in alphabetical order in wordpress, PHP Order in alphabetical order

    Sắp xếp giá trị theo bảng chữ cái A, B, C, D.... group dữ liệu lại, cái nào chứ giống nhau gôm vô 1 chỗ, giống hình dưới.

    Câu hỏi của bạn Hiếu gửi vào fanpage của itseovn.com. Mình đã từng làm qua, nên copy code hướng dẫn các bạn nào cần thì vào lấy nhé.

    sap-xep-du-lieu-abc.png

    PHP:
    <?php
    $posts 
    get_posts(
        array(
            
    'post_type' => 'post',
            
    'post_status' => 'publish',
            
    'posts_per_page' => -1,
            
    'orderby' => 'title',
            
    'order' => 'ASC',
        )
    );
    $alphas range('A''Z');
    $letter_keyed_posts = array();
    if ( 
    $posts ) {
        foreach ( 
    $posts as $post ) {
            
    $first_letter strtouppermb_substr$post->post_title01,'UTF-8' ) );   // substr( $post->post_title, 0, 1 ) nếu là tiếng anh không.
            
    $first_letter locdautiengviet($first_letter); //xử lý cho tiếng việt nếu là Â Ê Đ,... sẽ gộp vào chung với các ký tự không dấu. VD: Ê gộp chung vào E, Đ gộp chung vào D
            
    if ( ! array_key_exists$first_letter$letter_keyed_posts ) ) {
                
    $letter_keyed_posts$first_letter ] = array();
            }
             
    $letter_keyed_posts$first_letter ][] = $post;
        }
    }
    foreach (
    $letter_keyed_posts as $key => $value) {
        
    ?>
        <table class="table">
          <caption><h2 class="text-left">List of --> <?php echo $key.'('.count($value).')'?></h2></caption>
          <thead>
            <tr>
              <th scope="col">#</th>
              <th scope="col">Title</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($value as $val) {
                
    ?>
                <tr>
                  <th scope="row"><?php echo $val->ID?></th>
                  <td><a href="<?php echo get_permalink$val->ID ); ?>"><?php echo $val->post_title?></a></td>
                </tr>
                <?php
            
    ?>
          </tbody>
        </table>
        <?php
    }
    ?>
    Hoặc
    PHP:
    $terms get_terms( array(
        
    'taxonomy' => 'pa_hoat-chat',
        
    'hide_empty' => false//chỉ lấy hoạt chất có sản phẩm thì chỉnh thành true, false là lấy tất cả
      
    ) );

    $temp_char""// temporary variable, initially blank;
    foreach($terms as $term)
    {
        
    $album_name$term->name;

        
    $first_char_of_albm$album_name[0]; // this will store first alphabet;

        
    $first_char_of_albmstrtoupper($first_char_of_albm); // make uppercase or lower as per your needs

        
    if($temp_char!=$first_char_of_albm)
        {
            echo 
    $first_char_of_albm.'<br />';

            
    $temp_char$first_char_of_albm// update $temp_char variable
        
    }
        echo 
    $album_name.'<br />';
    }
     
    Cảm ơn đã xem bài:

    Sắp xếp giá trị bài viết theo thứ tự bảng chữ cái cho mỗi chữ cái Wordpress

    Last edited by a moderator: 2/12/21
  2. DienDanRaoVat
    Tham gia ngày:
    14/2/14
    Bài viết:
    182
    Đã được thích:
    5
    Điểm thành tích:
    18
    Cảm ơn itseovn nhé, hỏi facebook tùm lum, chỉ được ba cái trả lời (...ẩn bởi @admin). Hỏi itseovn hơi lâu trả lời mà lúc nào cũng đúng 100%, em đã làm thành công.
     
  3. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,878
    Đã được thích:
    1,193
    Điểm thành tích:
    113
    Giới tính:
    Nam
    @seolagi sao em không đưa cái code có style css và code em làm cho cái brand lần trước, cái đó a thấy đẹp và hoàn thiện hơn. Có sẵn css cho các bạn không biết làm áp dụng tiện hơn.
    Cách giá trị của hàm get_terms có thể gọi thêm:
    PHP:
    'taxonomy' => '',
    'orderby' => 'name',
    'order' => 'ASC',
    'hide_empty' => 1,
    'include' => array(),
    'exclude' => array(),
    'exclude_tree' => array(),
    'number' => '',
    'offset' => 0,
    'fields' => 'all',
    'name' => '',
    'slug' => '',
    'hierarchical' => true,
    'search' => '',
    'name__like' => '',
    'description__like' => '',
    'pad_counts' => false,
    'get' => '',
    'child_of' => 0,
    'parent' => '',
    'childless' => false,
    'cache_domain' => 'core',
    'update_term_meta_cache' => true,
    'meta_query' => null,
    'meta_key' => ''
     
    Chỉnh sửa cuối: 15/9/22
    seolagi thích bài này.
  4. seolagi
    Tham gia ngày:
    16/4/14
    Bài viết:
    1,024
    Đã được thích:
    80
    Điểm thành tích:
    48
    Ah xin lỗi, quên nhắc tới người chỉ mình lần trước làm Code, trên là code gốc của @admin chỉ mình, còn code của mình là lấy dữ liệu trường taxonomy (các thuộc tính) để tạo 1 thuộc tính mới, của woocommerce, trước mình làm cái brand từ taxonomy thuộc tính, sau đó mình muốn xuất dạng lọc danh sách tất cả các thuộc tính của brand thương hiệu ra dưới dạng danh sách theo ký tự A B C.

    Tạo code bằng cách tạo 1 file thuonghieu.php sau đó add code sau vào:
    PHP:
    <?php
    /*
    Template Name: Thuong hieu (brand)
    */
    get_header();

    $terms get_terms( array(
        
    'taxonomy' => 'pa_thuong-hieu',
        
    'hide_empty' => false//chỉ lấy hoạt chất có sản phẩm thì chỉnh thành true, false là lấy tất cả
      
    ) );

    $alphas range('A''Z');
    $letter_keyed_posts = array();
    if ( 
    $terms ) {
        foreach ( 
    $terms as $post ) {
            
    $first_letter strtoupper(mb_substr$post->name01,'UTF-8' ) );
            
    $first_letter locdautiengviet($first_letter);
            if ( ! 
    array_key_exists$first_letter$letter_keyed_posts ) ) {

                
    $letter_keyed_posts$first_letter ] = array();
            }

            
    $letter_keyed_posts$first_letter ][] = $post;
        }
    }
    $page_id get_queried_object_id();
    $yoast_title get_post_meta($page_id'_yoast_wpseo_title'true);
    $title strstr($yoast_title'%%'true);
    if (empty(
    $title)) {
        
    $title get_the_title($page_id);
    }

    $tocitem "";
    ?>
    <div class="cs-mainall">
        <div class="cs-container">
            <div id="breadcroumb" class="cs-breadcroumb">
                <ol>
                    <li><a href="/">Trang chủ</a></li>
                    <li><span class="divider">/</span> <a href="/thuong-hieu"><span>Thương hiệu</span></a></li>
                </ol>
            </div>
            <div class="cs-main-content">
                <div class="cs-ss-main-content">
                    <div class="cs-page-header">
                        <h1 class="cs-titlepage">
                            <?php echo 'Có <b>'.count($terms).'</b> '.$title?>
                        </h1>
                    </div>
                    <div class="cs-maincontent">
                        <?php foreach ($letter_keyed_posts as $key => $value) {
                            
    $tocitem .= '<li><a href="#thuong-hieu-'.$key.'">'.$key.'</a></li>';
                        
    ?>
                        <h2 id="thuong-hieu-<?php echo $key ?>"><?php echo $key.'<i>('.count($value).')</i>'?></h2>
                        <ul>
                            <?php foreach ($value as $val) { ?>
                                <li>
                                    <a href="<?php echo $val->slug?>"><?php echo $val->name?></a>
                                </li>
                            <?php ?>
                        </ul>
                        <?php  }  ?>
                    </div>
                    <div class="cs-footeritem">
                        <ul>
                            <?php echo $tocitem?>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php
    get_footer
    ();
    • Với pa_thuong-hieu là cái taxonomy của bạn nhé.
    thuoc-tinh.jpg

    Add thêm style css vào web để có giao diện đẹp:

    Mã:
    /* danh sach thuong hieu css */
    .cs-mainall .cs-ss-main-content{
    display: flex;
    flex-direction: column;
    }
    .cs-mainall .cs-ss-main-content .cs-page-header
    {
    order: 1;
    }
    .cs-mainall .cs-ss-main-content .cs-maincontent
    {
    order:3;
    }
    .cs-mainall .cs-ss-main-content .cs-footeritem
    {
    order: 2;
    }
    .cs-mainall .cs-footeritem ul {
        padding-top: 10px;
        border-top: 1px solid #ececec;
        border-bottom: 1px solid #ececec;
        margin-bottom: 20px;
    }
    .cs-mainall .cs-footeritem li
    {
    display: inline-block;
    font-size: 22px;
    margin: 0 0 10px 0;
    padding: 0 30px 0 0;
    color: #4b53e0;
    }
    .cs-mainall .cs-footeritem li > a {
        font-size: 22px;
        color: #4b53e0;
    }
    .cs-mainall .cs-maincontent ul
    {
    padding-top: 10px;
    border-top: 1px solid #ececec;
    border-bottom: 1px solid #ececec;
    margin-bottom: 20px;
    }
    .cs-mainall .cs-maincontent li
    {
    display: inline-block;
    font-size: 16px;
    margin: 0 0 10px 0;
    padding: 0 30px 0 0;
    color: #000;
    }
    .cs-mainall .cs-maincontent a
    {
    color: #000;
    }
    .cs-mainall
    {
    max-width: 1200px;
    margin: 0 auto;
    padding-left: 15px;
    padding-right: 15px;
    }
    .cs-mainall .cs-maincontent h2 i
    {
    font-size: 12px;
    font-style: normal;
    color: #000;
    vertical-align: 3px;
    margin-left: 7px;
    }
    .cs-mainall .cs-maincontent h2 {
        font-size: 22px;
        font-weight: bold;
        color: #4b53e0;
    }
    .cs-mainall .cs-titlepage
    {
    font-size: 24px;
    font-weight: bold;
    }
    .cs-mainall .cs-titlepage b
    {
    color:red;
    }
    .cs-mainall .cs-breadcroumb
    {
    padding: 10px 5px 7px 10px;
    background-color: #fff;
    margin-top: 20px;
    border-radius: 2px;
    border: 1px solid #eee;
    margin-bottom: 20px;
    }
    .cs-mainall .cs-main-content
    {
    background-color: #fff;
    padding: 15px 15px;
    margin-bottom: 20px;
    }
    .cs-mainall .cs-breadcroumb ol
    {
    margin-bottom: 0px;
    }
    .cs-mainall .cs-breadcroumb a
    {
    color: #4b53e0;
    text-decoration: none;
    padding-right: 10px;
    }
    .cs-mainall .cs-breadcroumb span
    {
    color: #333;
    text-decoration: none;
    padding-right: 10px;
    }
    .cs-mainall .cs-breadcroumb li
    {
    list-style: none;
    display: inline;
    }
    /* end danh sach thuong hieu css */
    Bạn muốn chỉnh lại Breadcrumb của chi tiết các thương hiệu thì chỉnh lại ở: /flatsome/woocommerce/global/breadcrumb.php nhé.

    Gọi ra ngoài phần chi tiết sản phẩm, chèn code sau vào function.php của themes:
    PHP:
    //add thương hiệu vào web
    add_action('woocommerce_single_product_summary','itseovn_product_hoat_chat',  15 );
    function 
    itseovn_product_hoat_chat() {
      global 
    $product;
      
    $term_obj_list get_the_terms$product->ID'pa_thuong-hieu' );
      
    $textthuonghieu '';
      if(
    count($term_obj_list) > 0)
      {
        foreach(
    $term_obj_list as $term)
        {
          
    $textthuonghieu .= $textthuonghieu == '' '<a href="/'.$term->slug.'">'.$term->name.'</a>' ', <a href="/'.$term->slug.'">'.$term->name.'</a>';
       
        }
        if(
    $textthuonghieu != '')
        {
          echo 
    '<div class="cs-thuonghieu"><span>Thương hiệu: '.$textthuonghieu.'</span></div>';
        }
      }
    }
    //end thương hiệu vào web
     
    Last edited by a moderator: 2/12/21
  5. thieubaotran87
    Tham gia ngày:
    1/3/21
    Bài viết:
    3
    Đã được thích:
    0
    Điểm thành tích:
    1
    Giới tính:
    Nữ
    Có sẵn css cho các bạn không biết làm áp dụng tiện hơn.
     
  6. nuoilo
    Tham gia ngày:
    11/3/22
    Bài viết:
    7
    Đã được thích:
    4
    Điểm thành tích:
    1
    Giới tính:
    Nữ
    bây giờ mình mới biết cái này, thank bạn
     
  7. buituan1999
    Tham gia ngày:
    18/3/22
    Bài viết:
    1
    Đã được thích:
    0
    Điểm thành tích:
    1
    Giới tính:
    Nam
    bây giờ mình mới biết cái này, cảm ơn bạn
     
  8. Nguyễn Tấn Long
    Tham gia ngày:
    30/1/16
    Bài viết:
    6
    Đã được thích:
    0
    Điểm thành tích:
    1
    Giới tính:
    Nam
    chèn code vào mình hay bị lỗi lắm