I am creating many pages and store them into the database. I am first styling them and than storing them. The idea is to use page.php to check the page id and display the needed post. This is all good, but I have many pages in the website. WHich means I have to perform the following statement many time.

 if(is_page(14)) {
   global $wpdb;
   $findContent = $wpdb->get_var("SELECT post_content  FROM wp_posts WHERE post_title = 'Why ......'");
    get_template_part( 'localnav' );
    echo $findContent;

My question is is that a good rpactice or is there a better idea? Note there asome pages which look alike, maybe I can create catogories, but is it possible to select part of the content and put it in a div for example?

  • But if it looks like that and I use the default template for a page, nothing is displayed – Vanina Yordanova Dec 15 '17 at 8:15

WordPress is fetching current post for you, you have to only type to the right url. When you get e.g. to example.com/lorem-ipsum/ WordPress will load automatically post with title Lorem ipsum and display it using page.php template. Your page.php template should be looking something like that.

<?php get_header(); ?>

    <div id="content">

        <?php while ( have_posts() ) : the_post(): ?>

            <h1><?php the_title(); ?></h1>
            <?php the_content(); ?>

        <?php endwhile; ?>


The concept of a loop is difficult to understand for novice developers. Only in the loop you can use functions like the_title orthe_content. Outside of the loop, these functions won't return you anything.


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.