source

현재 카테고리의 워드프레스 게시물만 나열합니다.

nicesource 2023. 10. 26. 21:15
반응형

현재 카테고리의 워드프레스 게시물만 나열합니다.

워드프레스 사이트에 두 번째 탐색 메뉴를 만들려고 합니다.

나는 이것이 현재 카테고리 내의 모든 게시물에 대한 링크만을 보여주기를 원합니다.

get_ posts 함수를 실험해 보았지만 현재 범주를 동적으로 선택하는 방법을 찾는데 어려움을 겪고 있습니다. 즉, 여기 범주에 무엇을 넣을 것인가=x

도움을 주시면 대단히 감사하겠습니다.

여기 제가 사용해왔던 템플릿 코드가 있습니다.

<ul id="catnav">

     <?php
     global $post;
     $myposts = get_posts('numberposts=5&category=1');
     foreach($myposts as $post) :
     ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
     <?php endforeach; ?>



    </ul>

여기서 나온 이 코드로 드디어 해결: http://www.snilesh.com/resources/wordpress/wordpress-recent-posts-from-current-same-category/

현재 페이지 및 목록 오름차순을 포함하도록 수정했습니다.

<ul id="catnav">
<?php
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$myposts = get_posts(array('numberposts' => 5, 'offset' => 0, 'category__in' => array($category), 'post_status'=>'publish', 'order'=>'ASC' ));
foreach($myposts as $post) :
setup_postdata($post);
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a>
</li>
<?php endforeach; ?>
<?php wp_reset_query(); ?>
<li><a href="?p=46">Why Us?</a></li>

</ul>
<!--Insted Of this-->
$myposts = get_posts('numberposts=5&category=1');
<!--Use This-->
$cat_ID = get_query_var('cat');
query_posts('cat='.$cat_ID.'&showposts=5&order=ASC');
$args=array(
'cat' => get_query_var('cat'),
  'orderby' => 'title',
  'order' => 'ASC',
  'posts_per_page'=>-1,
  'caller_get_posts'=>1
);
$my_query = new WP_Query($args);

저한테는 통했어요!

그래서 현재 카테고리의 모든 게시물을 보여주는 데 아주 적합한 코드를 찾았습니다.

 <ul id="catnav">

 <?php
foreach( ( get_the_category() ) as $category ) {
$the_query = new WP_Query('category_name=' . $category->category_nicename . '&showposts=5&order=ASC');
while ($the_query->have_posts()) : $the_query->the_post();
?>
            <li>
                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
            </li>
<?php endwhile; ?>
<?php
}
?>


</ul>

하지만 저는 제외하고 싶은 몇 가지 카테고리가 있습니다.제가 제외하고 싶은 두 개의 카테고리에 게시물이 존재합니다. 8, 9, 11 카테고리에 게시물이 표시됩니다.

무슨 생각 있어요?

카테고리명 대신 카테고리 id에서 게시물을 받는 것이 좋다고 생각합니다. if other 조건을 작성할 수 있고 id가 8,9,11인 게시물을 제외할 수 있습니다.

언급URL : https://stackoverflow.com/questions/13010805/list-wordpress-posts-from-current-category-only

반응형