Why isn't this php code removing the header and footer? - php

This code came with the theme, its suppose to remove header and footer but doesn't do it. What can i add to it to make it work? Thanks
Im trying to remove it from the following page http://www.mobiletechrx.com/tuts3/ but it wont seem to render without these elements. Thanks!
<?php
/**
* Template Name: No Header Or Footer Page
*/
?>
<?php get_header(); ?>
<?php while (have_posts()) : the_post(); ?>
<section style="background-image: url()" class="banner alternate_page">
<div class="container">
<div class="main_cap">
<div class="caption">
<h2>
</h2>
</div>
</div>
</div>
</section>
<section class="north_america default_page_with_sidebar">
<div class="container">
<div class="fullwidth_part">
<?php the_content(); ?>
</div>
</div>
</section>
<section class="mobiletech_built mobiletech_built_cutom">
<div class="container">
<div class="title_2">
<h2></h2>
</div>
<ul>
</ul>
</div>
</section>
<?php wp_reset_query(); ?>
<?php endwhile; ?>
<?php get_footer(); ?>

You have <?php get_header(); ?> and <?php get_footer(); ?> in there - what do you expect - these tags are there to include the header and footer?
You can erase those, but you have to make sure to still maintain a complete and valid HTML page structure - especially the header.php contains also lots of "invisible" stuff that you need.

You can remove the header and footer by removing <?php get_header(); ?> and <?php get_footer(); ?> respectively as johannes suggested. But keep in mind that the <head> tags is contained in the header and the <body> and <html> tags are included in both the header and footer.
What you can do is copy the HTML structure from header.php up to the <body> tag and paste in the template file. The same goes for the footer, copy the HTML structure that you need in footer.php and paste it in the theme file. So it will look something like this:
<?php
/**
* Template Name: No Header Or Footer Page
*/
?>
<!-- This is the start of the contents copied from header.php -->
<html>
<head>
<!-- Some Codes from your header.php-->
<?php wp_head();?>
<!-- Some scripts and styles-->
</head>
<body>
<!-- This is maybe the end of the contents copied from header.php -->
<?php while (have_posts()) : the_post(); ?>
<section style="background-image: url()" class="banner alternate_page">
<div class="container">
<div class="main_cap">
<div class="caption">
<h2>
</h2>
</div>
</div>
</div>
</section>
<!-- Some codes -->
<?php wp_reset_query(); ?>
<?php endwhile; ?>
<!-- Some of the contents copied from the footer.php. That includes the scripts called outside the wp_footer action hook and such.-->
<?php wp_footer();?>
</body>
</html>

Related

CSS won't link to my page.php template in Wordpress, everything else works fine

So I am going through a Wordpress theme course, and we are creating our page template setup. The page is loading fine with
<?php get_header(); ?>
<section id="page-template">
<div class="container">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</div>
</section>
<?php get_footer(); ?>
All my css files are linked into the header, but for some reason when I load up the pages I create, they don't load up the CSS we created for the page. We are importing all the css files into one main.css file, this is the one I am having trouble with.
#import "pagetemplate/page.css";
and in that file we have some basic css to test it out and make sure it connects.
#page-template {padding: 80px 0;}
#page-template .container {padding: 0 15px;}
Sorry if it's all over the place, I will be glad to expand on any questions.
I should add, my header.php page has the css link of
<link rel="stylesheet" href="<?php echo get_bloginfo('template_directory'); ?>/css/main.css">
Change the php code to
<?php bloginfo("template_directory"); ?>
So you link look like
<link rel="stylesheet" href="<?php bloginfo("template_directory"); ?>/css/main.css">
Ok, so I just figured it out. I am not sure if it's good practice the way I did it but I got it! I just linked the css folder and file straight into the page.php.
<link rel="stylesheet" href="<?php echo get_bloginfo('template_directory'); ?>/css/page-template/mypage.css">
I added this regular css link into my page.php template, since my get_header() is not recognizing my css link from my main page.
Here is the final view.
<link rel="stylesheet" href="<?php echo get_bloginfo('template_directory'); ?>/css/page-template/mypage.css">
<?php get_header(); ?>
<section id="page-temp">
<div class="container">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</div>
</section>
<?php get_footer(); ?>

How to include the header and the footer of wordpress website to a custom php file?

