I've been looking around for a while on how to do this but I don't think I've been wording my searches correctly. I'm developing a custom theme and want to break out a section of a page into a reusable piece that I can reference like you would insert a header with <?php get_header(); ?> but I want to use a different header file instead of header.php. How would I do this?
You can use the function like this:
<?php get_header( $name ); ?>
$name :: (string) (optional) Calls for header-name.php.
if you have a seperate header for home page you can create a header file like this "header-home.php" to call that header file use the get_header function like this:
<?php get_header( 'home' ); ?>
Also you can call multipe header in a theme like this according to different conditions
<?php
if ( is_home() ) :
get_header( 'home' );
elseif ( is_404() ) :
get_header( '404' );
else :
get_header();
endif;
?>
You can specify which header file you want to use by naming it, as Ajith R Nair said in his answer. <?php get_header('new');?> would load the file header-new.php in your theme's directory. If you plan on changing small parts of your header, I suggest not using duplicate header files, but instead define a global variable that holds an array of default parameters in the header file, and just use that same global variable in your theme pages to override defaults. Hopefully that points you in the right direction, though I would be happy to clarify if needed.
Related
I'm trying to learn Wordpress Structure with investigating some free plugins/themes.
I'm working on "Renger Blog Theme" right now, but I couldn't understand something. I've checked get_header() page in the WP manual but it still looks like a magic :)
This theme has custom function codes in
wordpress\wp-content\themes\renderblog\inc\renderoption.php
and this theme calling this file with just
get_header();
in index.php
There is no include code in header.php or wherever else.
How it's calling this specific PHP file with get_header()? Is it like a way to include automatically all files in the inc folder?
When I just delete get_header() from the index.php, the functions are not working.
WordPress get_header() is the function predefined by WordPress inbuilt structure. This function Includes the header template for a theme or if a name is specified then a specialized header will be included.
if the file is called "header-new.php" then specify "new".
For example <?php get_header('new'); ?>
Another example for Different headers for different pages.
<?php
// if detect home page of wordpress
if ( is_home() ) :
get_header( 'home' );
// if detect Not found page 404 of wordpress
elseif ( is_404() ) :
get_header( '404' );
// default header if nothing specific is detected
else :
get_header();
endif;
?>
So, my parent-theme uses a single.php calling a content-single.php with the function get_template_part().
The code for single.php:
get_header(); ?>
<div class="container">
<main id="main" class="postItem" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?>
<?php get_template_part( 'content', 'single' ); ?>
<?php codex_coder_content_nav( 'nav-below' ); ?>
<?php endwhile; // end of the loop. ?>
</main><!-- #main -->
<!--<?php get_sidebar(); ?>
I am using a custom post_type named "ourNews". Following wordpress documents, I created two files: single-ourNews.php and content-single-ourNews.php
With this, in my "single-ourNews.php" I changed the following line:
<?php get_template_part( 'content', 'single-ourNews' ); ?>
But it keeps loading the file "content-single.php". What am I doing wrong?
Other question is: How can I put a image with relative path on this custom template? I created a folder "img" and I was calling using:
But says that the image was not found. I read a little and some places said that I could not use relative path, but why not, if the theme uses? I'm confused.
Wordpress is still loading single.php that loads content-single.php
Your custom post is not ourNews. This is probably the label but not the slug. The slug is always lowercase and separated by a -. Try renaming your file to single-our-news.php.
Also, every time you register a custom post type, you should reset your permalinks options back to default and then back to what you want it to be.
For the relative path, you should not use it. You should use .'/img/name-of-image.jpg
When using get_template_part, the first parameter defines the base template name (e.g. content, which loads content.php) and the second parameter loads a suffixed version if available. Therefore your call
get_template_part('content', 'single-ourNews');
is looking for a file named content-single-ourNews.php first, then falls back to content.php in case the first one is not available. I'm not sure whether the get_template_part function converts the suffix parameter to something like single-ournews or single-our-news before appending it to the first parameter, be sure to test a few variants of that.
I'm not 100% sure if the function behaves differently or not in child themes and parent themes. One option is to override the parent's single.php in the child theme and modify it directly with
if ($post->post-type === 'ourNews') {
get_template_part('content', 'single-ourNews');
}
else {
get_template_part('content');
}
Lastly, WordPress will look for a file single-[cptslug].php before loading a template for a custom post type's single view.
I would like to change the text (illustrated on the image below) from 'Portfolio Archives' to 'Shop'.
I have archive.php and portfolio.php files, none of which has any obvious indication of how/where to change the text.
screen shot http://damtech.it/screen.png
Your image link is broken...
But the title usually is in your header.php file.
I also come across this problem previous week.I was using a theme.This may help you or may not.But I face same problem ,so I am answering.
If you are using some theme.Then there should be your Theme Setting.
Go to theme setting and there will be Header and footer options.You may change this text from there.My problem was solved from there.
For anyone else who wasted time trying the approved answer, reading the function reference at https://codex.wordpress.org/Function_Reference/get_header and paying attention to this section:
Multiple Headers
Different header for different pages.
<?php
if ( is_home() ) :
get_header( 'home' );
elseif ( is_404() ) :
get_header( '404' );
else :
get_header();
endif;
?>
The file names for the home and 404 headers should be header-home.php and header-404.php respectively.
leads to the following successful approach.
Copy your (yourthemename)/header.php file to (yourthemename)/header-customname.php
Change the get_header() call in the file you are targeting to get_header('customname')
Job done.
I am curious as to how to utilize conditional comments to define pages/files with PHP.
This technique is used in WordPress; one can define the template file by inserting conditional comments at the top of the file. Then when you select this template, the application knows which file to use. Something like this;
<?php
/*
Template Name: Snarfer
*/
?>
I would like to point out that I do not wish to perform this actin in a WordPress environment, but I am curious as to how they do it.
So how can one use this technique in PHP? I would assume one needs to create a function or a class or a series of functions and classes.
I have tried searching but perhaps I'm using the wrong keywords in my search.
If anybody could point me to documentation or tutorials i would be very appreciative.
I think you must have to give the condition on wordpress archive.php to check which custom post type you like to use and you can define here to which templete file you like to call.
<?php if ( 'news' == get_post_type() ) { ?>
<div class="container">
<?php
/* Include the Post-Format-specific template for the content.
* If you want to overload 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( 'news-blog', get_post_format() );
?>
</div>
<?php } else { ?>
Wordpress Default call
<?php } ?>
in get_template_part( 'news-blog', get_post_format() ); having news-blog is the file name of you template. for example news-blog.php
Is it possible to create a tag.php template that when you navigate to the url www.domain.com/tag/architecture/ it will display all the custom posts that have been tagged with that specific tag? And so on for various other tags?
What code will I need to include on my template?
Yes You can create,below is the code i used to display custom post type "knowledge"
<?php
global $query_string;
$posts = query_posts($query_string.'&post_type=knowledge');
if ( have_posts() ) while ( have_posts() ) : the_post();
?>
<?php the_title(); ?>
<?php endwhile; // end of the loop. ?>
This will help you understand hierarchy of templates:
http://codex.wordpress.org/images/1/18/Template_Hierarchy.png
Use of $query_string (example) available here:
https://developer.wordpress.org/reference/functions/query_posts/
According to the template hierarchy, you can create a file called tag.php which will be used instead of index.php if a tag page is displayed. You can also prepare separate templates for specific tags.
The best way is to start by creating a copy of your theme's index.php and calling it tag.php, this way you'll have some basic code working. Then you can modify tag.php to fit your needs - probably by editing The Loop, or maybe changing some includes if your theme loads the loop from separate file. (but in this case you should consider editing index.php to load some other loop for tag pages - is_tag() may come in handy)