How is get_header() calling a specific PHP file? - php

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;
?>

Related

Get page.php using php code

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.

Show posts in theme development

I am new in Wordpress. I am developing my own theme. I have included index.php, functions.php, header.php, footer.php, page.php, style.css etc.. Now my home page is working perfectly. When I am going to display category menu its showing only header and footer. Not inside the content. When displaying post also its showing Header and footer.
What are the steps included for displaying posts in theme development or is there any function I want to include in function.php ?
You need to use "the loop" to display the content from the page/post.
<?php get_header(); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<article>
<header><h1><?php the_title();?></h1></header>
<?php the_content(); ?>
</article>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>
Copy this code into a new file called single.php in your theme folder.
Best way is to copy the existing theme and change the theme name ( make changes in css , images , header , footer design ) and use that , it will work correctly.
If you are working with child theme.
than all pages display under default template. and default template is page.php.
for category there is a category.php or archive.php file in which you have to change code for more functions.
And for display post you have to change single.php
you can create template for additional functionality.
that's it.
You might be missing content.php, category.php,content-single.php and many more files which in result not displaying your content part and only displaying header and footer.
Best way is download fresh word press from :
https://wordpress.org/download/
It will include all supporting files for load website.copy one of theme from fresh themes folder and replace it with your theme and customise in it.

Wordpress and PHP includes

If I was looking to use my own HTML for a certain page on my wordpress theme, how can I go about it?
Please advise where code should go
If I can use raw HTML or PHP INCLUDES.
If you are looking to place the HTML in the main page content, just use the text editor of WordPress(http://www.wpbeginner.com/glossary/text-editor/). The header & footer would be modified inside the header.php and footer.php files of your theme. the directory where these would be located should be /wp-content/themes/salient/. If you want the header and footer to only be modified only on specific pages, you will want to use the is_page() function built into WordPress(https://codex.wordpress.org/Function_Reference/is_page).
Example of limiting header or footer modification to specific page:
<?php if( is_page( 42 ) !== false ) { ?>
<!-- HTML that should only appear on page with id of 42. -->
<?php } ?>

Inserting custom content block into wordpress

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.

Creating a custom page on wordpress and adding content to it

I want to create a php page on the website (CMS Wordpress), but I do not understand how to incorporate the template files, for example
<?php get_header(); ?>
And other content. I know that it is possible to create pages through the admin interface, but I do not fit... if you try to create a page of template files and then go for it the direct link:
http://local/wp-content/themes/mytheme/mypage.php
with pre-embedding the code
<?php get_header(); ?>
it will fail like:
Fatal error: Call to undefined function get_header()
What should I do?
How i can "say" this CMS that the page is important for me, and it performs some functionality? That is, in fact, get the full url to her and that did not give out errors…
You need to include wp-load.php in your script to use any of WP functions:
<?
require_once('../../../wp-load.php');
get_header(); ?>
try that. xD

Categories