programing

워드프레스 페이지 내용은 어떻게 표시합니까?

kingscode 2023. 4. 1. 14:27
반응형

워드프레스 페이지 내용은 어떻게 표시합니까?

이게 정말 간단한 건 알지만 어떤 이유에서인지 떠오르지 않고 구글도 오늘 날 도와주지 않아요.

페이지 내용을 출력하고 싶은데 어떻게 해야 하나요?

난 이런 줄 알았어

<?php echo the_content(); ?>

@Marc B 코멘트 감사합니다.이 점을 발견하는 데 도움이 되었습니다.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>

이것은 보다 간결합니다.

<?php echo get_post_field('post_content', $post->ID); ?>

그리고 이것은 더욱 그렇다.

<?= get_post_field('post_content', $post->ID) ?>

@Sydney 루프를 호출하기 전에 wp_reset_query()를 입력해 보십시오.페이지 내용이 표시됩니다.

<?php
    wp_reset_query(); // necessary to reset query
    while ( have_posts() ) : the_post();
        the_content();
    endwhile; // End of the loop.
?>

편집: 이전에 실행한 다른 루프가 있는 경우 이 방법을 사용해 보십시오.이 루프를 호출하기 전에 wp_reset_query()를 배치합니다.

곳곳에 php 태그가 붙어있는 끔찍한 코드를 좋아하지 않는 사람들을 위해...

<?php
if (have_posts()):
  while (have_posts()) : the_post();
    the_content();
  endwhile;
else:
  echo '<p>Sorry, no posts matched your criteria.</p>';
endif;
?>

이 코드를 content div에 넣기만 하면 됩니다.

<?php
// TO SHOW THE PAGE CONTENTS
    while ( have_posts() ) : the_post(); ?> <!--Because the_content() works only inside a WP Loop -->
        <div class="entry-content-page">
            <?php the_content(); ?> <!-- Page Content -->
        </div><!-- .entry-content-page -->

    <?php
endwhile; //resetting the page loop
wp_reset_query(); //resetting the page query
?>

다음같이 페이지 내용을 쉽고 완벽하게 표시할 수 있습니다.

<?php if(have_posts()) : ?>
    <?php while(have_posts())  : the_post(); ?>
      <h2><?php the_title(); ?></h2>                        
      <?php the_content(); ?>          
      <?php comments_template( '', true ); ?> 
    <?php endwhile; ?>                   
      <?php else : ?>                       
        <h3><?php _e('404 Error&#58; Not Found'); ?></h3>
<?php endif; ?>         

주의:

내용 표시에 관해서는 i) comments_template() 함수는 다른 기능으로 코멘트를 유효하게 할 필요가 있는 경우 옵션으로 사용할 수 있습니다.

ii) _e() 함수도 옵션이지만 단순히 텍스트를 표시하는 것보다 더 의미 있고 효과적입니다.<p>. 스타일화된 404.120을 생성하여 리다이렉트할 수 있습니다.

이 간단한 php 코드 블록을 추가하면 이 작업을 수행할 수 있습니다.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
      the_content();
      endwhile; else: ?>
      <p>!Sorry no posts here</p>
<?php endif; ?>

"더 루프"는 나쁜 관행이다.Word Press는 이를 구현해서는 안 되며, 이 시점에서 폐지되어야 합니다.글로벌 상태에 의존하여 수정하는 랜덤 함수를 사용하는 것은 좋지 않습니다.다음은 제가 찾은 가장 좋은 대안입니다.

<?php
    $post = get_queried_object();
?>
<div>
    <?php echo do_shortcode(apply_filters('the_content', $post->post_content)); ?>
</div>

언급URL : https://stackoverflow.com/questions/5465765/how-do-i-display-a-wordpress-page-content

반응형