WordPress: Createirƙiri Sidebars ta atomatik Ga Kowane Fanni

Aiki don Rijistar Yankin Bauta don Kowane Jigo na WordPress

Na kasance mai sauƙaƙa wannan rukunin yanar gizon don inganta lokutan gudu da yunƙurin samar da kuɗaɗen shiga ta yanar gizo ba tare da bata masu karatu rai ba. Akwai hanyoyi da yawa wadanda Na sanya kudi a shafin… a nan sun kasance daga mafi karancin riba:

  • Tallafawa kai tsaye daga kamfanonin haɗin gwiwa. Muna aiki akan dabarun gama gari waɗanda ke haɗa komai daga yanar gizo zuwa hannun jarin kafofin watsa labarun don haɓaka al'amuran su, samfuran su, da / ko ayyukansu.
  • affiliate marketing daga tsararrun dandamali na haɗin gwiwa. Ina yin bincike da gano kamfanonin, ina tabbatar da cewa suna da daraja, da raba takamaiman labarin da nake rubutawa ko tallace-tallace da suka samar.
  • Kasuwancin albarkatu daga abokin tarayya wanda ya sake al'amuran da suka shafi tallace-tallace, nazarin harka, da takaddun fararen kaya.
  • Banner talla daga Google inda tallace-tallace masu dacewa suka watse ta atomatik ta samfuri da abun ciki.

Yankin gefe na WordPress

Tare da tallace-tallace na haɗin gwiwa wanda ke ba da ɗan kuɗaɗen shiga, na yanke shawara cewa ina so in haskaka takamaiman masu tallatawa bisa laákari da rukunin rukunin rukunin yanar gizon, don haka ina son ƙirƙirar ɓoye-ɓoye ba tare da sanya lambar kowane gefen gefe a shafin ba. Wannan hanyar, idan na ƙara wani nau'ikan - labarun gefe yana bayyana kai tsaye a yankin Widget dina kuma zan iya ƙara talla.

Don yin wannan, Ina buƙatar takamaiman lambar a cikin functions.php fayil na yaro taken. Abin godiya, na gano cewa wani ya riga ya rubuta kusan duk abin da nake buƙata: Createirƙiri Sidebars da aka idaddara don kowane Fanni a cikin WordPress. Ina so kawai ƙarin ƙarin iko akan waɗanne rukunoni zan so in nuna sandunan gefe a ciki.

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

Tare da mahawara masu yawa don dawo da rukunoni, Zan iya haɗawa da keɓance kowane rukuni da nake so niyya. A cikin bayanin gabanin, zan iya gyara kuma dace da saiti zuwa tsarin shafin yanar gizo na na gaba daya.

Bugu da ƙari, a cikin na functions.php, Ina so in kara aiki don ganin idan akwai wani gefen gefe kuma yana da widget din da aka kara masa:

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

Bayan haka, a cikin taken na labarun gefe, fayil ɗin samfuri, Na ƙara lamba don nuna karfin halin nuna yankin idan labarun gefe yana rajista kuma yana da widget a ciki.

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

Yankin gefe na WordPress don Kowane Irin

Sakamakon shine daidai abin da nake so:

Widget din Widget na Widget na WordPress don Kowane Fanni

Yanzu, ba tare da la'akari da ko na ƙara, gyara, ko share nau'ikan ba areas bangarorin labarun gefe na zasu kasance masu sabuntawa koyaushe!

Me kuke tunani?

Wannan shafin yana amfani da Akismet don rage spam. Koyi yadda aka sarrafa bayanan bayaninka.