0

here is my scenario: I got an index.php which loads every page I got one below the other. It's a one-pager in the end.

I found this snippet online and It helped me a lot by creating this.

      <?php
        $mypages = get_pages( array('child_of' => 1503, 'sort_column' => 'menu_order') );

        foreach( $mypages as $page ) {
            $content = $page->post_content;
            if ( ! $content ) // Check for empty page
            continue;

            $content = apply_filters( 'the_content', $content );
        ?>
        <section>
            <div class="headlines info-header"><img src="<?php echo get_template_directory_uri(); ?>/assets/images/arrow.png" alt="arrow"><?php echo $page->post_title; ?></div>
            <div class="page-content"><?php echo $content; ?></div>
        </section>
        <?php
        }
      ?>

It seems to work just fine. But now to the problem part of this. I want to display the posts also on this one page. My try was to create another page news and place a widget on this page which shows the latests posts, filtered by category.

But now the page title won't show up anymore, all I got is the following

notice: NOTICE: TRYING TO GET PROPERTY 'POST_TITLE' OF NON-OBJECT IN

So I thought the problem might have to do with the little <?php echo $page->post_title; ?> I already tried to just get the_titlebut I ended up getting the same error.

I'm kinda new to Wordpress so I don't know how to deal with this. Is there another way to get the page title on my index.php?

Best, Dominik

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
dwd0tcom
  • 37
  • 7
  • 1
    Possible duplicate of [Laravel get property of non object](https://stackoverflow.com/questions/33655965/laravel-get-property-of-non-object) – delboy1978uk Aug 30 '18 at 11:18
  • 2
    $page is NOT an object. Check with a var dump to actually see what you have. possibly an array? – delboy1978uk Aug 30 '18 at 11:19
  • Possible duplicate of [Wordpress Categories: Trying to get property of non-object](https://stackoverflow.com/questions/33468518/wordpress-categories-trying-to-get-property-of-non-object) – Masivuye Cokile Aug 30 '18 at 11:34
  • @MasivuyeCokile Thanks. But this time this method isn't working because I need the title of the page on the index.php. When I turn of the debug mode, the notice is gone but my is still missing. – dwd0tcom Aug 30 '18 at 12:13

1 Answers1

0

I managed to fix this. As @delboy1978uk told me, I check if the title is not empty by adding 2 lines of code.

Here is the final code:

          <?php
        $mypages = get_pages( array('child_of' => 1503, 'sort_column' => 'menu_order') );

        foreach( $mypages as $page ) {
            $content = $page->post_content;
          $title = $page->post_title;
            if ( ! $content ) // Check for empty page
          if ( ! $title) // Check for empty title < NEW!
            continue;

            $content = apply_filters( 'the_content', $content );
        ?>
        <section>
            <div class="headlines info-header"><img src="<?php echo get_template_directory_uri(); ?>/assets/images/arrow.png" alt="arrow"><?php echo $title; ?></div>
            <div class="page-content"><?php echo $content; ?></div>
        </section>
        <?php
        }
      ?>
dwd0tcom
  • 37
  • 7