0

When I use the_content in the page-template doesn't show anything.

Loop to call the page-template in the index:

index.php

<?php
/**
 * Main template file
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage Twenty_Eleven
 */

get_header();
$pages = get_pages();
foreach ($pages as $page_data) {
    $title = $page_data->post_title;
    $template =  get_post_meta( $page_data->ID, '_wp_page_template', true );
    switch($template) {
        case 'template-portfolio.php':
        get_template_part( 'includes/portfolio' );
        break;
        case 'template-landing.php':
        echo get_template_part( 'includes/landing' );
        break;
    }

}

get_footer();

landing.php:

<div id="opening" style="width:100%; height:100vh;  background: white; background-size:cover;  margin: 0 0 -86px 0; ">
        <div id="text_opening">
    <!--<?php  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;  ?>
    Stai visitando la pagina <?php echo $paged; ?>-->

    <?php 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
    ?>
    </div>
</div>

template-landing.php :

<?php
/*
Template Name: Landing
*/
get_header();
echo get_template_part( 'includes/landing' );
get_footer();

But it doesn't work, i can see only the div with the class "entry-content-page" but it's empty
I need to display only the content.
The strange thing is that if I click on "view page" in the admin panel, the preview show the content but in my site isn't there.
Any ideas?
Thanks!

EDIT:

PAGE VIA "VIEWPAGE":via view-page PAGE IN THE HOME: enter image description here

EDIT2:

I tried to go to the "reading setting" and if I set that page (template-landing) like front page I can see the content

7
  • 1
    I see a call to wp_reset_query, why is that there? It suggests you've used query_posts somewhere ( never use query_posts, seriously ). Can you expand your code example to include relevant parts such as any querys you make prior to the code? Can you also wrap your code in an if( have_posts() ) and print something out in the else case? You're looping over posts but you're not checking if you have any posts to begin with
    – Tom J Nowell
    May 14, 2014 at 22:55
  • @TomJNowell, i added more info in the question
    – dborghez
    May 14, 2014 at 23:07
  • Can you expand further? It's difficult to see how this all fits together, post entire files and post their filenames too.
    – Tom J Nowell
    May 15, 2014 at 9:16
  • @TomJNowell, I added more code and put everything in the right order for better explanation
    – dborghez
    May 15, 2014 at 11:26
  • It's still confusing, if it's what i think it is, you're going to get the main loop executed multiple times, and you're not giving it new content so the first time it runs it'll go through all the posts, and the second time it runs it'll have already gotten all the posts. Either you're expecting it to grab all these posts magically out of thing air, or you're not showing us the queries that retrieve them. This is an XY problem question. This whole thing is incredibly confusing and I'm still clueless a to what you are actually trying to build
    – Tom J Nowell
    May 15, 2014 at 14:44

2 Answers 2

2

Have you tried using the whole loop?

<?php $loop = new WP_Query();
if ( $loop->have_posts() ) : 
    while ( $loop->have_posts() ) : 
    $loop->the_post(); ?>
    <!-- before -->
        <?php the_content(); ?>
    <!-- after -->
    <?php endwhile; else: ?>
    <p><?php _e( 'Nope. Doesn\'t exist' ); ?></p>
<?php endif; ?>
7
  • I tried in this way, and the result is "Nope. Doesn't exist" :/
    – dborghez
    May 14, 2014 at 22:59
  • Then you have no content there it would seem. WHat do you see what you go into wp-config.php and put wp-debug to true? May 15, 2014 at 11:21
  • yep I saw that,but I can't understand why it's empty and why I can see the content when I go with the direct url? Check the images above. WP-DEGUB is enabled, it doesn't show anything (like you can see in the screenshots)
    – dborghez
    May 15, 2014 at 11:31
  • My final thing would be check your settings > reading, and see what is your front page, which is your blog page May 15, 2014 at 14:45
  • Just seen your edit, nice one May 15, 2014 at 14:45
0

SOLVED IN THIS WAY: (index.php)

<?php 
             
            $args = array(
            'child_of' => 0,
            'order' => 'ASC',
            'orderby' => 'menu_order',
            'hierarchical' => 1,
            'post_type' => 'page',
            'post_status' => 'publish',
            'post_parent' => '0',
            'posts_per_page' => '100'
            ); 

            

      $wp_query = query_posts($args);
      query_posts($args); // Query all published pages without parents
      $first_loop = true;
              
        if (have_posts()) :
        while (have_posts()) : the_post(); 
            //Determine template for page style
                        $template = get_post_meta( get_the_ID(), '_wp_page_template', true );
                        $parent_id = get_the_ID();
                        
                         switch($template) {
                             
                            case 'template-portfolio.php':
                                    get_template_part( 'includes/portfolio' );                                                      
                            break;
                        case 'template-landing.php':
                                    echo get_template_part( 'includes/landing' );                                                   
                        break;                       
                             
                         }
        endwhile;
        endif;
        ?>  
1
  • 1
    query_posts is a bad idea. WP_Query should be used instead May 17, 2014 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.