0
  1. I've created a custom theme, and I want to display a custom theme page that I created, whose file name is page-about.php.

  2. The only way that I know that it's possible to display custom theme pages in WordPress is to make their files templates, and then create a page via the WP-Admin whose template is of the desired file, so what I did, is added at the top of page-about.php the comment in PHP: Template Name: About Me.

  3. I, then, created a page via the WP-Admin called page-about.

  4. The contents of the pages are printed via the following loop in my functions.php page:

    function display_all_pages() {
    $pages = get_pages();
    foreach ($pages as $page) {
            $id = $page->post_title;
            $id = strtolower(str_replace(' ', '_', $id));
            $class = "page";
            $content = $page->post_content;
            echo ('<div ' . 'id="' . $id . '"' . 'class="' . $class . '">' . $content . '</div>');
        }
    }
    
  5. And this is the content of the page-about.php file:

    <?php
    /*
    *  Template Name: About Me
    */
    get_header();
    ?>
    
    <?php echo "hello"; ?>
    
    <body <?php body_class(); ?>>
    
    <p>
    This is the About Me page.
    </p>
    
    </body>
    
  6. I've checked by doing var_dump on $page inside the loop, and saw that post-content of about-page is empty. The problem is that my loop doesn't print anything. It makes sense to me that the post-content is empty, because the I inserted no content to page-about on WP-Admin. But, on the other hand, I saw in an old video, from about here that I did something that should work.

Why doesn't what I do work, and what do I need to do to make it work? Thanks in advance.

  • Are you trying to run your display_all_pages function in your template? Because, from your code, it doesn't appear that you are. – socki03 Jun 7 '17 at 16:36
  • Yes, display_all_pages runs, and displays successfully all the other pages that are were created. Just a side note: I don't have a page.php file. Is this relevant? My website is a one-pager, so display_all_pages is called only in my index.php file. – Gal Grünfeld Jun 7 '17 at 16:57
  • No, it's not relevant, but it is a fallback. – socki03 Jun 7 '17 at 16:59
0

I think your issue is stemming from what you said about not having any content in the about page. That content that you are trying to request would come from the content wysiwyg in the back of wordpress. Anything you print out on the template doesn't automatically get stored in the database, which is what you're requesting using that function.

  • Then how can I print the contents of the the file? I assume that there's a standard way to do that. Basically, I just want to create the file such that I could use PHP in it, and edit conveniently via my text editor. – Gal Grünfeld Jun 7 '17 at 17:02
  • Then you're going to want to use the get_template_part function. But i'd recommend removing get_header from your file, so you don't load it twice. developer.wordpress.org/reference/functions/get_template_part – socki03 Jun 7 '17 at 18:16
  • Is it possible for me to build the page not as a template? – Gal Grünfeld Jun 7 '17 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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