จะเห็นบ่อยใช่มั้ยกับการที่เวลาเราค้นหาอะไรสักอย่างใน Google แล้วมักจะมีเว็บที่มีชื่อบทความแล้วตามด้วยปี คศ ปัจจุบัน ไม่ก็มีเดือนด้วยต่อท้าย เพราะการทำแบบนี้นั้นจะทำให้ผู้คนที่ได้มาค้นพบเว็บเรานั้นรู้สึกว่าโพสต์ของเราถูกอัพเดทอยู่ตลอดเวลา ถือว่าเป็นทริคเล็ก ๆ ที่มีคนนิยมใช้กันมากเลย
วันนี้เราเลยจะมาสอนวิธีที่ใส่ปีและเดือนปัจจุบันลงในชื่อโพสต์ของเราโดยใช้ [year] และ [month] ที่เป็น Shortcode แบบนี้ โดยมีอยู่ 3 วิธีหลัก ๆ ดังนี้
- เพิ่มโค้ดลงไปใน functions.php วิธีนี้ผมจะไม่มีภาพวิธีเปิดไฟล์แต่สามารถนำโค้ดจากวิธีที่ 2 ไปใส่ได้เลย
- เพิ่มโค้ดลงไปใน Code Snippets
- ติดตั้งปลั๊กอิน Dynamic Month & Year into Posts
1.วิธีเพิ่มโค้ดลงไปใน functions.php
เข้าไปที่ Appearance -> Theme Editor และก็อปปี้โค้ดจากวิธีที่ 2 ลงไปใส่ได้เลย
2.วิธีเพิ่มโค้ดลงไปใน Code Snippets
1.ติดตั้งปลั๊กอิน Code Snippets
ก่อนอื่นเลยจะต้องติดตั้งปลั๊กอิน Code Snippets ก่อน เพื่อที่จะเพิ่มโค้ดลงไปใน WordPress ของเราได้ โดยจะมีปลั๊กอินแบบทั้งฟรีและเสียตังแล้วแต่ว่าใครถนัดใช้อันไหน โดยจะมีปลั๊กอินดังนี้ WPCodeBox, Scripts Organizer, Advanced Scripts, WPCode และ Code Snippets แต่วันนี้ผมจะมาสอนใช้ Code Snippets เพราะในบรรดาปลั๊กอินฟรีตัวนี้น่าจะโอเคสุดแล้วครับ

2.เปิดปลั๊กอิน Code Snippets แล้วก็กดปุ่ม Add New

3.เพิ่มโค้ด
ให้ก็อปปี้โค้ดด้านล่างนี้ลงไปในวางในช่อง Code
// สร้าง shortcode month เพื่อแสดงเดือนปัจจุบัน
// shortcode : [month]
add_shortcode( 'month' , 'current_month' );
function current_month() {
$month = date("F");
return "$month";
}
// สร้าง shortcode year เพื่อแสดงปีปัจจุบัน
// shortcode: [year]
add_shortcode( 'year' , 'current_year' );
function current_year() {
$year = date("Y");
return "$year";
}
add_filter( 'the_title', 'do_shortcode' ); // เปิดใช้งานฟังก์ชัน shortcode ในหัวข้อโพสต์
สำหรับคนที่ใช้ Rank Math ให้เพิ่มโค้ดด้านล่างนี้ต่อท้าย
add_filter( 'rank_math/frontend/title', 'do_shortcode' ); // เปิดใช้งานฟังก์ชัน shortcode ใน Rank Math Title
add_filter( 'rank_math/frontend/description', 'do_shortcode' ); // เปิดใช้งานฟังก์ชัน shortcode ใน Rank Math Meta Description
สำหรับคนที่ใช้ Yoast SEO ให้เพิ่มโค้ดด้านล่างนี้ต่อท้าย
add_filter( 'wpseo_title', 'do_shortcode' ); // เปิดใช้งานฟังก์ชัน shortcode ใน Yoast Title
add_filter( 'wpseo_metadesc', 'do_shortcode' ); // เปิดใช้งานฟังก์ชัน shortcode ใน Yoast Meta Description

