Hello I am trying to include a custom field from wordpress inside of a php include.
Heres what the custom field call looks like:
<?php the_sub_field('icon_number'); ?>
What I want to do, is place this code inside of a php include path.
So heres what it would look like manually:
<?php include("svgs/icon-1.php"); ?>
So combined I have:
<?php include ('svgs/icon-' . the_sub_field('icon_number') . '.php'); ?>
Some reason it is just spitting out the number instead of the include path.
Heres my second attempt using a variable:
<?php
$iconNumber = the_sub_field('icon_number');
include ('svgs/icon-' . $iconNumber . '.php');
?>
Still same output, just the numbers.
Did I mess something up?
-Joe
You want get_sub_field not the_sub_field. Generally speaking, the get_ functions will return a value for you to use, and the_ functions will forcefully echo the value that's returned.
Related
I'm creating a website with multiple pages, each one has a different background but the tag is inside header.php to simplify my code.
I wanted to assign a class to the tag (example ) in order to change the background of each page in css.
I tried assigning the class using php with an if because on index.php the header goes without a class (In order to simplify my css so i just have to change the background and not all the other stuff). I'm using the latest version of XAMPP and PHPStorm with PHP 7.3
In the actual file the code would say <?php include "header.php"; ?>
the filename i want is the name of the actual file where header.php is being included.
The code i tried but didn't work was
<header class="
<?php if(basename(__FILE__, '.php') == 'index')
{
echo ' ';
}
else {
echo basename(__FILE__, '.php');
}
?>">
The results i'm hoping for is that if the header is being used in example.php the class goes like class="example".
Right now the class is class="header" and i want it to be named after the page where header.php is included
Assign __FILE__ to a variable first (not in header.php) and change your test to use that variable.
a.php:
$pagename = basename(__FILE__, '.php'); // 'a'
header.php:
if ( $pagename == 'a' )
That said, you might not want to use your PHP file structure to decide on the layout, but try something like this instead: https://css-tricks.com/id-your-body-for-greater-css-control-and-specificity/
Don't use .php extension just use URL base Name of each page.
OR
you can make it using add check on page name like your page is index just
<div class="<?php $pagename == index ? 'classname' : 'nothing or you can define some class here as well or leave it blank'>"
Thanks
I'm just curious how I can include a PHP file that doesn't have a static name like home.php, or contacts.php?
I want to have it so when I click on a link it includes the name of the page I was sent to.
So, I click on "Contacts", instead of my code being:
<?php include "contacts.php"; ?>
It's:
<?php include "somekindofcodetohavethisdynamic.php"; ?>
Is there a way to do this?
There should be some logic that returns the dynamic stuff, right? Put that into a function:
function getDynamicName($page) {
// Magic happens here.
}
And, get it stored in some variable. And include it in the normal way.
$contactPage = getDynamicName("contact");
include "{$contactPage}.php";
If you are very lazy, you can do this as well:
include getDynamicName("contact") . ".php";
I want to use include straight in define in php
for e.g.
<?php define('panel' , 'include "../panel/admin.php";'); //in a saperate php page ?>
and include this file
Now I want use
<?php panel ?> //in another page
you cant use include() in define
you must do this
<?php define("panel" , "../panel/admin.php"); ?>
and then include it
<?php
include(panel);
?>
mohade's answer is correct.
However you can do the same with your existing code by below way.
define('panel' , 'include "/panel/admin.php";');
eval(panel); // eval — Evaluate a string as PHP code
Hope it will help you :)
Im having a little problem and cant get it to resolved for some reason. I have built a website and got it running but i have now passed some of the files over to a folder to build more themes. What i am doing is trying to change themes back end and for this instance i will use the theme "smooth"
this is what i have, if i put this:
<?php
include "config_inc.php";
?>
include "themes/<?=$aset['Theme']?>/index.php]";
it will display on the page as the correct path "themes/smooth/index.php" so i know it is connecting to the database correctly. But now if i put it in this format:
<?php
include "config_inc.php";
?>
<?php
include "themes/<?=$aset['Theme']?>/index.php]";
?>
It just shows a blank page.
I have tested the link as in "my_domain/themes/smooth/index.php" and every is displaying correctly
Hope someone can help. Thanks
You shouldn't use php tags inside php tags to insert variable in string. Just use {} to do it:
include "themes/{$aset['Theme']}/index.php";
Or
include 'themes/' . $aset['Theme'] . '/index.php';
I want to do this, but it gives error :( for better understanding my problem I'm giving an example:
<?php
include 'script.php?text=hiii';
?>
content of the script.php
<?php
echo $_GET['text'];
?>
So, how can i pass an argument while including the script page?
You could set $_GET['text'] before including the file:
$_GET['text'] = 'hiii';
include 'script.php';
But this obviously won’t affect other variables like $_SERVER['REQUEST_URI'], $_SERVER['QUERY_STRING'] etc.
After you include any script, the included script will act as it's in the same page.
For yourpage.php?text=hiii, that include('script.php') will automatically print hiii, as content of script.php will be in your included page.
You could've done something like this:
<?php
$_GET['text'] = 'what you want to do';
include('script.php');
?>
Actually we don't need to add it to $_GET. just create a variable and use it. Example:
script.php
<title><?php $text; ?></title>
<!--- other code goes here -->
index.php
<?php
$text = 'Welcome back';
include 'script.php';
?>