Is there an easy way to change the WP default template hierarchy?
For instance;
Say I want to change my theme directory structure so that it completely changes from the Template Hierarchy suggested here based upon conditionals:
http://codex.wordpress.org/Template_Hierarchy
if I wanted to make sure that for all page types (is_single() is_home() etc) it always opens one template file which then instigates my own pattern to provide the output?
Thanks very much!
I would do it like this:
Easy to read and works well
single.php:
<?php include_once('template-you-want.php');
home.php:
<?php include_once('template-you-want.php');
If you don't want to have these two files at all do it in index.php:
<?php
// at the very top
if (is_single()){
include_once('template-you-want.php');
die(); // don't continue
}
if (is_home()){
include_once('template-you-want.php');
die(); // don't continue
}
Related
I'm trying to create a second single.php for my WordPress theme.
The first single is used with get_permalink() but it's impossible to create another single2.php
How is this feasible?
I'd like to use the single2.php the same way as the regular single.php
I've seen some tutorials but every time they associate the single2.php with the creation of WP categories and I know it's doable without it. I just don't know how.
Open single.php in your HTML editor. Delete the contents of single.php and place the following code in it. Substitute the number 3 for the Category ID # you noted.
<?php
$post = $wp_query->post;
if ( in_category('3') ) {
include(TEMPLATEPATH . '/single2.php');
} else {
include(TEMPLATEPATH . '/single1.php');
}
?>
You should have a look at the Wordpress Template Hierarchy, it could give you tips to do that :
You could create a page-{slug}.php or a page-{id}.php file for example, but it's not the nicest way as it's only for a specific slug or specific id.
I'd recommend to create a template file, where you can create your page. This template can be reuse for several pages !
I am working with wordpress.
I see that in my index.php there is a code called <?php get_footer(); ?> ..and I get it, it's simple. This code will pull footer.php.
It seems to me that the get_footer() is built in to wordpress that pulls anything that is named footer.php.
I have created my own page called page.php.
I want to be able to 'get' this page and show in my php code enabled 'sidebar widget'.
I have tried to paste this code, and I am more that certain that its wrong:
<?php
echo file_get_contents("side.php");
?>
What code would I need if I want my custom page called page.php to be shown?
The WordPress way to load a template file is get_template_part():
get_template_part( 'page' );
// loads page.php from the theme directory.
Note that page.php is a special filename in WordPress themes, this file is loaded as a template if a page is displayed. You should give your file a different name if you want to prevent this.
Details are in the already mentioned template-hierarchy.png
Edit:
After rereading your question: If your page.php is not from a template, but in a folder of a plugin you are developing as a sidebar widget, the also already mentioned include('page.php'); would be the way to go.
page.php is a special page of wordpress. See this diagram.
https://developer.wordpress.org/files/2014/10/template-hierarchy.png.
The wordpress way is to create a own template.
https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/
try the following
include ('side.php');
OR
require ('side.php');
you may also use include_once / require_once
Please use require_once or include in php file like below.
require_once('side.php');
or
include ('side.php');
try this,
require( dirname( __FILE__ ) . '/page.php' );
if your page is in same level where is your parent page.
Add this code in your php page
<?php include_once('filename.php'); ?>
There are so many options that you can chose:
You can use get_template_part():
From the Docs: WordPress now offers a function, get_template_part(),
that is part of the native API and is used specifically for reusing
sections - or templates - of code (except for the header, footer, and
sidebar) through your theme.
Other solution is require_once() or include_once().
But if i compare both function include_once() is faster than to require_once() for smaller application.
For better understanding about the require and include you can read this Question.
Goal - have a section of my sidebar that is conditional depending on what page I'm on. All child pages of the selected page should also show the conditional section.
There are at least two ways to do this - 1) have all the conditional hooks and resulting code in one sidebar file and, 2) have multiple sidebar.php documents and call them based off what pages are being visited.
I've pretty much failed at both ways so far... :( I'm working on the second method right now though, because I think it might be more flexible long term. Anyway, in my page.php I replaced:
<?php get_sidebar() ?>
With
<?php
if (is_page(1997) || $post->post_parent) {
get_sidebar('sidebar-test-may12.php')
}
else { get_sidebar()
} ?>
With the hope of testing the method. What I want to happen is that if the page is page id 1997 or any of its child pages please show sidebar-test-may12.php. Otherwise show the regular sidebar. With the new code the whole page goes blank. Any suggestions or solutions for me? Seems like a common problem, but I haven't been able to figure it out yet. Thanks!
You have a few problems with your code example:
1) $post->post_parent is not being checked against anything else, so it will only return true if it is a child page (not necessarily the child of the page you want to target)
2) get_sidebar() is being called incorrectly. If you want to get 'sidebar-test-may12.php' from your theme folder, you need to call get_sidebar('test-may12')
3) You're missing semicolons after your function calls
So your code should look like this:
<?php
if(is_page(1997) || $post->post_parent == 1997) {
get_sidebar('test-may12'); //get sidebar-test-may12.php
}
else{
get_sidebar(); //get sidebar.php
}
?>
Let me know if that helps.
UPDATE: Bear in mind, $post->post_parent does NOT get the top-most ancestor ID of a child page. If you want to grab the top-level ID regardless of depth, consider doing this:
<?php
$ancestors = get_ancestors(get_the_ID(), 'page');
if(is_page(1997) || end($ancestors) == 1997)
get_sidebar('test-may12'); //get sidebar-test-may12.php
else
get_sidebar(); //get sidebar.php
?>
POSSIBLE SOLUTION: Building off your example and my proposed ancestry check, one thing you can do is have your template check to see if a special sidebar exists in your theme based on the parent page's slug. This way, if you decide a particular page needs a special sidebar for it and all of its children/grandchildren/great-grandchildren/etc. you just need to add it into your theme with a name of 'sidebar-{parent_slug}.php'. So:
<?php
$id = get_the_ID();
$ancestors = get_ancestors($id, 'page');
$top_page = $ancestors ? get_page(end($ancestors)) : get_page($id);
if(locate_template('sidebar-'.$top_page->post_name.'.php'))
get_sidebar($top_page->post_name);
else
get_sidebar();
?>
This way, you don't need a ton of conditionals to decide which Sidebar file to load on a general Page template.
Use some error trapping to show what's going on with your php, either toggling an error log in .htaccess of a simple widget: http://wordpress.org/extend/plugins/error-log-dashboard-widget/
I've hardcoded logic for sidebars, but I've also used WordPress › Widget Logic « WordPress Plugins for my own and client sites. It's easy to use with a simplified version of WP conditionals, like is_category and is_page ('101')
For someone else tackling this same problem -- there is a new plugin out that solves this problem very well. It even makes creating the sidebars easy. You can check it out here:
http://wordpress.org/extend/plugins/custom-sidebars/
I need to reference a particular functions.php file, that can be stored in the template directory of my Joomla template. WordPress seems to include this, but if I do something like:
Include the functions file
<?php require_once ( 'functions.php' ); ?>
In the functions.php:
<?php
function displaySomething() {
echo "Hello World";
}
?>
and then in the templates index.php:
<?php displaySomething(); ?>
The template breaks/is blank. For aguements sake, I just want to have a single referencable php case function.
Try this -
<?php require_once ($this->baseurl.'/templates/'.$this->template.'/functions.php'); ?>
However, chances are that you shouldn't do it this way. In general it's not a good idea to hard code anything in to your template because that removes the ability to select the pages that you want the code to be executed on and it removes the ability to easily edit what ever the code is doing.
You really should put your code in a module or plugin. Putting it in a module makes it easy to select the pages you want the code on, turn it on or off globally, or to change the output easily. Putting it in a plugin would make it easy to put it on pages based on one of the Joomla event triggers, and to turn it on or off globally.
For the amount of time it would take to turn it in to an extension, it is well worth it.
Try with (using the constant JPATH_BASE):
<?php
#Loading functions
require_once (
JPATH_BASE."/templates/{$this->template}/functions.php"
);
?>
I want to use the if statement to select specific pages that have <body class="section-category"> and output custom content for only those pages.
My PHP is not very good and I hope this is a very simple task to do. Can anyone give me a tip?
I would not check for the existence of a certain body class. Those classes are only the results of other if-else logic and they can easily be overridden or altered, breaking your page template. It's better to check the values those classes are based upon. If I were you, I would try to figure out how this body class was generated and re-use that code.
For example, if your theme's template.php does something like this:
$body_classes[] = 'section-' . form_clean_id(arg(0));
Then I would put this in my template.php:
<?php if (form_clean_id(arg(0)) == 'category'): ?>
// Do fancy stuff!
<?php endif; ?>
It sounds like you want a flag available for certain pages; one that you can read out and act upon in your theme.
In template.php:
function YOURTHEME_preprocess_page(&$vars) { //Many themes already have this function implemented
if (stripos($vars['foo-bar'], 'section')) {
$vars['is_section'] = TRUE;
}
else {
$vars['is_section'] = FALSE;
}
}
Then in your page.tpl.php
<?php if ($is_section): ?>
<p>I am in a section</p>
<?php endif; ?>
That way you keep logic where it belongs: in the preprocessors. And you keep logic out of the template, where it most certainly does not belong!
The if (stripos($vars['foo-bar'], 'section')) { can most probably be made a lot smarter, if you do a var_dump($vars) there, all available variables will dump on your screen (browser). I am certain you will find a variable that you can check against, and that is less 'fuzzy' then a body class. After all: that class is meant for one thing only: to serve as class in the body-tag. And not for checking if you are in some section.
You have the full Drupal available there too, so you can even use any function that (e,g, the section) modules provide.
I would look into a module like Context or Panels to do this- it will serve you better in the long run than coding it in.