programing

Wordpress의 사용자 지정 분류법에서 모든 게시물 가져오기

instargram 2023. 4. 2. 09:43
반응형

Wordpress의 사용자 지정 분류법에서 모든 게시물 가져오기

Wordpress에서 분류법에서 모든 게시물을 가져올 수 있는 방법이 있습니까?

taxonomy.php당기와 관련된 용어에서 투고를 취득하는 코드를 가지고 있습니다.

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

용어에 관계없이 분류법에 있는 모든 투고가 포함된 페이지를 만들고 싶습니다.

간단한 방법이 있습니까?아니면 분류법에 문의하여 용어를 반복해야 합니까?

@PaBLoX는 매우 훌륭한 솔루션을 만들었지만, 각 조건에 대해 매번 모든 투고를 조회할 필요가 없는 솔루션을 직접 만들었습니다.한 투고에 여러 개의 용어가 할당되어 있다면 어떻게 해야 할까요?같은 투고를 여러 번 렌더링하지 않을까요?

 <?php
     $taxonomy = 'my_taxonomy'; // this is the name of the taxonomy
     $terms = get_terms($taxonomy);
     $args = array(
        'post_type' => 'post',
        'tax_query' => array(
                    array(
                        'taxonomy' => 'updates',
                        'field' => 'slug',
                        'terms' => wp_list_pluck($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;
  ?>

wp_list_module

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

첫 번째 아이템을 투고하면 다음 아이템을 만들 수 있습니다.foreach; 루프:

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

이 방법으로 모든 것을 투고하려면 , 「나의 솔루션」이라고 하는 일반적인 워드프레스 루프를 포어치 안에 작성합니다만, 다음과 같은 것이 필요합니다.

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;

도 비슷한 글을 올렸어요.

포스트 타입과는 달리 WordPress에는 분류 슬러그 자체의 경로가 없습니다.

분류법 자체에 분류법의 용어가 할당된 모든 게시물이 나열되도록 하려면 의 연산자를 사용해야 합니다.

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

용어 쿼리 루프에서 배열 내의 모든 게시 참조를 수집하여 나중에 새로운 WP_Query에서 사용할 수 있습니다.

$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 );
<?php
get_posts(array(
    'post_type' => 'gallery',
    'tax_query' => array(
        array(
        'taxonomy' => 'gallery_cat',
        'field' => 'term_id',
        'terms' => 45)
    ))
);
?>

언급URL : https://stackoverflow.com/questions/3354272/get-all-posts-from-custom-taxonomy-in-wordpress

반응형