다음 페이지가 있는지 확인하는 방법


16

저는 워드 프레스 개발에 익숙하지 않고 HTML을 워드 프레스 테마로 변환하려고했지만 Chris Coyer의 빈 테마로 시작했습니다.

<div class="navigation">
    <div class="next-posts">
        <?php next_posts_link('&laquo; Older Entries') ?>
    </div>
    <div class="prev-posts">
        <?php previous_posts_link('Newer Entries &raquo;') ?>
    </div>
</div>

가있는 경우에만 div를 출력하는 방법은 무엇입니까 next_posts_link()? <ul>페이지 매김에 사용할 때 이것이 필요합니다 . 그렇게하지 않으면 빈 총알이 나옵니다

답변:


18

사용 get_previous_posts_link하고 다음과 get_next_posts_link 같이 존재하는지 확인할 수 있습니다 .

$prev_link = get_previous_posts_link(__('&laquo; Older Entries'));
$next_link = get_next_posts_link(__('Newer Entries &raquo;'));
// as suggested in comments
if ($prev_link || $next_link) {
  echo '<ul class="navigation">';
  if ($prev_link){
    echo '<li>'.$prev_link .'</li>';
  }
  if ($next_link){
    echo '<li>'.$next_link .'</li>';
  }
  echo '</ul>';
}

도움이 되었기를 바랍니다


문제는 빈 목록을 렌더링 할 가능성이 있다는 것입니다.이 경우 유효하지 않은 마크 업이 생성되며 실제로 사용자가 기존 코드와 동일한 문제를
— 겪게됩니다

3
@ t31os, 난 내가 할 것 같아요if ($prev_link || $next_link) // output ul
— Jiew Meng

그래, 그렇게 할거야 ..;)
— t31os

코드 업데이트
— Bainternet

코드 덕분에 echo '</ul>';마지막 줄 btw 직전에 누락되었습니다 .
— Davey

13

나는 이것을 얼마 전에 작성했지만 여전히 유효해야합니다.

http://www.ericmmartin.com/conditional-pagepost-navigation-links-in-wordpress-redux/

functions.php파일에 다음 기능을 추가 할 수 있습니다 .

/**
 * If more than one page exists, return TRUE.
 */
function show_posts_nav() {
    global $wp_query;
    return ($wp_query->max_num_pages > 1);
}

코드를 다음으로 업데이트하십시오.

<?php if (show_posts_nav()) : ?>
<div class="navigation">
    <div class="next-posts"><?php next_posts_link('&laquo; Older Entries') ?></div>
    <div class="prev-posts"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
</div>
<?php endif; ?>

3

가장 좋은 해결책은 확인 $wp_query->max_num_pages이지만 다음을 사용할 수도 있습니다.

<?php
if(paginate_links()) {
...
}

1
이 노트에 중요 $wp_query어디서나 사용할 수 표시되지 않습니다; 예를 들어 WooCommerce 템플릿 재정의 내에서 사용하려고 시도했지만 객체가 존재하지 않았으므로 functions.phpEric Martin의 답변을 사용하여 대신 내부에서 호출해야했습니다 .
— Brett

물론, 커스텀 루프 나 우 커머스 같은 경우도 처리해야합니다. 귀하의 경우 woocommerce의 wp 쿼리 또는 생성하는 쿼리를 얻는 방법을 확인해야합니다.
— Maxwell sc
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.