WordPress: Tsim Sidebars Caw Rau Txhua Pawg

Muaj nuj nqi rau npe Side Side rau txhua WordPress qeb

Kuv tau ua kom yooj yim rau lub xaib no txhawm rau txhim kho lub sijhawm nrawm dua thiab sim ua kom tiav nyiaj txiag rau lub xaib zoo dua yam tsis muaj kev ua phem rau kuv cov neeg nyeem. Muaj ntau txoj hauv kev uas kuv tau monetized lub xaib… ntawm no yog los ntawm feem ntau mus rau qhov muaj txiaj ntsig:

  • Kev Txhawb nqa ncaj qha los ntawm tuam txhab khub. Peb ua haujlwm rau cov tswv yim sib koom ua ke uas koom nrog txhua yam ntawm webinars rau cov kev tshaj tawm hauv zej zog los txhawb lawv cov xwm txheej, cov khoom lag luam, thiab / lossis cov kev pabcuam.
  • affiliate li los ntawm ib qho kev txig ntawm cov chaw sib koom tes. Kuv nplai thiab txheeb xyuas cov tuam txhab, xyuas kom lawv muaj suab npe nrov, thiab qhia cov khoom tshwj xeeb uas kuv sau lossis tshaj tawm uas lawv muab.
  • Cov peev txheej lag luam los ntawm ib tus khub uas tso tawm tshaj tawm kev lag luam rau cov koom txoos, kev tshawb nrhiav cov ntaub ntawv, thiab cov ntawv dawb.
  • Banner tshaj tawm los ntawm Google qhov twg cov ntawv tshaj tawm cuam tshuam tau cia li tawg ntawm kuv tus qauv thiab cov ntsiab lus.

WordPress Sidebars

Nrog rau kev koom nrog kev lag luam muab qee cov nyiaj tau zoo, Kuv txiav txim siab tias kuv xav tsom teeb rau cov tshaj tawm tshwj xeeb raws li qeb ntawm lub xaib, yog li kuv xav dynamically tsim cov sidebars tsis muaj qhov nyuaj-code txhua sidebar ntawm lub xaib. Txoj kev no, yog tias kuv ntxiv qeb - qhov xwm txheej tau tshwm sim hauv kuv thaj tsam Widget thiab kuv tuaj yeem ntxiv ib qho tshaj tawm.

Ua li no, Kuv xav tau qee qhov cai tshwj xeeb hauv functions.php file ntawm kuv tus menyuam lub ntsiab. Zoo siab, Kuv pom tias ib tug neeg twb sau ze txhua yam uas kuv xav tau: Tsim Widgetized Sidebars rau Txhua Pawg ntawm WordPressCov. Kuv tsuas xav tau qee qhov kev tswj xyuas ntxiv dhau uas pawg kuv tuaj yeem xav ua kom pom cov sidebars hauv.

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' );

Nrog rau kev sib cav ntawm kev sib cav kom tau txais cov pawg, Kuv tuaj yeem suav thiab cais tawm yam uas kuv xav tau hom phiaj. Nyob rau hauv nqe lus foreach, Kuv tuaj yeem hloov kho thiab phim cov kab lus rau kuv txhua qhov chaw WordPress sab saud.

Txuas ntxiv, hauv kuv functions.php, Kuv xav ntxiv qhov ua haujlwm kom pom yog tias muaj ib tog nyob tog thiab muaj cov kws kho mob ntxiv rau nws:

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;
}

Tom qab ntawd, hauv kuv lub ntsiab lus sidebar daim ntawv foos, Kuv ntxiv cov cim rau cov lus pom zoo hauv thaj chaw yog tias cov ntawv sau npe tau sau npe thiab muaj cov khoom siv hauv nws.

$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);
    }
}

WordPress Sidebars rau Txhua Pawg

Qhov tshwm sim yog raws nraim qhov kuv xav tau:

WordPress Widget Sidebars rau Txhua Pawg

Tam sim no, tsis hais seb kuv puas ntxiv, kho, lossis rho tawm pawg… kuv cov chaw ib puag ncig yuav nco ntsoov ua kom tiav!

Yuav ua li cas koj xav hais tias?

Qhov Web site no siv Akismet los txo cov kev pabcuam. Kawm li cas koj cov ntaub ntawv tawm tswv yim tiav.