WordPress: Pangani Ma Sidebars Basi pagulu Lonse

Ntchito Yolembetsa Ma Sidebars Pagawo Lililonse la WordPress

Ndakhala ndikuchepetsa tsambali kuti ndichepetse nthawi zothamanga ndikuyesera kupanga ndalama patsambali popanda kukwiyitsa owerenga anga. Pali njira zingapo zomwe ndapangidwira kuti ndigwiritse ntchito tsambalo… nazi zikuchokera kopita kopindulitsa:

  • Zothandizira mwachindunji ochokera kumakampani othandizana nawo. Timagwira ntchito pamalingaliro amtundu uliwonse omwe amaphatikiza chilichonse kuyambira pa intaneti mpaka pazogawana kuti zithandizire zochitika zawo, malonda awo, ndi / kapena ntchito zawo.
  • Kugulitsa othandizira kuchokera pamitundu ingapo yothandizana nayo. Ndimasanthula ndikuzindikira makampaniwo, kuwonetsetsa kuti ndiwotchuka, ndikugawana zomwe ndalemba kapena zotsatsa zomwe amapereka.
  • Kutsatsa pazida kuchokera kwa mnzanu yemwe amatulutsa zochitika zokhudzana ndi kutsatsa, zochitika zamakalata, ndi mapepala oyera.
  • Kutsatsa kwa mbendera kuchokera ku Google pomwe zotsatsa zoyenera zimangobalalitsidwa mosavuta kudzera mu template yanga ndi zomwe zili.

Mabala oyambira a WordPress

Ndi kutsatsa kothandizana nawo komwe kumapereka ndalama zabwino, ndidaganiza kuti ndikufuna kuwonetsa otsatsa enieni malinga ndi gulu la tsambalo, chifukwa chake ndimafuna kupanga mipiringidzo mwamphamvu popanda kulembetsa zolimba mbali zilizonse patsamba. Mwanjira iyi, ndikangowonjezera gulu - sidebar imangowonekera m'dera langa la Widget ndipo nditha kuwonjezera zotsatsa.

Kuti ndichite izi, ndimafunikira nambala inayake mu functions.php fayilo yamutu wamwana wanga. Mwamwayi, ndapeza kuti winawake anali atalemba kale pafupifupi chilichonse chomwe ndimafuna: Pangani Zingwe Zazikulu Zomwe Zili M'gulu Lonse mu WordPress. Ndimangofuna zina zowongolera pazinthu zomwe ndingafune kuwonetsa zipilala zam'mbali.

function add_category_sidebars() {
    $args = array(
        'type'                     => 'post',
        'orderby'                  => 'name',
        'order'                    => 'ASC',
        'hide_empty'               => 1,
        'hierarchical'             => 1,
        'exclude'                  => '',
        'include'                  => '',
        'number'                   => '',
        'taxonomy'                 => 'category'
        ); 
    
    $categories = get_categories($args);

    foreach ($categories as $category) {
        if (0 == $category->parent)
            register_sidebar( array(
                'name' => $category->cat_name,
                'id' => $category->category_nicename . '-sidebar',
                'description' => 'This is the ' . $category->cat_name . ' widgetized area',
                'before_widget' => '<aside id="%1$s" class="widget %2$s">',
                'after_widget' => '</aside>',
                'before_title' => '<h3 class="widget-title">',
                'after_title' => '</h3>',
            ));
        }
}
add_action( 'widgets_init', 'add_category_sidebars' );

Ndi zifukwa zingapo zobweretsera magulu, nditha kuphatikiza ndikupatula magulu aliwonse omwe ndikufuna kuwunikira. M'mawu amtsogolo, nditha kusintha ndikusintha mawonekedwe anga akunyumba kwa WordPress.

Kuphatikiza apo, mu functions.php, Ndikufuna kuwonjezera ntchito kuti ndiwone ngati mbali yambali ilipo ndipo yawonjezeredwa ndi chida:

function is_sidebar_active($cat_name) {
    global $wp_registered_sidebars;
    $cat_id = get_cat_ID($cat_name);
    $widgetlist = wp_get_sidebars_widgets();
    if ($widgetlist[$cat_id])
        return true;
    return false;
}

Kenako, mkati mwa mutu wanga sidebar template, ndikuwonjezera nambala kuti ndisonyeze mwamphamvu malowa ngati sidebar idalembetsedwa ndikukhala ndi widget.

$queried_object = get_queried_object();
if ($queried_object) {
    $post_id = $queried_object->ID;
}
if(is_category() || in_category($cat_name, $post_id)) {
    $sidebar_id = sanitize_title($cat_name);
    if( is_sidebar_active($sidebar_id)) {
        dynamic_sidebar($sidebar_id);
    }
}

Ma Sidebars a WordPress a Gulu Lonse

Zotsatira zake ndizomwe ndimafuna:

Zingwe Zapakatikati za Widget ya WordPress Mgawo Lonse

Tsopano, mosasamala kanthu kuti ndikuwonjezera, kusintha, kapena kufufuta magawo ... madera anga akumbali nthawi zonse azikhala achikale!

Mukuganiza chiyani?

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