I have a custom php file that I want to include the header.php and the footer.php files to it , The file is located inside the theme files next to header.php and footer.php , but when I try
<?php get_header(); ?>
I get an error , Undefined function get_header.
Is there is a way to include it or I would have to use include/require to include them manually?
Did you create those files in your project? You should create a footer.php and header.php on your project.
Your page should look something like so:
<!-- The header of the page -->
<?php get_header(); ?>
<!-- The main wrapper of the page -->
<main class="main-container container_960">
<!-- Main content Section -->
<section class="main-content">
<!-- Printing the content -->
<?php if (have_posts()) : while (have_posts()) :the_post() ?>
<?php
the_title("<h1>","</h1>");
the_content();
?>
<?php endwhile; else : ?>
<!-- Showing that there is no content -->
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</section>
<!-- Pinting the Sidebar -->
<?php get_sidebar(); ?>
</main>
<!-- Printing the footer -->
<?php get_footer() ?>
Create a file for the header, call it header.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php the_title();?></title>
<!-- This is for adding the meta information, where styles registered in functions php -->
<?php wp_head(); ?>
</head>
<body>
<header class="the_header">
<!-- You can create your content here
</header>
...
And you should create another file for the footer,again footer.php:
<footer class="main-footer">
The footer
</footer>
<!-- We call here the scripts enqeued in functions.php-->
<?php wp_footer();?>
</body>
</html>

Which is the best way to load header, nav and footer? I used php but I'm not sure

I have got header.html, nav.html and footer.html. I need to load them on a index.php.
is it right? Because this works but I don't know if this is the best way to do.
index.php
<html>
<body>
<header><?php require('../layout/header.html')?></header>
<nav><?php require('../layout/nav.html')?></nav>
<div id="content">Hello World</div>
<footer><?php require('../layout/footer.html')?></footer>
</body>
</html>
header.php
<html>
<body>
<img src="../images/header.png">
</body>
</html>
You cannot have multiple <html> tags in the resulting HTML, i.e., the following code is not valid:
<html>
<html>
<header></header>
</html>
</html>
When you split your HTML into different files in order to reuse code, you have to consider that they will be, probably, injected in a pre-existent HTML code. Therefore, I would suggest the following code:
main.html
<html>
<body>
<!-- Header -->
<?php require('../layout/header.html')?>
<!-- Nav -->
<?php require('../layout/nav.html')?>
<!-- Content -->
<div id="content">Hello World</div>
<!-- Footer -->
<?php require('../layout/footer.html')?>
</body>
</html>
layout/header.html
<header>
<!-- Your header HTML -->
</header>
layout/nav.html
<nav>
<!-- Your Nav HTML -->
</nav>
layout/footer.hml
<footer>
<!-- Your footer HTML -->
</footer>

Where/How do I include the navigation bar in a yii view

I am new to PHP frameworks and yii and I am trying to understand how views/layouts work. This is how I currently understand how to layout a page template:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="page_wrp">
<?php include 'includes/top_right_nav.php' ?>
<div id="content_wrp">
<div id="content">
</div>
</div><!--#content_wrp-->
<?php include 'includes/main_nav.php' ?>
</div><!--#page_wrp-->
</body>
I understand that basic yii way is more like this:
<?php /* #var $this Controller */ ?>
<!DOCTYPE>
<html>
<head>
<title><?php echo CHtml::encode($this->pageTitle); ?></title>
</head>
<body>
<div class="container" id="page">
<div id="header">
<div id="logo"><?php echo CHtml::encode(Yii::app()->name); ?></div>
</div><!-- header -->
<div id="mainmenu">
<!--
DON'T WANT TO USE ZII.WIDGETS FOR MENU
WANT TO KNOW BEST WAY TO INCLUDE MY ONW NAVIAGTION MENU-
->
<?php $this->widget('zii.widgets.CMenu',array('...')); ?>
</div><!-- mainmenu -->
<?php echo $content; ?>
<div id="footer"></div><!-- footer -->
What I don't understand is what is the best practice/analogous way to include my own navigation bar in the context of yii/MVC. Can I still use includes as above or do I have to use zii.widgets? If it's ok to use includes where do I put the files?
Any help would be much appreciated.
You don't have to use Yii widgets however it's recommended to use them, they are quite flexible.
Make a view file for your menu markup in the layouts folder, something like _myMenu.php
_menu.php
<ul>
<li>menu item 1</li>
<li>menu item 2</li>
</ul>
then in your layout use renderPartial() to render your _menu.php partial view inside your layout.
main.php
<div id="mainmenu">
<?php $this->renderPartial("//layouts/_menu"); ?>
</div>
This is a really simple solution, if you want to make the menu more dynamic you can add a method to your controller to handle this.

How could I improve this template system?

