As you can see, the default excerpt length is still 150, but we’ve also applied
some filters to it. A filter allows you to write a function that modifies the
value of something — in this case, the excerpt’s length. The name (or tag) of
this filter is excerpt_length, and if no functions are attached to it, then its
value will remain 150. Let’s see how we can now use this to modify the
default value.
First, we have defined a function that does nothing but return a number. At
this point, nothing is using the function, so let’s tell WordPress that we want
to hook this into the excerpt_length filter.
We’ve successfully changed the default excerpt length in WordPress,
without touching the original function and without even having to write a
custom excerpt function. This will be extremely useful, because if you
always want excerpts that are 200 characters long, just add this as a filter
and then you won’t have to specify it every time.
Suppose you want to tack on some more text, like “Read on,” to the end of
the excerpt. We could modify our original function to work with a hook and
then tie a function to that hook, like so:
This hook is placed at the end of the function and allows us to modify its
end result. This time, we’ve also passed the output that the function would
normally produce as a parameter to our hook. The function that we tie to
this hook will receive this parameter.
All we are doing in our function is taking the original contents of $excerpt
and appending our “Read on” text to the end. But if we choose, we could
also return the text “Click the title to read this article,” which would replace
the whole excerpt.
While our example is a bit redundant, since WordPress already has a better
function, hopefully you’ve gotten to grips with the thinking behind hooks.
Let’s look more in depth at what goes on with filters, actions, priorities,
arguments and the other yummy options available.
Filters And Actions
Filters and actions are two types of hooks. As you saw in the previous
section, a filter modifies the value of something. An action, rather than
modifying something, calls another function to run beside it.
A commonly used action hook is wp_head. Let’s see how this works. You
may have noticed a function at the bottom of your website’s head section
named wp_head(). Diving into the code of this function, you can see that it
contains a call to do_action(). This is similar to apply_filters(); it
means to run all of the functions that are tied to the wp_head tag.
Let’s put a copyright meta tag on top of each post’s page to test how this
works.
ที่คุณสามารถดู , เริ่มต้นตัดตอนความยาวยังเป็น 150 เลย แต่เราก็ยังใช้
ตัวกรองบางมัน ตัวกรองช่วยให้คุณสามารถเขียนฟังก์ชันที่ปรับเปลี่ยน
ค่าบางอย่าง ในกรณีนี้ความยาวของข้อความที่ตัดตอนมา . ชื่อ ( หรือแท็ก )
excerpt_length กรองนี้ และถ้าไม่มีฟังก์ชั่นที่แนบมากับมัน แล้วค่าของ
จะยังคง 150 เรามาดูวิธีที่เราสามารถใช้เพื่อปรับเปลี่ยน
ค่าโดยปริยาย
ก่อนอื่นเราต้องกำหนดฟังก์ชันที่ไม่ได้ทำอะไรแต่กลับหมายเลข ที่
จุดนี้ ไม่มีอะไรใช้ ฟังก์ชัน ดังนั้นบอกคลิป ที่เราต้องการ
ตะขอนี้เข้าไปในกรอง excerpt_length
เราสามารถเปลี่ยนค่าเริ่มต้นข้อความที่ตัดตอนมาความยาวใน WordPress
โดยไม่ต้องสัมผัสฟังก์ชั่นเดิมโดยไม่ต้องเขียน
เองตัดตอนฟังก์ชันนี้จะเป็นประโยชน์อย่างมาก เพราะถ้าคุณ
ต้องการตัดตอนที่ 200 ตัวอักษรยาว เพียงแค่เพิ่มนี้เป็นตัวกรอง
แล้วคุณจะไม่ต้องระบุทุกครั้ง
สมมติว่าคุณต้องการเพิ่มบางข้อความ เช่น " อ่านต่อ " ที่ส่วนท้ายของข้อความที่ตัดตอนมา
. เราสามารถปรับเปลี่ยนฟังก์ชันเดิมของเราที่จะทำงานกับตะขอและ
แล้วผูกฟังก์ชันเบ็ดที่เช่นดังนั้น :
ตะขอนี้วางไว้ในตอนท้ายของหน้าที่ และช่วยให้เราสามารถปรับเปลี่ยนของ
จบผล ในครั้งนี้ เรายังผ่านออกที่ฟังก์ชันจะ
โดยปกติผลิตเป็นพารามิเตอร์เบ็ดของเรา ฟังก์ชันที่เราผูกกับเบ็ดนี้จะรับค่า
ทั้งหมดนี้ เรากำลังทำในการทำงานของเราคือ เอาเนื้อหาเดิมของ $ ตัดตอน
และท้ายของเรา " อ่าน " ข้อความสุดท้ายแต่ถ้าเราเลือก เราสามารถ
กลับไปยังข้อความ " คลิกที่ชื่อเรื่องเพื่ออ่านบทความนี้ " ซึ่งจะแทนที่ข้อความที่ตัดตอนมาทั้งหมด
ในขณะที่ตัวอย่างของเราเป็นบิตมากเกินไป เนื่องจาก WordPress มีฟังก์ชันดีกว่า
, หวังว่าคุณจะเข้าใจความคิดเบื้องหลัง hooks .
กันเถอะ ดูเพิ่มเติมในเชิงลึกที่เกี่ยวข้องกับตัวกรอง , ลำดับการกระทำ , ,
ข้อโต้แย้งและอร่อยอื่น ๆตัวเลือกพร้อมใช้งาน ตัวกรองและการกระทำ
ตัวกรองและการกระทำเป็นสองประเภทของตะขอ ตามที่คุณเห็นในส่วนก่อนหน้า
, ตัวกรองปรับค่าบางอย่าง การกระทำ มากกว่า
ปรับเปลี่ยนบางอย่าง เรียกฟังก์ชันอื่นที่วิ่งอยู่ข้างๆ ที่ใช้กันทั่วไปคือการตะขอ
wp_head . ลองมาดูวิธีการทำงานนี้ คุณ
อาจจะสังเกตเห็นการทำงานที่ด้านล่างของส่วนหัวของเว็บไซต์
ชื่อ wp_head() . ดําน้ําในรหัสของฟังก์ชันนี้ คุณสามารถดูว่ามีการเรียก do_action()
. นี้จะคล้ายกับ apply_filters() ;
หมายถึงการใช้ทั้งหมดของฟังก์ชันที่เกี่ยวข้องกับ wp_head แท็ก .
เอาลิขสิทธิ์แท็ก meta ที่ด้านบนของโพสต์แต่ละหน้าเพื่อทดสอบวิธีนี้
ทํางาน
การแปล กรุณารอสักครู่..
