Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

How to create Custom Templates of the "Content Pages" in One Page WordPress Site?

Hello! I did the Workshop of One Page WordPress Site and it worked fine, great content, by the way! :)

The problem is that each Page has the "content-page.php" template. Is there any way to make Custom Templates like "content-page-portfolio.php" and assign this template to that particular page/section?

Is like we do it with normal Custom Templates ("page-portfolio.php"), and then we just write at the top: "Template Name: Portfolio".

Thanks :)

7 Answers

my first reaction is that you might be able to use the get_template_part function. It is a great way to include sections on your page.

Basically, have your custom html and loop within its own file. The loop in the template part would grab the specific page content you want. You would then use get template part to call that file into your page.

This is helpful when you need to conditionally load different template parts. Maybe provide an example of what you would like to change and we can see if that is really the best approach for your needs.

Thank you, it worked with the get_template_part :)

Hi there! Sofia Martinez,

There a a couple of ways to tackle this one is taking the Wordpress Development course here at Treehouse. It's a simple course to go through, if your creating your own theme. The first six steps are the main lessons to learn when creating a theme. The second option would be using plugins for for theme.You can add plugins to create theme pages but sometimes they don't look like you want them to. I know of a couple plugins that are good but they have to be bought.

https://teamtreehouse.com/tracks/wordpress-development

Hope that helps.

Hi! Thanks, I'm taking the course, actually.

I just have the doubt, because I ran into this Workshop, of how can I create custom templates for each page/section in a Site of one single page, is that possible?

No Prob,

I have done online workshop outside treehouse from other places such as tutsplus and other websites. They have good practices but you can't really ask questions when something breaks on your own project. I usually read documentation and take outside courses to grow my knowledge on a certain topics. Usually when creating a custom page for wordpress you have to make a copy of the page.php re-name it. Then on the top comment-out section on the top of the php document you have to change this to whatever you want to call the new page. You have to have some knowledge of php to create your new page, but once done you can see your new page on the dashboard once you add it to the your ftp or localhost.

Let me know if you have any questions.

Yes, I have done that in a "normal" website. The thing is, that in One Page Website you have a custom page for THE page in the website (the home page). This is the code for that:

<?php /* Template Name: One Page Site */ ?>

<?php get_header(); ?>

    <main role="main">

        <?php $args = array('post_type' => 'page', 'order' => 'ASC');
              $the_query = new WP_Query( $args ); ?>

        <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> 

            <div class="wrapper">

                <?php get_template_part( 'content', 'page' ); ?>

            </div>  

        <?php endwhile; endif; ?>

    </main>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

In this one-page-site.php I call the content-page.php that has a loop to display the other pages in THAT same page, rather than in different pages. This is the code:

<?php global $post; ?>

<article id="<?php echo $post->post_name; ?>" <?php post_class(); ?> >

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

    <div class="content">
        <?php the_content(); ?>

        <?php
            wp_link_pages( array(
                'before' => '<div class="page-links">' . __( 'Pages:', 'one-page-theme' ),
                'after'  => '</div>',
            ) );
        ?>
    </div>
</article>

I want to create a custom "content-page.php" for each Page, rather than all use the same.

I tried to make a content-page-portfolio.php file adding this at the top:

<?php /* Template Name: Portfolio */ ?>

Then going to editing the page, select the Template "Portfolio" (it is available, I can select it). But when I test it, writing something different in the code, it is not displayed.

So, I want to know if there is a way to do it.

I'm sorry, English is not my native language... obviously, I don't know if I'm explaining myself correctly...

No worries,

I know how it feels when your trying to explain something, English isn't my first language either, Spanish is my first language. Let me see if I got you right: Your creating a one page site that scrolls up and down depending on the link that is press on the navigation. But your trying to create a content page with a different look for the other items.

The reason why it might not be working is because your either calling the same things in the array. They might need to be renamed and you would have to use advanced custom post types.

