PHP: Shandisa WordPress API Kuvaka Shortcode kuna List Subpages

WordPress PHP

Tiri kushanda pane yakaoma kuita kweyemabhizimusi mutengi izvozvi. Iyo saiti iri kuvakwa muWordPress asi ine toni yemabhero uye muridzo. Kazhinji, pandinenge ndichiita irwu rudzi rwebasa, ini ndinofarira kusevha yetsika kodhi yekudzokorora gare gare kune mamwe masayiti. Mune ino kesi, ini ndaifunga kuti iri basa rinobatsira zvakadaro, ndaida kugovana nenyika. Tiri kushandisa iyo Avada WordPress theme neiyo Fusion Peji Muvaki semusoro wevabereki, uye kuendesa zvakati wandei zvetsika kodhi mune yedu theme yemwana.

WordPress yatove nemabasa akati wandei muAPI yayo inogona kushandiswa kunyora zvinyorwa zvidiki, senge wp_list_pages uye tora_mapeji. Dambudziko nderekuti havadzosere ruzivo rwakakwana kana iwe uri kutarisira kugadzira zvine simba runyorwa neboka reruzivo.

Kune uyu mutengi, ivo vaida kutumira tsananguro dzebasa uye kuve nerunyorwa rwemabasa ekuvhurwa otomatiki anogadzirwa mukuburuka kweodha neyavo mazuva ekuburitsa. Ivo zvakare vaida kuratidza chidimbu cheiyo peji.

Saka, kutanga, isu taifanira kuwedzera kutsigira kutsigira kune iyo peji template. Mumabasa.php yenyaya yavo, takawedzera:

add_post_type_support ('peji', 'excerpt');

Ipapo, isu taida kunyoresa tsika shortcode iyo yaizogadzira runyorwa rwezvikamu zvidiki, zvinongedzo kwavari, uye chirevo chavo. Ita izvi, isu tinofanirwa kushandisa iyo WordPress Chidimbu. Mumabasa.php, takawedzera:

// Rondedzero Subpages mune Rondedzero basa dknm_list_child_pages ($ atts, $ zvemukati = "") {global $ post; $ atts = shortcode_atts (arraw ('ifempty' => 'Hapana Zvinyorwa', 'aclass' => ''), $ atts, 'list_subpages'); $ args = array ('post_type' => 'peji', 'posts_per_page' => -1, 'post_parent' => $ post-> ID, 'orderby' => 'publish_date', 'order' => 'DESC' ,); $ mubereki = nyowani WP_Query ($ args); kana ($ mubereki-> uine_zvinyorwa ()) {$ tambo. = $ zvemukati. ' '; apo ($ parent-> have_post ()): $ parent-> the_post (); $ tambo. = ' '.tora_the_title ().' '; kana (has_excerpt ($ post-> ID)) {$ string. = '-' .get_the_excerpt (); } $ tambo. = ' '; nguva; } zvimwe {$ string = ' '. $ atts [' ifempty '].' '; } wp_reset_postdata (); dzosera $ tambo; } wedzera_shortcode ('list_subpages', 'dknm_list_child_pages');

Ikozvino, iyo shortcode inogona kuitwa mukati mesaiti kuratidza mapeji evana ane chinongedzo uye excerpt. Usage:

[list_subpages aclass = "button" ifempty = "Tine hurombo, parizvino hatina mabasa ekuvhurwa."] Chinyorwa cheMabasa [/ list_subpages]

Mhedzisiro yacho yakanaka, yakachena isina unordered runyorwa rwemabasa akaburitswa, ari mapeji evana pasi pebasa rebasa ravo.

Dai pakanga pasina mabasa akaburitswa (hapana mapeji emwana), ichashambadza:

Tine hurombo, parizvino hatina chero mabasa ekuvhura.

Dai paive nemabasa akaburitswa (mapeji evana), ichaburitsa:

Chinyorwa cheMabasa:

Unofungei?

Nzvimbo iyi inoshandisa Akismet kuderedza spam. Dzidza kuti sei deta yako inoshandiswa.