• Resolved thetoolman123

    (@thetoolman123)


    Hi,

    I have the following code in page.php:

    <section id="title">
    	<div class="container">
    		<h1><?php the_title(); ?></h1>
    	</div>
    </section>

    What I am trying to do is to add my own content below the <h1> tag, but have different content on each page that uses page.php

    Is this possible, or would it be better to create a new template for each page?

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter thetoolman123

    (@thetoolman123)

    No to worry, I just created a new template 🙂

    Moderator bcworkz

    (@bcworkz)

    You’re going to create a new template for every new page that needs different content inserted? That defeats the purpose of using templates! Content unique to each page should be stored in the DB and fetched and output by the same template code used for all such pages.

    If you want to stay with your solution, that’s fine, it’s your site. But if you want to learn how to properly utilize a CMS system, we can discuss further.

    Thread Starter thetoolman123

    (@thetoolman123)

    Hi,

    I only have a few pages so templates will work better for me I think.

    How would you recommend achieving different content for each page? The way it is done in the theme is that the page title is outputted from each page into the code I posted.

    Moderator bcworkz

    (@bcworkz)

    You may think only a few pages need this today. What about a few months from now when the need increases? Maybe it will, maybe it won’t. It’s still wise to anticipate reasonable growth.

    You can use the custom fields meta box to enter the desired content for each individual page. Admittedly, this meta box does not present the best UI, but it works. You could use a custom fields plugin like Advanced Custom Fields to build a better UI if you like. For example, an ACF field could be a large <textarea> for entry of larger amounts of content.

    Either way, such data entered is saved in the post meta table. As an example, lets say in the custom field you entered “The best foobars available!” under the name “my-content”. To display this on the page, add to your page template something similar to:

    $my_content = get_post_meta( get_the_ID(), 'my-content', true );
    if ('' != $my_content ) {
      echo "<div class=\"my-content\"><h3>$my_content</h3></div>";
    }

    Then this one template will suffice for all such pages. If a page does not have any content defined, nothing happens, it’s as if the added code did not exist. When content is defined, the unique value saved with the page is output along with the common div and h3 HTML. The same template is used even if there are 100 pages with differing content and hundreds more without. It could be your theme’s default page template if you want.

    Another situation that might arise is you only have four different messages which are selected based on an assigned taxonomy term or category. While you could have four different templates, one for each term, you could instead use a switch/case structure to define the four messages and selectively output one of them.

    switch (wp_get_post_terms(get_the_id(),'category')[0]->name) {
      case 'foo':
        $msg = 'first message';
        break;
      case 'bar':
        $msg = 'second message';
        break;
    // declare two more cases...
    
      default:
        $msg = '';
    }
    if ('' != $msg ) {
      echo "<div class=\"my-msg\"><h3>This is the $msg.</h3></div>";
    }

    One block of code covers all four possible conditions plus the case where no message should output. Again, one template could cover the needs for many dozens of pages.

    Typically the reason to create multiple templates should be limited to significant changes to the total layout of a page where coding it all on one template would be cumbersome and it’d be difficult to read the resulting code.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘changing content on individual pages via code’ is closed to new replies.