반응형
WordPress WP_Query - 상위 페이지만 쿼리
WP_Query를 사용하여 사용자 지정 게시물 유형의 게시물을 쿼리하고 있습니다.이 사용자 지정 게시물 유형에는 부모 및 자식 페이지가 있습니다.첫번째 부모 페이지를 뽑으려고 합니다.제가 이걸 어떻게 하죠?
$parent_only_query = new WP_Query(array(
'post_type' => 'my-custom-post-type',
'post_parent' => 0
));
while ($parent_only_query->have_posts()){
$parent_only_query->the_post();
// etc...
}
wp_reset_query(); // if you're not in your main loop! otherwise you can skip this
데이터베이스에 대한 쿼리를 수행하여 이 기능을 달성할 수 있습니다.
<?php
$parent_posts= $wpdb->get_results( "SELECT ID, post_title FROM $wpdb->posts WHERE post_parent=0 AND post_type='page' AND post_status='publish' ORDER BY menu_order ASC" );
foreach($parent_posts as $record){ ?>
<a href="<?php echo get_permalink($record->ID) ?>" >
<h1><?php echo $record->post_title; ?></h1>
</a>
<p><?php echo $record->post_title;?></p>
<?php } ?>
참고:-$wpdb
는 전역 변수입니다.
쿼리를 실행하고 이를 반복하면 다음과 같이 각 게시물의 부모 ID에 액세스할 수 있습니다.$post->post_parent
, 만약 그것이 null이 아니라면 당신은 그 포스트 컨텐츠를 다음과 같이 얻을 수 있습니다.get_post()
:
<?php
if($post->post_parent):
$parent = get_post($post->post_parent);
?>
<h2><?=$parent->post_title;?></h2>
<p><?=$parent->post_content;?></p>
<?php
endif;
?>
언급URL : https://stackoverflow.com/questions/5414669/wordpress-wp-query-query-parent-pages-only
반응형
'programing' 카테고리의 다른 글
MSVC에 대한 __attribute__(형식(printf, 1, 2))? (0) | 2023.10.25 |
---|---|
SQL Server 2008에서 테이블 설계를 변경할 수 없음 (0) | 2023.10.25 |
'log'에 대한 정의되지 않은 참조 (0) | 2023.10.25 |
테이블 셀의 텍스트 랩핑 방지 방법 (0) | 2023.10.25 |
Google Apps Script에서 MySQL SP가 호출되면 갑자기 중단됨 (0) | 2023.10.25 |