So I have header.php file with my header and page1.php, page2.php, page3.php with my content. Every page starts from
<?php include_once("header.php") ?>
But title is always static in my code. I tried to add <title><?php $title_value ?></title> to my header.php, and
<?php $title="Contacts"; include_once("header.php") ?>
to my page1.php but title is invisible. It seems like $title didn't exist. How can I fix it and add different titles to my pages?
<?php $title_value ?> does nothing. You need to echo/print the value in the header.php.
<title><?php echo $title_value; ?></title>
In header.php you also should include a check incase you forget a title in a file.
$title_value = empty($title_value) ? 'Default Title' : $title_value;
You only declared the variable.
You didnt print or echo it.
Use:
<?= $title; ?>
Or:
<?php echo $title; ?>
Or:
<?php print $title; ?>
first you have to echo the value of the title like that:
<?php echo($my_var) ;?>
// or
<?=$my_var?>
after to change the title of your page u can pass a $_GET param to your html title value ( not the better way but the easier )
ex:
// on the fist page
<a href="/myurl?title=page2">
// on the second page
<?php echo($_GET['title']) ;?>
Related
I am making static php and i want somthing like this (i am a php beginner :):
From this:
<?php $titleid="example title"; ?>
To this:
<?php $title="<?php echo $titleid; ?>"; ?>
To get this:
<h1><?php echo $title; ?></h1>
And then, the expected output is:
<h1>example title</h1>
I use this cause the variable $titleid is in other php file.
In this line
<?php $title="<?php echo $titleid; ?>"; ?>
You should not use the echo, since at that time you don't want any output. Also avoid the double <?php ?> brackets.
Just write that line as
<?php $title=$titleid; ?>
First, you open <?php tag only once in file. if you want get example title in your pages, you define this content one variable and then get result with echo :
Ex
<?php
$title = "<h1>example title</h1>";
echo $title;
The below code will not display any output as the variable is declared below the echo as PHP gets executed line by line. Is there any way to search for the variable in the whole page and then execute the code?
<?php
include "header.php";
$title = "Test";
?>
header.php
<html>
<head>
<title><? echo $title ?></title>
</head>
You need to learn how compilers/interpreters works. PHP is interpreted language and The binary that lets you interpret PHP is compiled.
PHP run from top to bottom.
so its like
<?php // start from here
echo "$title"; <-- $title is undefined here
$title = "Test"; <-- now you declared $title with value so it goes in memory now
//end
So you need to first check weather $title is set or not than respond according to it
if(isset($title)){
echo $title;
}
According to your logic, I suggest you to use contants like below:
Create a separate file, let's say constant.php and include it on all other pages
<?
define("TITLE", "This is title");
?>
Use it like below:
<?php echo TITLE;?>
Thanks
I'm displaying a gallery on my wordpress site using the following code:
<?php echo do_shortcode('[satellite gallery=2 auto=off caption=off thumbs=on]'); ?>
Now I've added a custom post value/field called 'guitLink' where I can add the gallery number which I want to then use in the above shortcode, so something like this:
<?php echo do_shortcode('[satellite gallery=' .get_post_custom_values('guitLink'). 'auto=off caption=off thumbs=on]'); ?>
Any ideas how to achieve this?
Use:
<?php $guitLink = get_post_custom_values('guitLink');
echo do_shortcode('[satellite gallery=' .$guitLink[0]. ' auto=off caption=off thumbs=on]'); ?>
Instead of
<?php echo do_shortcode('[satellite gallery=' .get_post_custom_values('guitLink'). ' auto=off caption=off thumbs=on]'); ?>
I have a file named page.php and it is something like this;
<? include "header.php"; ?>
<body>
...
</body>
<? include "header.php"; ?>
Between the <body> tags, I run a sql query and get a variable called as $title.
I want to show that variable as the html title of my page which is in the header.php .
How can I do this?
I simply tried what i said, but it didn't work.
Run it before the <body> tags, save the results to a variable, and process this variable wherever you want (as a title and in the <body> tags).
You should try to avoid mixing PHP code with your HTML. Run the SQL query before you include header.php, store the results in an variable, and store the title in a variable. For example:
<?php
// SQL code here, store the data in e.g. $data and title in $title
include "header.php";
?>
<body>
<?php
// output page using $data
?>
</body>
<?php include "header.php"; ?>
Then your header.php would include something like <title><?php echo $title; ?></title>
FIXED!
I have 2 php pages that both define the variable "title" as something different. However, echoing the variable on both pages results in the first page's variable's value being displayed on both pages. Any idea why and how I could get the variable to change for each page?
first php page:
<?php
$title = "Posts";
echo $title;
?>
This displays "Posts".
second php page:
<?php
$title = "New Posts";
echo $title;
?>
This also for some reason displays "Posts". Shouldn't this page display "New Posts"?
If you are including the second page on the first page before you define $title on the first page, then the included value will be overwritten.
Are all of your variables defined in the global namespace? If so, this problem will be inevitable when you're including PHP files within other PHP files.
You could resolve the problem by properly encapsulating your variables within a class or namespace; for example:
In file one:
<?php
namespace included;
$title = "original title!";
?>
And in file two:
<?php
namespace including;
require_once "file_one.php";
$title = "new title!";
echo \included\$title;
echo \including\$title;
echo $title;
?>
Which will display:
original title!
new title!
new title!