PHP: Gwiritsani ntchito WordPress API kuti mupange Shortcode kuti mulembe Subpages

WordPress PHP

Tikugwira ntchito yovuta kwambiri kwamakasitomala kasitomala pakadali pano. Tsambali likumangidwa mu WordPress koma lili ndi mabelu ndi mluzu. Nthawi zambiri, ndikamagwira ntchito yamtunduwu, ndimakonda kusunga nambala yakusinthira pambuyo pake patsamba lina. Poterepa, ndimaganiza kuti inali ntchito yothandiza, ndimafuna kugawana ndi dziko lapansi. Tikugwiritsa ntchito Mutu wa Avada WordPress wokhala ndi Fusion Page Builder monga mutu wa kholo, ndikugwiritsa ntchito nambala yazikhalidwe zamwana wathu.

WordPress ili ndi ntchito zingapo mu API yake zomwe zitha kugwiritsidwa ntchito kulemba magawo, monga wp_list_pages ndi get_pages. Vuto ndiloti samabweza zambiri ngati mukuyembekeza kupanga mndandanda ndi zambiri.

Kwa kasitomala uyu, amafuna kutumiza mafotokozedwe antchito ndikuti akhale ndi mndandanda wazotsegulidwa zodzipangira zokha patsiku lawo lofalitsa. Ankafunanso kuwonetsa gawo lina la tsambalo.

Chifukwa chake, choyamba, timayenera kuwonjezera zowonjezera pazomwe zili patsamba. Mu works.php pamutu wawo, tidawonjezera kuti:

add_post_type_support ('tsamba', 'excerpt');

Kenako, timafunikira kulembetsa njira yachidule yomwe ingapangitse mndandanda wamagawo ang'onoang'ono, maulalo, ndi gawo lawo. Chitani izi, tiyenera kugwiritsa ntchito Chingwe cha WordPress. Mu works.php, tidawonjezera:

// Mndandanda wa Mndandanda mu Mndandanda ntchito dknm_list_child_pages ($ atts, $ content = "") {global $ post; $ atts = shortcode_atts (gulu ('ifempty' => 'Palibe Zolemba', 'aclass' => ''), $ atts, 'list_subpages'); $ args = array ('post_type' => 'tsamba', 'posts_per_page' => -1, 'post_parent' => $ post-> ID, 'orderby' => 'publish_date', 'order' => 'DESC' ,); $ kholo = WP_Query yatsopano ($ args); ngati ($ kholo-> have_post ()) {$ string. = $ okhutira. ' '; pamene ($ parent-> have_posts ()): $ parent-> the_post (); $ chingwe. = ' '.pangani_m_mutu ().' '; ngati (has_excerpt ($ post-> ID)) {$ string. = '-'. get_the_excerpt (); } $ chingwe. = ' '; nthawi zonse; } china {$ string = ' '. $ atts [' ifempty '].' '; } wp_reset_postdata (); bweretsani chingwe cha $; } onjezani_shortcode ('mndandanda_subpages', 'dknm_list_child_pages');

Tsopano, shortcode itha kugwiritsidwa ntchito patsamba lonselo kuti iwonetse masamba amwana ndi ulalo ndi chidule. Wakagwiritsidwe:

[list_subpages aclass = "batani" ifempty = "Pepani, pano tiribe mwayi wopeza ntchito."] Mndandanda wa Ntchito [/ mndandanda_subpages]

Zotsatira zake ndi zabwino, zoyera zosasanjidwa mndandanda wazantchito, zomwe ndi masamba aana pansi pa tsamba lawo la ntchito.

Ngati pakadalibe ntchito yofalitsidwa (palibe masamba a ana), ikasindikiza:

Pepani, pano sitikhala ndi mwayi wantchito.

Ngati panali ntchito zosindikizidwa (masamba a ana), ifalitsa:

Mndandanda wa Ntchito:

Mukuganiza chiyani?

Tsambali likugwiritsa ntchito Akismet kuchepetsa spam. Phunzirani momwe deta yanu ikufotokozera.