2010-07-28 16 views
9

WordPress'te bir sınıflandırmadaki tüm mesajları almanın bir yolu var mı?WordPress'te özel sınıflandırmadan gelen tüm mesajları alın

taxonomy.php'da, geçerli terimle ilgili terimlerden gelen mesajları alan bu koda sahibim.

$current_query = $wp_query->query_vars; 
query_posts(array($current_query['taxonomy'] => $current_query['term'], 'showposts' => 10)); 

Bu terimden bağımsız olarak, taksonomide tüm yayınların bulunduğu bir sayfa oluşturmak istiyorum.

Bunu yapmak için basit bir yolu var mı, yoksa terimler için sınıflandırma sorgulamak gerekiyor, o zaman döngü yo, vb önüne aldığımızda

cevap

8
$myterms = get_terms('taxonomy-name', 'orderby=none&hide_empty');  
echo $myterms[0]->name; 

ilk öğe gönderme ediyorum, onları yalak daha sonra bir foreach; Döngü:

foreach ($myterms as $term) { ?> 
    <li><a href="<?php echo $term->slug; ?>"><?php echo $term->name; ?></a></li> <?php 
} ?> 

sen -my, foreach bir iç normal wordpress döngü oluşturmak Solüsyonu- hepsini göndermek istediğiniz ama böyle bir şey olması gerekiyor eğer, onları listeler itiraf edince yol:

foreach ($myterms as $term) : 

$args = array(
    'tax_query' => array(
     array(
      $term->slug 
     ) 
    ) 
); 

// assigning variables to the loop 
global $wp_query; 
$wp_query = new WP_Query($args); 

// starting loop 
while ($wp_query->have_posts()) : $wp_query->the_post(); 

the_title(); 
blabla.... 

endwhile; 

endforeach; 

Çok benzer bir şey yayınladım here.

+2

yukarıdaki örneği:

taksonomisi sülük kendisi taxonominin herhangi bir terim atanmış tüm mesajları listelemek yapmak için, EXISTS operator of tax_query in WP_Query kullanmak gerekir term_name '' bu tür 'taksonomi' => "$ term_adı" 'şeklinde çift tırnaklı olmalı veya bu tür '' taksonomi '=> $ term_adı' gibi daha iyi tırnaklar yoksa veya daha önce atamayı atlayıp daha iyi kullanmalısınız 'taxonomy' => $ term-> slug'. Dedi ki, gösterilen yöntem ['tax_query' => array (...) 'kullanarak [http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters] lehine [kullanımdan kaldırılmıştır]. Bu yardımcı olur umarım. – MikeSchinkel

+0

Üzgünüm, gecikme ... haklısın. Cevabımı buna göre değiştirdim :) –

+0

Mükemmel! Birlikte umarım çabalarımız başkalarına yardım eder. – MikeSchinkel

11

@PaBLoX çok güzel bir çözüm yaptı, ancak kendimi biraz zor olan ve her gönderi için her seferinde sorgulama gereği duymadığım bir çözüm buldum. ve tek bir gönderide birden fazla dönem atanmışsa ne olur? Aynı yayını birden çok kez oluşturmaz mı?

<?php 
    $taxonomy = 'my_taxonomy'; // this is the name of the taxonomy 
    $terms = get_terms($taxonomy, 'orderby=count&hide_empty=1'); // for more details refer to codex please. 
    $args = array(
     'post_type' => 'post', 
     'tax_query' => array(
        array(
         'taxonomy' => 'updates', 
         'field' => 'slug', 
         'terms' => m_explode($terms,'slug') 
        ) 
       ) 
     ); 

    $my_query = new WP_Query($args); 
    if($my_query->have_posts()) : 
     while ($my_query->have_posts()) : $my_query->the_post(); 

       // do what you want to do with the queried posts 

      endwhile; 
    endif; 
    ?> 

bu fonksiyon m_explode i functions.php dosyaya konulan özel bir fonksiyonudur.

function m_explode(array $array,$key = ''){  
     if(!is_array($array) or $key == '') 
      return;   
     $output = array(); 

     foreach($array as $v){   
      if(!is_object($v)){ 
       return; 
      } 
      $output[] = $v->$key; 

     } 

     return $output; 

     } 

GÜNCELLEME

Şu özel m_explode fonksiyonunu gerektirmez. wp_list_pluck() işlevi tam olarak aynı şeyi yapar. Bu yüzden m_explode'u wp_list_pluck() ile değiştirebiliriz (parametreler aynı olurdu). DRY, değil mi?

+0

bunun için çok teşekkürler!) Gerçekten çok yardımcı oldum. – Woppi

0

Terimler sözdizimi terimlerindeyken, tüm gönderi başvurularını bir dizide toplayabilir ve bunu daha sonra yeni bir WP_Query'de kullanabilirsiniz.

$post__in = array(); 
while ($terms_query->have_posts()) : $terms_query->the_post(); 
    // Collect posts by reference for each term 
    $post__in[] = get_the_ID(); 
endwhile; 

... 

$args = array(); 
$args['post__in'] = $post__in; 
$args['orderby'] = 'post__in'; 
$other_query = new WP_Query($args); 
0

Post türlerinden farklı olarak, WordPress'in taksonomi sümükünün kendisi için bir yolu yoktur. ` 'Sınıflandırmasında' => '$ ile on line

// Register a taxonomy 'location' with slug '/location'. 
register_taxonomy('location', ['post'], [ 
    'labels' => [ 
    'name' => _x('Locations', 'taxonomy', 'mydomain'), 
    'singular_name' => _x('Location', 'taxonomy', 'mydomain'), 
    'add_new_item' => _x('Add New Location', 'taxonomy', 'mydomain'), 
    ], 
    'public' => TRUE, 
    'query_var' => TRUE, 
    'rewrite' => [ 
    'slug' => 'location', 
    ], 
]); 

// Register the path '/location' as a known route. 
add_rewrite_rule('^location/?$', 'index.php?taxonomy=location', 'top'); 

// Use the EXISTS operator to find all posts that are 
// associated with any term of the taxonomy. 
add_action('pre_get_posts', 'pre_get_posts'); 
function pre_get_posts(\WP_Query $query) { 
    if (is_admin()) { 
    return; 
    } 
    if ($query->is_main_query() && $query->query === ['taxonomy' => 'location']) { 
    $query->set('tax_query', [ 
     [ 
     'taxonomy' => 'location', 
     'operator' => 'EXISTS', 
     ], 
    ]); 
    // Announce this custom route as a taxonomy listing page 
    // to the theme layer. 
    $query->is_front_page = FALSE; 
    $query->is_home = FALSE; 
    $query->is_tax = TRUE; 
    $query->is_archive = TRUE; 
    } 
} 
İlgili konular