Im trying to see different title for different page
In header.php i check file name to set correct title like this
<?php
$page = basename($_SERVER['PHP_SELF']);
if ($page == 'index.php'): ?>
<title>Home page</title>
<?php elseif($page == 'price.php'): ?>
<title>Price page</title>
<?php elseif($page == 'areas.php'): ?>
<title>Areas page</title>
<?php endif; ?>
Only for <?php elseif($page == 'price.php'): ?> is not working i mean it is not printing the title tag for some reason , file name price.php is correct . If i print echo $page in <?php elseif($page == 'price.php'): ?> it is showing me price.php
What is the problem ?
With match expression introduced in php 8 more effective and elegant way than conditions if-elseif... I hope this simplify your code and solve the problem.
<?php
$page = match(basename($_SERVER['PHP_SELF'])) {
'index.php' => 'Home page',
'price.php' => 'Price page',
'areas.php' => 'Areas page',
default => 'Unknown page'
};
?>
<title><?=$page?></title>
As an alternative syntax, you may use:
<?php
$title = '';
switch (basename($_SERVER['PHP_SELF'])) {
case ('index.php') : $title = 'Home page'; break;
case ('price.php') : $title = 'Price page'; break;
case ('areas.php') : $title = 'Areas page'; break;
}
echo '<title>'.$title.'</title>';
?>
Related
I have my website, for example with this layout:
<?php include 'header.php';?>
<!-- CONTENT -->
<?php include 'footer.php';?>
Now, on each page, for example: home, contact, about us, etc, I have to add a class active to the menu. But since I have just one file with the header, how can I change some HTML in each single page?
Thanks, and sorry if this was asked before, I can't find this anywhere, and I don't know how to search for it.
Say, you 3 pages.
Get them in an array in header.php.
$pages = array();
$pages['home'] = 'Home';
$pages['about-us'] = 'About Us';
$pages['contact'] = 'Contact Us';
Show menu like and get current page parameter:
If the parameter matches your page, show active class.
<ul>
<?php
$current = isset($_GET['page']) ? $_GET['page'] : 'home';
if (! empty($pages)) {
foreach ($pages as $url => $title) {
$active = ($current == $page) ? 'active' : '';
// Observe, the class 'active', main logic is here.
?>
<li><?php echo $title;?></li>
<?php
}
}
?>
</ul>
I'd like to give my title <?php echo get_the_title(); ?> conditional statement. If not HOMEPAGE just display whatever it is = <?php echo get_the_title(); ?>. if HOMEPAGE display THIS IS + <?php echo get_the_title(); ?>
so the syntax should be
<?php
$path = $_SERVER['REQUEST_URI'];
$page = basename($path);
$page = basename($path, '.php');
?>
<?php if($page == 'index')
{echo 'This is'. get_the_title(); }
else
{echo get_the_title();}
?>
the problem is I dont really know in wordpress do we use the same way or something smarter or easier.
OH, FORGOT!
Actually, my index/homepage is not real index, it is the page called "HOMEPAGE "
setting from Reading Settings -> a static page, front page => HOMEPAGE
Thanks for any advice.
place your code in functions.php
function title_edit($title){
if(!is_front_page() || get_post_type() != 'page') return $title;
$title = 'Home: '.$title;
return $title;
}
add_filter('the_title','title_edit');
Problem solved thanks #silentboy
<?php if(is_front_page())
echo 'This is'.get_the_title();
else echo get_the_title(); ?>
I'm using the php code:
<body id="<?php echo str_replace("index.php?","",(basename($_SERVER['REQUEST_URI'],".php"))); ?>">
and
<?php
if(isset($_GET['start'])){
include('includes/start.php');
}else if(isset($_GET['help'])){
include('includes/help.php');
}else{
include('includes/start.php');
}
?>
It works great - cutting index.php?help to "help" and index.php?start to "start" in body ID. But when I enter index.php in body ID is "index" not "start". Is there any way to tell index.php with included start.php to display "start" id body ID?
UPDATE
It should work dynamically - body ID is name of included .php file, something like this code:
<?php
$page = str_replace(array( 'server_name', 'index', '?', '/', '.php'), '', $_SERVER['REQUEST_URI']);
$page = $page ? $page : 'start';
?>
<body id="<?php echo $page ?>">
Am not sure i get you 100% but you can try
$id = $_SERVER['QUERY_STRING'];
$bodyID = $id;
switch ($id) {
case "help" :
include ('includes/help.php');
break;
default :
$bodyID = "start";
include ('includes/start.php');
break;
}
printf("<body id=\"%s\" >", $bodyID);
If you run index.php?help it would include include ('includes/help.php'); and also output
<body id="help" >
It would be best to use this instead:
<?php
$ids = 'start,help,something';
$ids = explode(',',$ids);
$included = 0;
foreach ($ids as $id) {
if (isset($_GET[$id])) {
include('includes/'.$id.'.php');
$included = 1;
break;
}
}
if (!$included) include('includes/'.$ids[0].'.php');
?>
Then:
<body id="<?php echo $id; ?>">
How do you do multiple page titles with on header file? Theres one thing though. For the index page, i've got
error_reporting(0);
if ($_GET["error"]=="404") {
include("forum/styles/art_air/web_template/overall_header.php");
include("include/404");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
} else {
include("forum/styles/art_air/web_template/overall_header.php");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
}
So i would have the header before anything else. So how would i manage to make so that
index?error=404 and index have different titles? Thanks in advance.
In overall_header.php
<?php
$title = "Hello, wolrd!";
if ( $_GET["error"] == "404" ) {
$title = "Error";
}
?>
<title><?php echo $title; ?></title>
Use JavaScript and document.title.
Example:
<script language="javascript">document.title = "My Title"</script>
JS can be used in body.
Another method is to set a $GLOBAL variable before including everything.
Example:
error_reporting(0);
$GLOBALS['404'] = 0;
if ($_GET["error"]=="404") {
$GLOBALS['404'] = 1;
include("forum/styles/art_air/web_template/overall_header.php");
include("include/404");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
} else {
$GLOBALS['404'] = 0;
include("forum/styles/art_air/web_template/overall_header.php");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
}
In your overall_header.php:
if($GLOBALS['404'] == 1) echo '<title>404: Not Found</title>';
else echo '<title>My Title</title>';
You could try a switch
<?php
$page = $_SERVER['PHP_SELF'];
switch($page) {
case "index.php":
echo "<title>My Homepage</title>";
break;
case "apples.php":
echo "<title>The Best Apples!</title>";
break;
case "bananas.php":
echo "<title>We Sell Bananas</title>";
break;
}
?>
How can I write the following statement in PHP:
If body ID = "home" then insert some html, e.g.
<h1>I am home!</h1>
Otherwise, insert this html:
<p>I'm not home.</p>
Doing it with native PHP templating:
<?php if ($bodyID==='home') { ?>
<h1>I am home!</h1>
<?php } else { ?>
<p>I'm not home!</p>
<?php } ?>
You can try using this :
$html = '';
if ( $body_id === 'home' )
{
$html .= '<h1>I am home!</h1>';
}
else
{
$html .= '<p>I\'m not home.</p>';
}
echo $html;
This will echo the html code depending on the $body_id variable and what it contains.
You can use a switch command like so:
switch($body)
{
case 'home': //$body == 'home' ?
echo '<h1>I am home!</h1>';
break;
case 'not_home':
default:
echo '<p>I'm not home.</p>';
break;
}
The default means that if $body does not match any case values, then that will be used, the default is optional.
Another way is as you say, if/else statements, but if within template / view pages you should try and use like so:
<?php if ($body == 'home'):?>
<h1>I am home!</h1>
<?php else:?>
<p>I'm not home!</p>
<?php endif; ?>
Assuming $bodyID is a variable:
<?php
if ($bodyID==='home') {
echo "<h1>I am home!</h1>";}
else {
echo "<p>I'm not home!</p>";}
?>
Personally I think that the best way to do that without refreshing and without having to set a variable (like $body or something like that) is to use a javascript code, this because "communications" between JS & PHP is a one-way communication.
<script language="javascript">
<!--
if( document.body.id === "home" ){
window.document.write("<h1>I am home!</h1>") ;
}
else{
window.document.write("<p>I'm not home!</p>") ;
}
-->
</script>
otherwise you can build a form and then take the body.id value using $_GET function... It always depends on what you've to do after you now body.id value.
Hope this will be usefull & clear.
you can try in the following way:
$body_id = "home";
if ($body_id == "home") {
echo "I am home!";
} else {
echo "I am not home!";
}
or
$body_id = "home";
if (strcmp($body_id, "home") !== 0) {
echo 'I am not home!';
}
else {
echo 'I am home!';
}
Reference:
https://www.geeksforgeeks.org/string-comparison-using-vs-strcmp-in-php/