Customising Specific Page Content

Hi,

Am I right in thinking that in order to most efficiently customise the content, on a page called ‘kudos’, I would need to do the following steps…

  1. In the top level, duplicate page.php and call it page-kudos.php.
  2. In the templates directory, duplicate content-page.php and call it content-kudos.php.
  3. Back in the top level, in page-kudos.php, modify the second ‘get_template_part’ to point to content-kudos.php.

Having a basic understanding on how sage uses DRY; does the method I outlined above not have a lot of duplicate code in page-kudos.php? Is there a better method that I am overlooking?

Thanks,
Andy.

Hi Andy,

Your process is not wrong, but it’s not something I would recommend if you have lots of identical page-*.php pages.

Instead you can use something like this in page.php:

<?php while (have_posts()) : the_post(); ?>
  <?php get_template_part('templates/page', 'header'); ?>
  <?php get_template_part('templates/content-page', basename(get_permalink()); ?>
<?php endwhile; ?>

This will then load the content-page template relevant to the slug. So when the slug is kudos, content-page-kudos.php will be loaded, with a fallback to content-page.php if the kudos content template does not exist.

Sage is a starter theme so we don’t decide everything for you. You need to figure out what will be changing the most in your design and tailor the templates towards that.

I hope that helps.

1 Like

Thats brilliant thanks. Much appreciated.

This is probably a really dumb question, but let’s say I want to display posts from a particular category but I want the page name to be different than the category. Do I create a page-whatever.php or a category-whatever.php file?

Thanks,
Jason

You can pull in posts from a specific category wherever you want.

You have lots of options, but this should probably be it’s own thread. You can make a category-whatever.php file, edit the titles.php file, etc. Not sure the link @andycheeseman posted is what you need, but if it is, go for it!

I got an error when I used the code above and am mostly a lurker here who doesn’t know too much about theming or php syntax but I am learning fast. As soon as I solve one issue, there’s always another. I discovered that there were missing quotes in the above code but this code stopped the errors. Still trying to figure the rest out to get the proper page template loading.

<?php while (have_posts()) : the_post(); ?>
  <?php get_template_part('templates/page', 'header'); ?>
  <?php get_template_part('templates/content-page', 'basename(get_permalink()'); ?>
<?php endwhile; ?>

The original code was missing a closing bracket, not quotes. Try replacing the offending line with the following:

  <?php get_template_part('templates/content-page', basename(get_permalink())); ?>
1 Like