How can I include a file from a child theme - php

I have a file in my child theme root directory named image-svg.php
The in another file under childthemename/page-templates/filexample.php
I want to include image-svg.php
I tried different ways but the file does no get included:
<?php get_template_part('africa-map-svg.php'); ?>
<?php include('africa-map-svg.php'); ?>
How can I include this file ?

You can use get_template_directory to retrieve the directory of the current theme.
<?php require get_template_directory().'/africa-map-svg.php'; ?>

You can use following function to include any file. get_theme_file_uri()
please try this
<?php require_once (get_theme_file_uri('/africa-map-svg.php')); ?>

Related

PHP How to exclude a file which is included in the file I am including?

Suppose I want to include header.php file and header.php already contains another file included functions.php. If in my index.php I am including header.php then naturally functions.php will also get included. But what if for some specific reason I want to break the joint and exclude functions.php while including header.php? Is this possible?
Since after a lot of searching I could not find a native PHP function the workaround I came up with is:
if(basename($_SERVER['PHP_SELF']) != 'page-name.php'){
include('functions.php');
}
With this code functions.php will be included in all pages from header.php except only on page-name.php
You can just change for:
include_once 'functions.php';
so it only will include once even if Header.php and Index.php has it.

Can't include php file from parent directory

I have two side-menus on my Wordpress site that I have saved as php files. I am able to call these into my page.php file with the the following line of PHP <?php include("mobile-menu.php"); ?>
For some reason, I am unable to do the same in another of my page templates in a sub-folder. My template structure is fairly simple, in my main theme folder I have header.php, footer.php, mobile-menu.php etc. Then in a sub folder called "page-templates", I have another template called full-page.php. This is the template in which I am having trouble calling mobile-menu.php with the include function.
I have tried the following:
<?php include("../mobile-menu.php"); ?>
<?php include(__DIR__."../mobile-menu.php"); ?>

Add php page in wordpress outside the theme

I have added a php page (test.php) in my root directory (WWW) where wordpress is installed. I don't want to use the custom template method (add this page in my wordpress theme directory)
I have added in this php file this:
<?php
require('wp-blog-header.php');
get_header();
?>
<h1>Test</h1>
<?php
get_footer();
?>
When I do: www.example.com/test.php this page is correctly loading but the title of this page display "error 404"
I don't understand how can I resolve this problem.
you need to include wp-load.php to use header, in your case replace require('wp-blog-header.php'); with
<?php require_once('wp-load.php'); ?>
I have placed this code in my root directory in test.php file. This code is working fine if you replace require('wp-blog-header.php'); with require_once('wp-load.php');

PHP require_once directories issue

I have a website with the following directory structure:
index.php
connections/database.php
contacts/details.php
inc/functions.php
I would like the database.php page to use require_once to include the functions.php page e.g:
require_once 'inc/functions.php';
I would like to then use require_once for the both the index.php page and the details.php page to just require the database.php page which in turn will also include the functions.php page. I can get this working for either the index.php page or the details.php page but not both as I have to keep changing the syntax, e.g.
require_once 'inc/functions.php';
require_once '../inc/functions.php';
Otherwise I get errors that the functions.php page cannot be found. Is there a way to include the database.php page which also includes the functions.php page that will work from the different directories for both the index.php page and the contacts/details.php page?
thanks
Your best bet is to set a $basepath variable in your config file pointing to the absolute root path of your app. Then you can go with require_once $baseapth."/inc/functions.php"
You can use dirname(__FILE__)!

I cannot display the header region in my Drupal theme

I am not seeing the content I have for the header region in my drupal theme is it best practice to use the following code in the page.tpl.php?
<?php include('region--header.tpl.php'); ?>
I see the content when I use the code above but I cannot see it when I use the code below:
<?php if ($page['header']): ?>
<?php print render($page['header']); ?>
<?php endif; ?>
In my .info file I have the following code:
regions[header] = Header
Copy your base region.tpl.php file to the theme's templates folder.
region--header.tpl.php file and region.tpl.php file should be placed in the templates folder.

Categories