WordPress: Gadzira Sidebars otomatiki Kune Chero Chikamu

Basa rekunyoresa Sidebars kune yega yega Chikamu cheWordPress

Ndanga ndichirerutsa saiti ino kuvandudza nguva dzekumhanya uye kuyedza kuita mari saiti zvirinani ndisingatsamwise vaverengi vangu. Pane nzira dzakawanda dzandakaita mari saiti iyi ... heino anobva kune mazhinji kusvika kune mashoma anobhadhara:

  • Kutungamira kutsigira kubva kumakambani evamwe. Isu tinoshanda pamazano ehurongwa anosanganisira zvese kubva pawebhusaiti kusvika kumagariro enhau kusimudzira zviitiko zvavo, zvigadzirwa, uye / kana masevhisi.
  • Affiliate marketing kubva pamhando dzakasiyana dzemapuratifomu. Ini ndinotsvaga nekuona makambani, ndinoita shuwa kuti ane mukurumbira, uye ndinogovana chaiwo zvinyorwa zvandinonyora kana kushambadza zvavanopa.
  • Resource kushambadzira kubva kune mumwe wako anoburitsa zviitiko zvine chekuita nekutengesa, nyaya dzenyaya, uye mapepa machena.
  • Banner kushambadzira kubva kuGoogle uko kushambadza kwakakodzera kunozviparadzira kuburikidza netemplate yangu uye zvemukati.

WordPress Sidebars

Nekubatana kwekushambadzira kunopa mari yakanaka, ndakafunga kuti ini ndaida kutarisisa chaizvo vashambadzi zvinoenderana nechikamu chesaiti, saka ini ndaida kugadzira zvine simba mabara epadivi pasina kuomesa kodhi yega yega padivi pane ino saiti. Nenzira iyi, kana ndikawedzera chikamu - padivi paro zvinongoerekana zvaitika munzvimbo yangu yeWidget uye ndinogona kuwedzera kushambadza.

Kuti ndiite izvi, ini ndaida imwe chaiyo kodhi mune iyo functions.php faira remusoro wemwana wangu. Nekutenda, ini ndakaona kuti mumwe munhu anga atonyora dzinenge zvese zvandaida: Gadzira Widgetized Sidebars eDunhu rega rega muWordPress. Ini ndanga ndichingoda kumwe kwekuwedzera kudzora pamusoro pemapoka api andingashuve kuratidza iwo emativi ebara mu.

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

Iine rondedzero yekukakavara kwekutora zvikamu, ndinogona kusanganisira uye kusabvisa chero zvikamu zvandinoshuvira kunongedza. Pakati pechirevo chepamberi, ini ndinogona kugadzirisa nekufananidza marongero neangu akazara WordPress saiti padivi fomati.

Kuwedzera, mune yangu functions.php, Ini ndoda kuwedzera basa kuti ndione kana padivi pari uye paine widget yakawedzerwa pairi:

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

Zvino, mukati medingindira rangu sidebar template faira, ini ndinowedzera kodhi kuratidza zvine simba nzvimbo yacho kana bhaa repadivi rakanyoreswa uye riine widget mariri.

$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 dzeChikamu Chero

Mhedzisiro yacho ndizvo chaizvo zvandaida:

WordPress Widget Sidebars eDunhu rega rega

Zvino, zvisinei kuti ini ndinowedzera, kugadzirisa, kana kudzima zvikamu… nzvimbo dzangu dzepadivi dzichagara dzichienderana.

Unofungei?

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