Ok, I'm going to explain it in Spanish... but is the same that I've been saying (for those who doesn't speak Spanish). Hope there isn't a problem :s

Estoy haciendo un sitio en WordPress de una sola página con el Workshop "One Page WordPress Site". Lo que hice fue hacer el archivo one-page-site.php donde va este código:

<?php /* Template Name: One Page Site */ ?>

<?php get_header(); ?>

        <?php $args = array('post_type' => 'page', 'order' => 'ASC');
              $the_query = new WP_Query( $args ); ?>

        <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> 

            <?php get_template_part( 'content', 'page' ); ?>

        <?php endwhile; endif; ?>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Que es un loop y lo que hace es que por cada página que creamos en el Panel de Administración (PA) de WordPress (WP), la despliega usando el template que tenemos en el archivo content-page.php:

<?php global $post; ?>

<article id="<?php echo $post->post_name; ?>" <?php post_class(); ?> >

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

</article> 

Lo que quiero hacer es usar diferentes templates para cada página porque no quiero que la página de Inicio, el Portafolio, la página About y Contact sean iguales.

Puedo poner la diferencia con html dentro de la edición de la página en el PA pero no se trata de eso, se supone que la edición de una página en WP debe ser amigable, ¿no?

Entonces lo que intenté fue hacer algo similar a lo que hacemos con los archivos page.php en una página de WP normal (donde cada link te lleva a una nueva página en vez de a un #id en la misma). Donde hacemos un archivo page-portfolio.php y en la página de Portfolio simplemente se selecciona el template y funciona.

Así que hice un archivo content-page-portfolio.php y le puse arriba el nombre del template, quedando así:

<?php /* Template Name: Portfolio */ ?>

<?php global $post; ?>

<article id="<?php echo $post->post_name; ?>" <?php post_class(); ?> >

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

</article>

Luego fui al PA a editar la página Portfolio y seleccioné el template Portfolio. Luego edité content-page-portfolio.php para probar que en la parte de Portfolio se visualizara el cambio. Pero no, no funciona, se ve igual que las demás.

Así que me pregunto si hay alguna forma de hacer un template diferente para cada sección, o todas deben tener el mismo template.

Gracias por la paciencia :D

No sé si ya me había explicado bien y lo que quisiste decir es que hay que editar esta parte del archivo one-page-site.php:

<?php $args = array('post_type' => 'page', 'order' => 'ASC');
              $the_query = new WP_Query( $args ); ?>

Si es así, sabes cómo tiene que quedar?

Lo de Custom Post Types no sé porque de todas formas necesito un template distinto para que se visualice bien el contenido, ¿no?

Hola Sofia,

Perdon por no responder mas pronto. Ya entendi lo que querias decir vi el video que dijistes. Y lo que el maestro hizo fue copiar page.php para todas las paginas. Y por eso tienes todas las paginas iguales. Y lo que quieres hacer tu es crea tu propio layout para cada pagina individual. Hay dos differentes maneras de hacer esto;

La primera es un poco dificil si no sabes php, ACF y loops para obtener cada pagina que vas a crear. Y despues que termines de usar php para hacer y obtener todas las classes y id's, tu usarias css para ajustar la pagina como quieras.

La segunda option es usar un plugin que ajuste el layout por el dashboard backend. El maestro habla al final del video de la posibilidad de usar plugins para ajustar tu layout. Y con el plugin no tienes que usar php o loops o css.

Yo se de un plugin que yo recomendado a personas que no saben de web pero quieren hacer su propia wordpress website. Pero si tu quieres tu puedes aprender hacer to propias paginas aqui. Pero tendrias que aprender un poco mas. Aqui esta el link para el plugin. http://vc.wpbakery.com/

Hay plugins gratis este plugin tiene su trial version pero este plugin es bueno para todo lo que quieras hacer y es una buena compra.

Buena Suerte. -Victor

Thank you for your answers. I made it work with the get_template_part function with if statements. I don't know if it is the best solution but it worked :)