At the moment, I have a base HTML template file. When ever I want to make a new page, I copy the template and place some require_once statements in between specific tags. I was wondering if there's a better way that would make it unnecessary to copy the template each time. Here's a typical example:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/second.css" />
<script language="JavaScript" type="text/javascript"
src="js/validation_functions.js"></script>
<title>Order a Ticket for the ball</title>
</head>
<body>
<div id="banner">St. Tom's Ambulance Ball</div>
<!-- START[container] -->
<!-- "body" -->
<div id="container">
<!-- START[header] -->
<div id="header">
<!-- header -->
<div id="header_text">introduction</div>
<div id="header_cell2">the process</div>
<div id="header_cell3">start</div>
</div>
<!-- END[header -->
<!-- START[content] -->
<!-- "other container" -->
<div id="content">
<!-- START[form] -->
<div id="form">
<?php
require_once(realpath($config["directories"]["views"]."/index.form.view.php"));
?>
</div>
<!-- END[form] -->
<!-- START[data] -->
<!-- "main content" -->
<div id="data">
<?php
require_once(realpath($config["directories"]["views"]."/index.data.view.php"));
?>
</div>
<!-- END[data] -->
<!-- START[side] -->
<div id="side">
<?php
require_once(realpath($config["directories"]["views"]."/index.side.view.php"));
?>
</div>
<!-- END[side] -->
</div>
<!-- END[content] -->
<!-- START[footer] -->
<div id="footer">
<!-- footer -->
<div id="footer_text">
<ul>
<li>home</li>
<li>partners</li>
<li>projects</li>
<li>contact us</li>
</ul>
</div>
<div id="footer_cell2"> </div>
<div id="footer_cell3"> </div>
</div>
<!-- END[footer] -->
</div>
<!-- END[container] -->
</body>
</html>
EDIT: I have taken note of your suggestions to use GET. The new idea is to have each request url formed as index.php?page=page_name. This request would then be dealt with by a main controller which then sets the variables of the template based on the value of $_GET['page']. For this, the template will now be:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/second.css" />
<script language="JavaScript" type="text/javascript"
src="js/validation_functions.js"></script>
<title><?php h($title) ?></title>
</head>
<body>
<div id="banner">St. Tom's Ambulance Ball</div>
<!-- START[container] -->
<!-- "body" -->
<div id="container">
<!-- START[header] -->
<div id="header">
<!-- header -->
<div id="header_text"><?php h($header_1) ?></div>
<div id="header_cell2"><?php h($header_2) ?></div>
<div id="header_cell3"><?php h($header_3) ?></div>
</div>
<!-- END[header -->
<!-- START[content] -->
<!-- "other container" -->
<div id="content">
<!-- START[form] -->
<div id="form">
<?php
require_once(realpath($view_1));
?>
</div>
<!-- END[form] -->
<!-- START[data] -->
<!-- "main content" -->
<div id="data">
<?php
require_once(realpath($view_2));
?>
</div>
<!-- END[data] -->
<!-- START[side] -->
<div id="side">
<?php
require_once(realpath($view_3));
?>
</div>
<!-- END[side] -->
</div>
<!-- END[content] -->
<!-- START[footer] -->
<div id="footer">
<!-- footer -->
<div id="footer_text">
<ul>
<li>home</li>
<li>partners</li>
<li>projects</li>
<li>contact us</li>
</ul>
</div>
<div id="footer_cell2"> </div>
<div id="footer_cell3"> </div>
</div>
<!-- END[footer] -->
</div>
<!-- END[container] -->
</body>
</html>
Note: h() is a function that first of all removes all undesired entity tags before echoing a string.
On a related note, at the top of each page I have some controller files which are included with require_once. I was wondering if it would be possible to implement a function that simply includes files based on a specific input string (name of the functionality/page) i.e "index" in this way:
function include_controller($page){
switch($page){
case "index":
require_once(realpath($config["directories"]["controllers"]."/index_.php"));
break;
case "checkout":
require_once(realpath($config["directories"]["controllers"]."/checkout_.php"));
break;
default:
break;
}
}
Instead of hard coding the includes into each file, you could have a controller file in which you pass the page to be displayed through a $_GET variable. The controller then handles the logic and includes the appropriate page or pages. This is the way a lot of MVC frameworks do it.
Edit: To answer your second question, instead of using a switch, you could just check to make sure the file exists. If it does, include that file, otherwise output an error ("Page doesn't exists" or something similar).
function include_controller($page){
if (file_exists($config["directories"]["controllers"]."/$page_.php")) {
// page exists, include it
require_once($config["directories"]["controllers"]."/$page_.php"));
} else {
// page not found
}
}
Obviously you should probably make the function a little more robust and probably limit the files that will be included to a certain directory or something. Also make sure you properly filter the $page variable so users aren't able to access any file.
Keep this one file as your template file. Then for all the functionality in your site always hit this file. Lets sat this file is index.php. So all functionality requests go to index.php. But with different parameters so for functionality A.
index.php?function=a
For functionality b
index.php?function=b
you can add more parameters also.
Now on the basis of a,b and the set of parameters see what files you want to include as require once.
Like the others already said, it would be better to use some kind of MVC framework. Or at least use a template engine (e.g. Smarty). Your example is ok though, for the 90ies :)
You can get by with one template if you choose a different way of specifying what page is being requested, such as using a GET variable. You can load the pages in a database and specify each of the included pieces, then have one php 'template engine' that loads the requested page from the database and outputs the template with the right includes.
If your server supports it, you can references to things you want to include on all pages in .htaccess:
php_value auto_prepend_file "header.php"
php_value auto_append_file "footer.php"
(Found this on codingforums.com)

Categories