programing

Wordpress - 용어별 사용자 지정 게시 유형 목록의 사용자 지정 분류 페이지

newnotes 2023. 3. 11. 09:25
반응형

Wordpress - 용어별 사용자 지정 게시 유형 목록의 사용자 지정 분류 페이지

나는 분류학이 있다php 페이지는 다음과 같이 표시되어야 합니다.

커스텀 투고 타입 타이틀(리소스)

커스텀 택사노미 1(리소스 유형)

자원 유형 Term 1 (화이트 페이퍼)

  • 화이트 페이퍼 투고 1

    화이트 페이퍼 투고 2

    화이트 페이퍼 투고 3

자원 유형 Term 2 (비디오)

  • 비디오 투고 1

    비디오 투고 2

    비디오 투고 3

Wordpress 3.0의 새로운 문서를 모두 이해하려고 했지만, 2.8과 혼재된 것 같아서 더 혼란스러울 뿐입니다.

오브젝트를 배열로 변환할 필요가 없습니다.오브젝트를 너무 번거롭게 하지 않고 완벽하게 작업할 수 있습니다.(적어도 나에게) 궁금한 것은 다음과 같은 것을 얻을 수 있다는 것입니다.

  Array
  (
      [0] => stdClass Object
          (
              [term_id] => 7
              [name] => Magister comunicaciones aplicadas
              [slug] => magister-comunicaciones-aplicadas
              [term_group] => 0
              [term_taxonomy_id] => 7
              [taxonomy] => linea-de-estudio
              [description] => 
              [parent] => 0
              [count] => 4
          )

      [1] => stdClass Object
          (
               [term_id] => 8
               [name] => Engagement marketing
               [slug] => engagement-marketing
               [term_group] => 0
               [term_taxonomy_id] => 8
               [taxonomy] => linea-de-estudio
               [description] => 
               [parent] => 0
               [count] => 5
          )
  )

기본적으로는 사물의 배열이기 때문에 그렇게 취급해야 합니다.예를 들어 첫 번째 이름을 원하는 경우:

$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
} ?>

그러면 분류법에서 나온 기사를 게시할 수 있습니다.

커스텀 투고 타입의 경우는, 다음과 같은 루프를 작성할 필요가 있습니다.

$args = array(
    'post_type' => 'post-type-name',
    'taxonomy' => 'term'
    //for example
    //'resources' => 'videos'
);

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

그런 다음 각 분류법/항 :)에 대해 각각 하나씩 여러 개의 루프를 작성할 수 있습니다.

더 화려해지고 싶은 경우(반복하고 싶지 않은 경우), 첫 번째 루프 안에 두 번째 루프를 포함시키고 분류법(리소스)과 마지막 루프에 있는 용어(비디오)에 변수를 할당할 수 있습니다(예에서 마지막 루프만).일반적인 워드프레스 루프는 커스텀 포스트 타입과 각 용어로 제한됩니다.

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

        $term_name = $term->slug;

        $args = array(
        'post_type' => 'post-type-name',
        'taxonomy' => "$term_name"
        );

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

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

   the_title();
   blabla....

   endwhile;

endforeach; ?>

물론 역순으로도 할 수 있습니다.단일 템플릿의 커스텀유형에 대한 정규 루프를 작성할 수 있습니다(하나밖에 없는 것처럼 보입니다).내부에는 모든 커스텀항이 포함됩니다.

별로 우아하진 않지만 그게 내가 생각해낼 수 있는 최선의 방법이야.누군가 이것을 이해할 수 있기를 바랍니다. 혼란스럽게 들립니다.

혹시 콜백 기능으로 가능할까?

안녕, 마논 1165, 나 사실 방금 이 일을 해냈다구.큰 고통이었어요. 제 코드 조각이 도움이 되었으면 좋겠네요!

커스텀 페이지 템플릿을 만들었습니다.그리고 어떤 행동을 했었죠

<?php $categories = get_terms('taxonomy-name', 'orderby=name&hide_empty=0'); $cats = object_to_array($categories); ?>

자, 그냥print_r($cats)카테고리 배열이 표시됩니다.

개체를 배열로 변환해야 합니다.

function object_to_array($data) 
{
  if(is_array($data) || is_object($data))
  {
    $result = array(); 
    foreach($data as $key => $value)
    { 
      $result[$key] = object_to_array($value); 
    }
    return $result;
  }
  return $data;
}

했다

<ul id="cat-list">
<?php foreach($cats as $cat) { ?>
  <li><a href="/taxonomy-name/<?php echo $cat['slug']; ?>"><?php echo $cat['name']; ?> (<?php echo $cat['count']; ?>)</a><br><?php echo $cat['description']; ?></li>
<?php } ?>
</ul>

도움이 됐으면 좋겠네요!

이것은 나에게 있어서 효과가 있었다:-

<?php
    $custom_terms = get_terms('custom_taxonomy');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'custom_post_type',
        'tax_query' => array(
            array(
                'taxonomy' => 'custom_taxonomy',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo '<h2>'.$custom_term->name.'</h2>';

        while($loop->have_posts()) : $loop->the_post();
            echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
        endwhile;
     }
}
>?

언급URL : https://stackoverflow.com/questions/3358049/wordpress-custom-taxonomy-page-of-custom-post-type-listing-by-terms

반응형