1

I'm trying to create a custom WordPress theme from scratch. As you can see, my file structure looks like this - footer.php, header.php, index.php, about.php, and contact.php within my theme folder:

enter image description here

I've completed the home page (index.php) and now I'm trying to create these other pages and link to them. But when I go to these pages from the home page, the only way that I could get them to link was through this long, ugly link structure:

enter image description here

I'm wondering if I've gone about this the wrong way. If I create the new pages in WordPress instead of manually via FTP, will this allow me to change the permalink structure so that I can have new.gatewaywebdesign.com/contact.php instead of new.gatewaywebdesign.com/wp-content/themes/gatewaywebdesign/contact.php?

I went ahead and tried to create a contact page in the WordPress dashboard but it won't let me get rid of index.php after the site name:

enter image description here

Any other suggestions for editing / trimming link structure? Thanks

2

As @Milo mentioned in his answer, you can't and shouldn't directly call a template file. It is a security risk, and you will no longer be able to access WordPress's engine, so you can't use functions such as the_permalink.

To include a template part in your home.php or any where you wish, you can use get_template_part():

get_template_part( 'relative/path/to/template' );

Don't include .php. It will be added automatically. So, if your about.php is inside /theme/my-theme/folder/, and your home.php is in /theme/my-theme/, you should use this function the following way:

get_template_part( 'folder/about' );

That will include about.php wherever you use the code. Now you have full access to WordPress inside your about.php and there is no need of any ugly URL.

UPDATE

To add a page, create a blank php file named page-whatever.php. Then add the following to its header:

<?php 
/**
 * This is the template for Pages
 *
 * @package YourPackage
 */

get_header();

get_template_part('path/to/portfolio');

Now, you can see your new page template in the back end, while adding a page (under page template drop-down menu).

  • Alright so I have the link from index to about like this: <div class="menu-item about"><a class="menu-link" href=<?php get_template_part ("/wp-content/themes/gatewaywebdesign/portfolio");?>>About</a></div> But when I click on the link, nothing happens. Do I need to get right of the opening and closing php tags / change something else? The path is correct. – HappyHands31 Jun 26 '17 at 2:27
  • @HappyHands31 get_template_part doesn't provide the template in that way. Create a custom page, and then include your portfolio in it using the code. There are a limited number of core php files. The rest should be included by get_template_part. It's like as if you echo the php file in your template, when you use get_template_part. – Jack Johansson Jun 26 '17 at 3:09
  • And so do I create a custom page simply by going to FTP and saying "create new file -- page-about.php", for example? "Include your portfolio in it using the code" - sorry but using what code? get_template_part? How can I use get_template_part correctly so that I can link from index.php (my home page) to the about page? I tried following this tutorial for creating custom pages in WordPress but I don't see a template option in the WP dashboard: i.imgur.com/N0RV3iD.jpg – HappyHands31 Jun 26 '17 at 14:06
  • @HappyHands31 I've added the detail about creating a page template and including your portfolio. – Jack Johansson Jun 26 '17 at 14:41
1

Theme PHP files never get linked to or loaded directly. You create pages via the admin interface, whose content lives in the database, and WordPress maps incoming requests to the appropriate theme file based on the type of request- home, archive, single post, single page, etc.. Have a look at the Template Hierarchy for more info on which theme files get used for each type of request.

The issue of index.php appearing in permalinks is separate. You typically need a server that has mod_rewrite enabled for full "Pretty" Permalinks. Right now you've got what's known as PATHINFO, or "Almost Pretty" permalinks. Have a read through the Using Permalinks page for more info on the requirements for each.

  • Right so have I named my files properly? I.e. if I want to link to about.php, should it literally be called about.php within the theme folder or should it be called single.php? That's where I'm confused - the template hierarchy says that single.php is what will display a singular page - but then all of the other singular pages would have to be named single.php also - how would we know which one is which? – HappyHands31 Jun 26 '17 at 1:39
  • As far as the permalinks - where do I declare the mod_rewrite module? Is there certain code that I need to insert / change in a certain file? I'm using hostgator. – HappyHands31 Jun 26 '17 at 1:42
1

If what you want is to have different template on //site.com/contact/ then you need to create a page, set this permalink to it and create template file called page-contact.php.

That is how WP works:

  1. Search for specific template (such as our page-contact.php)

  2. Search for generic template (e.g. page.php, single.php and so on)

  3. Use index.php

So if it can't find 1st it goes to 2nd and then falls to index.php. Just take a look at how WP template hierarchy built

  • I'm trying to follow this tutorial on how to create a custom page. But after creating the file and uploading it via FTP to my theme folder: i.imgur.com/Y9h0AMS.png I still don't see an option to select that template - or any template - in the WordPress dashboard: i.imgur.com/N0RV3iD.jpg Selecting "Parent" just gives me the options "Contact" and "Sample Page", but there's no option for selecting the template. Any ideas? – HappyHands31 Jun 26 '17 at 13:55
  • @HappyHands31 two ways: 1. You need to name your template as page-permalink.php where permalink is slug for your page (you can set it under page title) as I said before 2. It seems you forgot to add <?php /* Template Name: CustomPage1 */ ?> in the beginning of your template. – KazeZlat Jun 29 '17 at 12:19
0

The answer to this question is yes, creating the pages in WordPress does allow you to change the link structure. What I needed to do was rework index.php so that it used the WordPress Loop, and then displayed the front page as whatever the front page was set to in the WordPress dashboard. So my index.php ended up looking like this:

<?php get_header();?>

<?php if ( have_posts() ) : ?>

// Set front page
<?php if ( is_home() && ! is_front_page() ) : ?>
// Display page title
<header>
<h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
</header>
<?php endif; ?>

<?php
// Start the loop.
while ( have_posts() ) : the_post();

/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );

// End the loop.
endwhile;

// Previous/next page navigation.
/* the_posts_pagination( array(
'prev_text'          => __( 'Previous page', 'twentyfifteen' ),
'next_text'          => __( 'Next page', 'twentyfifteen' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>',
) );*/

// If no content, include the "No posts found" template.
else :
get_template_part( 'content', 'none' );

endif;
?>

<?php get_footer();?>

Make sure that your theme has all the necessary core files - single.php, content-link.php, content-none.php, content-page.php, content-search.php, content.php, and page.php.

Then in WordPress, create a menu and set the pages that you've created in WordPress to be in that menu.

In header.php, set the links to the different pages in your nav menu like this:

<div class="menu-container"><!--menu items-->
<?php $main_menu = inits_get_main_menu();
        foreach ($main_menu as $menu) { //var_dump( $menu); exit; ?>
        <div class="menu-item contact">
        <a class="menu-link" href="<?php echo  $menu->url; ?>">
        <?php echo  $menu->title; ?>
        </a>
        </div>
<?php } ?>
</div>

Now your menu has been set in the header, and when you click on one of the links to your other pages, the link structure will look the way that you want it to.

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.