Tùy biến giao diện cho từng trang trong WordPress

5/5 - (1 bình chọn)

Trong bài này, mình sẽ chia sẻ cho các bạn cách làm thế nào để có thể tạo ra được một template Custom Single Post cho từng bài viết, cho từng tác giả hay cho từng category riêng biệt trong WordPress. Để có thể nắm được những hướng dẫn trong bài, yêu cầu bạn phải có các kiến thức cơ bản về HTML – CSS và hiểu về cách thức làm việc của theme WordPress.

custom post type wordpress

Template Custom Single Post cho các bài viết khác nhau

Kỹ thuật này cho phép bạn tạo ra các template Custom Single Post và bạn có thể lựa chọn template cần sử dụng cho mỗi bài viết mà bạn đang viết.
Để làm được việc này, bạn phải tải plugin Single Post Template về và thực hiện theo các hướng dẫn sau đây. Tiếp theo, bạn tạo một file custom, ví dụ ở đây mình tạo một file tên là customsinglepost.php.
Nội dung trong file này bạn có thể copy từ file single.php rồi thay đổi lại theo mong muốn của bạn, hoặc bạn cũng có thể tự tạo ra một thiết kế dành cho riêng bạn. Tuy nhiên, bạn nhớ phải thêm đoạn code sau vào đầu file mà bạn tạo ra:
<?php/*Single Post Template: [Mô tả tên template]Mô tả: Phần này không bắt buộc nhưng giúp ích cho việc mô tả Post Template*/?>

Sau khi tạo xong template custom cho single post, bạn lưu template đó vào các file theme khác trong thư mục theme của bạn. Để xem plugin đã được kích hoạt hay chưa, bạn vào bảng quản trị WordPress thử viết một bài mới sẽ thấy một drop-down box cung cấp cho bạn các tùy chọn để bạn lựa chọn template mà bạn muốn cho bài viết.

Capture
Ghi chú: Nếu bạn không chọn bất kỳ template nào, nó sẽ tự động hiển thị template mặc định. Bạn có thể tạo nhiều template như ý muốn. Có một plugin khác cũng thực hiện được công việc tương tự như trên, tên là Custom Post Template. Phương pháp trên là lý tưởng để tạo template Custom Single Post cho từng bài viết.
Nếu bạn muốn có một template hoàn toàn khác nhau cho từng category thì phương pháp trên có thể tự động hóa chỉ với một vài code trong file của bạn. Điều này sẽ giúp bạn tiết kiệm được thời gian phải đi chỉnh sửa và chọn template riêng cho từng bài, nhất là khi bạn có rất nhiều bài viết đã publish trong category đó.
Các phương pháp tự động này được một trong những nhà lập trình theme WordPress nổi tiếng là Justin Tadlock chia sẻ.

Template Custom Single Post cho từng Category riêng biệt

Để sử dụng template single post riêng cho từng category, bạn thêm function sau đây vào file functions.php trong theme của bạn:
// Định nghĩa đường dẫn mặc định cho folder template singledefine(SINGLE_PATH, TEMPLATEPATH . '/single'); // Lọc single_template bằng custom functionadd_filter('single_template', 'my_single_template'); // Function template single chọn templatefunction my_single_template($single){     global $wp_query, $post;      // Kiểm tra template single theo category     // Kiểm tra category slug và ID     foreach((array)get_the_category() as $cat) :      if(file_exists(SINGLE_PATH . '/single-cat-' . $cat->slug . '.php'))          return SINGLE_PATH . '/single-cat-' . $cat->slug . '.php';      else if(file_exists(SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php'))          return SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php';      endforeach;}

Template Custom Single Post cho từng tác giả riêng biệt