Enter title here เป็นชื่อของฟังชั่นเราที่จะแสดงอยู่หน้ารายการของ Code Snippets
Description จะใส่อะไรก็ได้เพื่อที่จะอธิบายเวลาเราเปิดเข้ามาดูโค้ด
เสร็จแล้วกด Save Changes and Activate
4.การเอา Shortcode ไปใช้งาน
คุณสามารถนำโค้ด shortcode [year] และ [month] ไปใส่ในชื่อโพสต์ของคุณได้เลย



3.วิธีติดตั้งปลั๊กอิน Dynamic Month & Year into Posts

วิธีนี้สำหรับคนที่ไม่อยากไปยุ่งกับโค้ดสามารถติดตั้งปลั๊กอิน Dynamic Month & Year into Posts และใช้งาน Shortcode ได้เลย
รายชื่อปลั๊กอินที่ Dynamic Month & Year into Posts รองรับ
- Rank Math (100%)
- Rank Math Pro (100%)
- SEOPress
- SEOPress Premium
- Yoast SEO (limited due to their recent changes)
- Yoast SEO Premium (limited due to their recent changes)
- Gutenberg and Block Editor Content, Headings and Buttons
- GenerateBlocks, Ultimate Blocks, Kadence Blocks, Spectra, Otter Blocks
- Elementor Page Builder (100%)
- WP Bakery Page Builder
- Visual Composer
- Beaver Builder
- Brizy Builder
- Oxygen Builder
- Jetpack
- Intelly Related Posts a.k.a. Inline Related Posts (IRP)
- Advanced Custom Fields (Manual: please see the FAQs)
- Lightweight Accordion (including Schema)
- All major themes like default WordPress themes, Astra, Neve, Kadence, GeneratePress and Blocksy.
รายการ Shortcode ทั้งหมดที่ปลั๊กอินนี้มี
- [date] วันนี้ ตัวอย่าง March 27, 2023
- [year] ปีปัจจุบัน ตัวอย่าง 2023
- [pyear] ปีที่แล้ว ตัวอย่าง 2022
- [ppyear] ย้อนหลังไป 2 ปีจากปีนี้ ตัวอย่าง 2021
- [nyear] ปีหน้า ตัวอย่าง 2024
- [nnyear] 2 ปีจากปีนี้ ตัวอย่าง 2025
- [month] เดือนปัจจุบัน ตัวอย่าง March
- [nmonth] เดือนหน้า ตัวอย่าง April
- [pmonth] เดือนก่อนหน้า ตัวอย่าง February
- [mon] ชื่อย่อของเดือนปัจจุบัน ตัวอย่าง Mar
- [nmon] ชื่อย่อของเดือนหน้า ตัวอย่าง Apr
- [pmon] ชื่อย่อของเดือนก่อนหน้า ตัวอย่าง Feb
- [mm] เดือนปัจจุบันเป็นตัวเลขโดยมีศูนย์ต่อท้าย ตัวอย่าง 03
- [mn] เดือนปัจจุบันเป็นตัวเลขโดยไม่มีศูนย์ต่อท้าย ตัวอย่าง 3
- [dt] เดือนปัจจุบันเป็นตัวเลขโดยไม่มีศูนย์ต่อท้าย ตัวอย่าง 27
- [monthyear] แสดงเดือนและปีปัจจุบันพร้อมกัน ตัวอย่าง March 2023
- แสดงอักษรตัวพิมพ์ใหญ่ของชื่อเดือน โดยนำ c ไปใส่ข้างหน้า เช่น [cmonth], [cnmonth], [cmon] และอื่น ๆ
- [weekday] สำหรับวันนี้ของสัปดาห์ ตัวอย่าง Monday และ [wd] สำหรับวันแบบสั้น ตัวอย่าง Mon
- [blackfriday] แสดงวัน Black Friday ตัวอย่าง November 24
- [cybermonday] แสดงวันที่ Cyber Monday ตัวอย่าง November 27