PHP: E hoʻohana i ka WordPress API e kūkulu i kahi Shortcode i nā papa inoa papa inoa

WordPress PHP

Ke hana nei mākou i kahi hoʻokō paʻakikī no kahi mea lawelawe ʻoihana i kēia manawa. Kūkulu ʻia ka pūnaewele ma WordPress akā he tona o nā bele a me nā pupuhi. ʻO ka manawa pinepine, ke hana nei wau i kēia ʻano hana, makemake wau e mālama i ka pāʻālua no ka repurposing ma hope ma nā pūnaewele ʻē aʻe. I kēia hihia, ua manaʻo wau he hana pono ia, makemake wau e kaʻana like me ka honua. Ke hoʻohana nei mākou i ka Kumuhana Avada WordPress me ka Fusion Page Builder ma ke ʻano he mākua kumuhana, a deploying loa i kahi ʻāpana o ke code kuhi i kā mākou keiki kumuhana.

He ʻelua mau hana a WordPress i loko o kāna API i hiki ke hoʻohana ʻia e papa inoa ai i nā ʻāpana, e like me wp_list_pages a me get_pages. ʻO ka pilikia ʻaʻole lākou e hoʻihoʻi i ka ʻike inā makemake ʻoe e hana ikaika i kahi papa inoa me ka nui o ka ʻike.

No kēia mea kūʻai aku, makemake lākou e kau i nā wehewehe hana a loaʻa i ka papa inoa o ka wehe ʻana o ka hana i ka wā iho e ka lā hoʻolaha. Ua makemake lākou e hōʻike i kahi ʻāpana o ka ʻaoʻao.

No laila, ʻo ka mea mua, pono mākou e hoʻohui i kahi kākoʻo excerpt i ka anakuhi ʻaoʻao. I loko o functions.php no kā lākou kumuhana, ua hoʻohui mākou:

add_post_type_support ('ʻaoʻao', 'excerpt');

A laila, pono mākou e hoʻopaʻa inoa i kahi shortcode maʻamau e hoʻoulu i ka papa inoa o nā subpage, nā loulou iā lākou, a me ka excerpt no lākou. Hana i kēia, pono mākou e hoʻohana i ka Polokika WordPress. Ma functions.php, ua pākuʻi mākou:

// List Subpages in a List function dknm_list_child_pages ($ atts, $ content = "") {global $ post; $ atts = shortcode_atts (array ('ifempty' => 'ʻAʻohe Records', 'aclass' => ''), $ atts, 'list_subpages'); $ args = array ('post_type' => 'ʻaoʻao', 'posts_per_page' => -1, 'post_parent' => $ post-> ID, 'orderby' => 'publish_date', 'order' => 'DESC' ,); $ makua = WP_Query hou ($ args); inā ($ makua-> loaʻa_post ()) {$ string. = $ ʻike maʻiʻo. ' '; ʻoiai ($ makua-> loaʻa_post ()): $ makua-> ka_post (); $ string. = ' '.get_the_title ().' '; inā (has_excerpt ($ post-> ID)) {$ string. = '-' .get_the_excerpt (); } $ string. = ' '; hope loa; } ʻē aʻe {$ string = ' '. $ atts [' ifempty '].' '; } wp_reset_postdata (); hoʻihoʻi $ string; } add_shortcode ('list_subpages', 'dknm_list_child_pages');

I kēia manawa, hiki ke hoʻokō ʻia ka shortcode ma loko o ka pūnaewele e hōʻike i nā ʻaoʻao keiki me kahi loulou a me kahi ʻāpana. Hoʻohana:

[list_subpages aclass = "pihi" ifempty = "E kala mai, ʻaʻohe wahi wehe o kā mākou hana i kēia manawa."] Papa Inoa o na Hana [/ list_subpages]

ʻO ka hopena he papa inoa maikaʻi ʻole maʻemaʻe ʻole o nā hana i paʻi ʻia, ʻo ia nā ʻaoʻao keiki ma lalo o kā lākou ʻaoʻao ʻoihana.

Inā ʻaʻohe hana i paʻi ʻia (ʻaʻohe ʻaoʻao keiki), e paʻi ia:

E kala mai, ʻaʻohe o kā mākou hana i kēia manawa.

Inā he mau hana i paʻi ʻia (nā ʻaoʻao keiki), e paʻi ia:

Papa Inoa o na Hana:

Pehea kou manaʻo?

Ke hoʻohana nei kēia pūnaewele i ka Akismet e ho'ēmi i ka spam. E aʻo pehea e hanaʻia ai kāuʻikeʻikepili.