วิธีใส่เดือนและปีปัจจุบันในชื่อโพสต์ของ WordPress โดยใช้ Shortcode (2023)

จะเห็นบ่อยใช่มั้ยกับการที่เวลาเราค้นหาอะไรสักอย่างใน Google แล้วมักจะมีเว็บที่มีชื่อบทความแล้วตามด้วยปี คศ ปัจจุบัน ไม่ก็มีเดือนด้วยต่อท้าย เพราะการทำแบบนี้นั้นจะทำให้ผู้คนที่ได้มาค้นพบเว็บเรานั้นรู้สึกว่าโพสต์ของเราถูกอัพเดทอยู่ตลอดเวลา ถือว่าเป็นทริคเล็ก ๆ ที่มีคนนิยมใช้กันมากเลย

วันนี้เราเลยจะมาสอนวิธีที่ใส่ปีและเดือนปัจจุบันลงในชื่อโพสต์ของเราโดยใช้ [year] และ [month] ที่เป็น Shortcode แบบนี้ โดยมีอยู่ 3 วิธีหลัก ๆ ดังนี้

  1. เพิ่มโค้ดลงไปใน functions.php วิธีนี้ผมจะไม่มีภาพวิธีเปิดไฟล์แต่สามารถนำโค้ดจากวิธีที่ 2 ไปใส่ได้เลย
  2. เพิ่มโค้ดลงไปใน Code Snippets
  3. ติดตั้งปลั๊กอิน 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 เพราะในบรรดาปลั๊กอินฟรีตัวนี้น่าจะโอเคสุดแล้วครับ

Code Snippets Plugin

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

Code Snippets Dashboard

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
Current Month and Year in Post Title Code

Enter title here เป็นชื่อของฟังชั่นเราที่จะแสดงอยู่หน้ารายการของ Code Snippets

Description จะใส่อะไรก็ได้เพื่อที่จะอธิบายเวลาเราเปิดเข้ามาดูโค้ด

เสร็จแล้วกด Save Changes and Activate

4.การเอา Shortcode ไปใช้งาน

คุณสามารถนำโค้ด shortcode [year] และ [month] ไปใส่ในชื่อโพสต์ของคุณได้เลย

การนำ [year] ไปใส่ใน Post Title
ตัวอย่างการแสดงผลปีปัจจุบันในชื่อโพสต์
ตัวอย่างโค้ด html ของหน้าเว็บ

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

Dynamic Month & Year into Posts Plugin

วิธีนี้สำหรับคนที่ไม่อยากไปยุ่งกับโค้ดสามารถติดตั้งปลั๊กอิน 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 ทั้งหมดที่ปลั๊กอินนี้มี

  1. [date] วันนี้ ตัวอย่าง March 27, 2023
  2. [year] ปีปัจจุบัน ตัวอย่าง 2023
  3. [pyear] ปีที่แล้ว ตัวอย่าง 2022
  4. [ppyear] ย้อนหลังไป 2 ปีจากปีนี้ ตัวอย่าง 2021
  5. [nyear] ปีหน้า ตัวอย่าง 2024
  6. [nnyear] 2 ปีจากปีนี้ ตัวอย่าง 2025
  7. [month] เดือนปัจจุบัน ตัวอย่าง March
  8. [nmonth] เดือนหน้า ตัวอย่าง April
  9. [pmonth] เดือนก่อนหน้า ตัวอย่าง February
  10. [mon] ชื่อย่อของเดือนปัจจุบัน ตัวอย่าง Mar
  11. [nmon] ชื่อย่อของเดือนหน้า ตัวอย่าง Apr
  12. [pmon] ชื่อย่อของเดือนก่อนหน้า ตัวอย่าง Feb
  13. [mm] เดือนปัจจุบันเป็นตัวเลขโดยมีศูนย์ต่อท้าย ตัวอย่าง 03
  14. [mn] เดือนปัจจุบันเป็นตัวเลขโดยไม่มีศูนย์ต่อท้าย ตัวอย่าง 3
  15. [dt] เดือนปัจจุบันเป็นตัวเลขโดยไม่มีศูนย์ต่อท้าย ตัวอย่าง 27
  16. [monthyear] แสดงเดือนและปีปัจจุบันพร้อมกัน ตัวอย่าง March 2023
  17. แสดงอักษรตัวพิมพ์ใหญ่ของชื่อเดือน โดยนำ c ไปใส่ข้างหน้า เช่น [cmonth], [cnmonth], [cmon] และอื่น ๆ
  18. [weekday] สำหรับวันนี้ของสัปดาห์ ตัวอย่าง Monday และ [wd] สำหรับวันแบบสั้น ตัวอย่าง Mon
  19. [blackfriday] แสดงวัน Black Friday ตัวอย่าง November 24
  20. [cybermonday] แสดงวันที่ Cyber ​​Monday ตัวอย่าง November 27

Related Posts