WordPress.org

Ready to get started?Download WordPress

Forums

Custom Page Template (9 posts)

  1. RobbieB1953
    Member
    Posted 3 months ago #

    I've created a page template to call in a sidebar with dynamic content. The code below is the last few line with the changes I have used to make my page template - but it's not registering in my page template selection options in the dashboard. I may have the wrong code...

    </div><!-- #content -->
    	</div><!-- #primary -->
    	<?php get_template_part( 'sidebar-content2' ); ?>
    </div><!-- #main-content -->
    
    <?php
    get_sidebar();
    get_footer();

    But now this has created another question. I made a test page template, just a duplicate of the page default (below) and loaded it as alt-page.php and it didn't register in my page template selection options either. Am I using the wrong name structure? Missing another step? Thanks!:

    <?php
    /**
     * The template for displaying all pages
     *
     * This is the template that displays all pages by default.
     * Please note that this is the WordPress construct of pages and that
     * other 'pages' on your WordPress site will use a different template.
     *
     * @package WordPress
     * @subpackage Twenty_Fourteen
     * @since Twenty Fourteen 1.0
     */
    
    get_header(); ?>
    
    <div id="main-content" class="main-content">
    
    <?php
    	if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
    		// Include the featured content template.
    		get_template_part( 'featured-content' );
    	}
    ?>
    	<div id="primary" class="content-area">
    		<div id="content" class="site-content" role="main">
    
    			<?php
    				// Start the Loop.
    				while ( have_posts() ) : the_post();
    
    					// Include the page content template.
    					get_template_part( 'content', 'page' );
    
    					// If comments are open or we have at least one comment, load up the comment template.
    					if ( comments_open() || get_comments_number() ) {
    						comments_template();
    					}
    				endwhile;
    			?>
    
    		</div><!-- #content -->
    	</div><!-- #primary -->
    	<?php get_sidebar( 'content' ); ?>
    </div><!-- #main-content -->
    
    <?php
    get_sidebar();
    get_footer();
  2. catacaustic
    Member
    Posted 3 months ago #

    You have forgotton one of the most important parts - the template name!

    Using that with your current file, you'd need it to look like this:

    <?php
    /**
     * Template Name: My Template
     *
     * The template for displaying all pages
     *
     * This is the template that displays all pages by default.
     * Please note that this is the WordPress construct of pages and that
     * other 'pages' on your WordPress site will use a different template.
     *
     * @package WordPress
     * @subpackage Twenty_Fourteen
     * @since Twenty Fourteen 1.0
     */

    Of cours,e you'd change 'My Template Name' for whatever you want your template to be called, but that one line at the top is all that you should need to do.

  3. RobbieB1953
    Member
    Posted 3 months ago #

    Hey thanks, catacaustic. this is the second time you've saved me.

    I'm still learning - I thought that /** content */ was just for internal info/benign but of course you're right!

  4. catacaustic
    Member
    Posted 3 months ago #

    Well, I do try to help out. :)

    You are right though. The comments can hold anything that you want tem too, and is definately the right place to put all of the stuff that is internal, or meta info, but there's still more to it in the few template files, orstyle.css files. And I have to admit that I knew it so fast after reading your question because I forgot to put the same line in a template file that I made today... so we all do it!

  5. RobbieB1953
    Member
    Posted 3 months ago #

    OK. The custom page template is registering. But I must be missing something in my custom sidebar template. Because the content I want to appear on only one page is appearing on all pages. I'm using this custom page template (named now):

    <?php
    /**
     * The template name: Custom Page
     *
     *
     * @package WordPress
     * @subpackage Twenty_Fourteen
     * @since Twenty Fourteen 1.0
     */
    
    get_header(); ?>
    
    <div id="main-content" class="main-content">
    
    <?php
    	if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
    		// Include the featured content template.
    		get_template_part( 'featured-content' );
    	}
    ?>
    	<div id="primary" class="content-area">
    		<div id="content" class="site-content" role="main">
    
    			<?php
    				// Start the Loop.
    				while ( have_posts() ) : the_post();
    
    					// Include the page content template.
    					get_template_part( 'content', 'page' );
    
    					// If comments are open or we have at least one comment, load up the comment template.
    					if ( comments_open() || get_comments_number() ) {
    						comments_template();
    					}
    				endwhile;
    			?>
    
    		</div><!-- #content -->
    	</div><!-- #primary -->
    	<?php get_template_part( 'sidebar-content2' ); ?>
    </div><!-- #main-content -->
    
    <?php
    get_sidebar();
    get_footer();

    And this is my custom sidebar template:

    <?php
    /**
     * Template Name: Sidebar Content2
     *
     * @package WordPress
     * @subpackage Twenty_Fourteen
     * @since Twenty Fourteen 1.0
     */
    
    if ( ! is_active_sidebar( 'sidebar-2' ) ) {
    	return;
    }
    ?>
    <div id="content-sidebar" class="content-sidebar widget-area" role="complementary">
    	<?php dynamic_sidebar( 'sidebar-2' ); ?>
    </div><!-- #content-sidebar -->

    But the sidebar content is appearing on all pages. What am I missing? Thanks!

  6. catacaustic
    Member
    Posted 3 months ago #

    The file for the sidebar isn't a page template like the first one is, so that doesn't need the name in the header comments (Yeah, I know that's a bit confusing). Those sort of templates get recognised by the file name. So you'd need to call it sidebar-content2.php and save it alongside the standard sidebar.php file.

    To use that in your template file, you'd call the finction:

    <?php get_sidebar ( 'content2' ); ?>

    That will use that sidebar for that file only, and the default sidebar.php one will be used for everything else.

  7. RobbieB1953
    Member
    Posted 3 months ago #

    Which line does this go on?

    <?php get_sidebar ( 'content2' ); ?>

  8. catacaustic
    Member
    Posted 3 months ago #

    You place that in the custom page template file, replacing the current call to get_sidebar().

  9. RobbieB1953
    Member
    Posted 3 months ago #

    Still missing something. I'm still getting the sidebar content on all my pages. Here's my page template:

    <?php
    /**
     * The template name: Custom Page
     *
     *
     * @package WordPress
     * @subpackage Twenty_Fourteen
     * @since Twenty Fourteen 1.0
     */
    
    get_header(); ?>
    
    <div id="main-content" class="main-content">
    
    <?php
    	if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
    		// Include the featured content template.
    		get_template_part( 'featured-content' );
    	}
    ?>
    	<div id="primary" class="content-area">
    		<div id="content" class="site-content" role="main">
    
    			<?php
    				// Start the Loop.
    				while ( have_posts() ) : the_post();
    
    					// Include the page content template.
    					get_template_part( 'content', 'page' );
    
    					// If comments are open or we have at least one comment, load up the comment template.
    					if ( comments_open() || get_comments_number() ) {
    						comments_template();
    					}
    				endwhile;
    			?>
    
    		</div><!-- #content -->
    	</div><!-- #primary -->
    	<?php get_sidebar( 'content2' ); ?>
    </div><!-- #main-content -->
    
    <?php
    <?php get_sidebar( 'content2' ); ?>
    get_footer();

    And here's my sidebar template - saved as sidebar-content2.php:

    <?php
    /**
     * The Content Sidebar
     *
     * @package WordPress
     * @subpackage Twenty_Fourteen
     * @since Twenty Fourteen 1.0
     */
    if ( ! is_active_sidebar( 'sidebar-2' ) ) {
    	return;
    }
    ?>
    <div id="content-sidebar" class="content-sidebar widget-area" role="complementary">
    	<?php dynamic_sidebar( 'sidebar-2' ); ?>
    </div><!-- #content-sidebar -->

    Thanks for looking at this!

Reply

You must log in to post.

About this Topic