• Hello,

    I’d like to display the Content of a WordPress Page. To show up the title I use single_post_title(); – but I cant find the call for the content which where defined for a related page.

    <?php /* Template Name: Template "content"  */?> 
    
    <?php get_header(); ?>
    
    <?php
           
        $args = array( 
            'post_type' => "adventskalender", 
            'posts_per_page' => 1
        );
    
        $loop = new WP_Query( $args );
        
        while ( $loop->have_posts() ) : $loop->the_post()
    
    ?>
    
        <div class="background-box">
            <?php echo get_the_post_thumbnail( $post_id, 'full', array() ); ?>
        </div>
    
        <h1>
            <?php the_title(); ?>
            <span><?php the_content(); ?></span>
        </h1>
    
        <?php wp_nav_menu( array( 'theme_location' => 'primary', 'container_class' => 'main-menu') ); ?> 
    
    <?php endwhile; ?>
    
    <div id="content">
        <p>
            <?php 
    
                if ( have_posts() ) {
                    single_post_title();
                    //
                    // In here the "page description text" should show up
                    //
                }
            ?>
        </p>
    </div>
    
    <?php get_footer(); ?>

    Can someone help with the related snippet – thank yo in advance.

    kind regards.

    • This topic was modified 6 years, 5 months ago by lazercaveman.
    • This topic was modified 6 years, 5 months ago by lazercaveman.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You need to call wp_reset_postdata() after your adventskalender loop completes. Then call the_post() within the if ( have_posts() ) conditional before trying to use template tags like the_title(), etc. Then simply calling the_content() will output the requested page content.

    It’s not that important for single page requests, but convention in coding WP templates tells us to run a complete loop to output the request, even if only one post is expected.

    // The Loop
    if ( have_posts() ) :
      while ( have_posts() ) :
        the_post();
        // output title, content, etc.
      endwhile;
    else :
      echo 'Nothing found';
    endif;
    Thread Starter lazercaveman

    (@lazercaveman)

    Hello,

    Thank you for helping. I already solved the problem in a similar way.

    
        <?php 
            if ( have_posts() ) {
                wp_reset_query();
                setup_postdata($post); 
                echo esc_attr(htmlentities(the_content()));
            } else {
                echo "Es wurde noch kein Text definiert.";
            };
        ?>

    Thank you anyway, kind regards

    • This reply was modified 6 years, 5 months ago by lazercaveman.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to display page content’ is closed to new replies.