Bạn đơn giản chỉ cần đặt code sau vào file functions.php và nó sẽ báo cho WordPress để tìm kiếm các template như single-author-admin.php hoặc single-author-1.php nếu nó không tìm thấy template trong folder /single của bạn.
// Định nghĩa đường dẫn mặc định cho folder template singledefine(SINGLE_PATH, TEMPLATEPATH . '/single'); // Lọc single_template bằng custom functionadd_filter('single_template', 'my_single_template'); // Function template single chọn templatefunction my_single_template($single){     global $wp_query, $post;      // Kiểm tra template single theo tác giả     // Kiểm tra user nicename và ID     $curauth = get_userdata($wp_query->post->post_author);      if(file_exists(SINGLE_PATH . '/single-author-' . $curauth->user_nicename . '.php'))          return SINGLE_PATH . '/single-author-' . $curauth->user_nicename . '.php';      else if(file_exists(SINGLE_PATH . '/single-author-' . $curauth->ID . '.php'))          return SINGLE_PATH . '/single-author-' . $curauth->ID . '.php';}

Mình không thể chỉ rõ cho bạn từng cách tạo template Custom Single Post bởi vì việc này tùy thuộc vào bạn và những gì mà bạn muốn thực hiện. Lưu ý quan trọng là bạn phải lưu tất cả các template custom trong folder name/single và folder này phải được đặt trong thư mục theme của bạn. Chúc bạn thành công.

5/5 - (1 bình chọn)


  1. c88 login

    1. C88 Fun – Boundless Entertainment Awaits!
    C88 Fun is more than a gaming platform; it’s an expedition waiting to be embraced. Featuring an intuitive interface and a diverse array of games, C88 Fun caters to all tastes. From timeless classics to cutting-edge releases, C88 Fun ensures every player discovers their gaming haven.

    2. JILI & Evo 100% Welcome Bonus – A Grand Welcome Awaits Newcomers!
    Embark on your gaming journey with a grand welcome from C88. New members are greeted with a 100% Welcome Bonus from JILI & Evo, doubling the excitement right from the beginning. This bonus acts as a catalyst for players to explore the diverse array of games available on the platform.

    3. C88 First Deposit Get 2X Bonus – Double the Excitement!
    At C88, generosity towards players is a fundamental principle. With the “First Deposit Get 2X Bonus” offer, players can revel in double the fun on their initial deposit. This promotion enhances the gaming experience, providing more avenues to win big across various games.

    4. 20 Spin Times = Get Big Bonus (8,888P) – Spin Your Way to Greatness!
    Spin your way to substantial bonuses with the “20 Spin Times” promotion. Accumulate spins and stand a chance to win an impressive bonus of 8,888P. This promotion adds an extra layer of excitement to the gameplay, combining luck and strategy for maximum enjoyment.

    5. Daily Check-in = Turnover 5X?! – Daily Rewards Await!
    Consistency is paramount at C88. By simply logging in daily, players not only bask in the thrill of gaming but also stand a chance to multiply their turnovers by 5X. Daily check-ins bring additional perks, making every day a rewarding experience for dedicated players.

    6. 7 Day Deposit 300 = Get 1,500P – Unlock Deposit Rewards!
    For those eager to seize opportunities, the “7 Day Deposit” promotion is a game-changer. Deposit 300 and receive a generous reward of 1,500P. This promotion encourages players to explore the platform further and maximize their gaming potential.

    7. Invite 100 Users = Get 10,000 PESO – Share the Excitement!
    C88 believes in the strength of community. Invite friends and fellow gamers to join the excitement, and for every 100 users, receive an incredible reward of 10,000 PESO. Sharing the joy of gaming has never been more rewarding.

    8. C88 New Member Get 100% First Deposit Bonus – Exclusive Benefits!
    New members are in for a treat with an exclusive 100% First Deposit Bonus. C88 ensures that everyone kicks off their gaming journey with a boost, setting the stage for an exhilarating experience filled with opportunities to win.

    9. All Pass Get C88 Extra Big Bonus 1000 PESO – Unlock Unlimited Rewards!
    For avid players exploring every nook and cranny of C88, the “All Pass Get C88 Extra Big Bonus” offers an additional 1000 PESO. This promotion rewards those who embrace the full spectrum of games and features available on the platform.

    Ready to immerse yourself in the excitement? Visit C88 now and unlock a world of gaming like never before. Don’t miss out on the excitement, bonuses, and wins that await you at C88. Join the community today, and let the games begin! #c88 #c88login #c88bet #c88bonus #c88win

Trả lời

Email của bạn sẽ không được hiển thị công khai.