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(); ?>
Related
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>';
?>
Im using the following code to echo content in wordpress based on the size of its title.
<?php
$title = the_title('','',false);
if(strlen($title) > 35):
echo content(20);
else:
echo content(45);
endif;
?>
Is there a simple way of projecting a media query before this to echo an output based on the window width so basically for mobiles and devices
As per a reply i still cant get this to work using:
<?php
if ( wp_is_mobile() ) {
echo content (20);
} else {
$title = the_title('','',false);
if(strlen($title) > 35):
echo content(20);
else:
echo content(45);
endif;
}
?>
Even simplifying the code to following doesn't seem to work:
<?php
if ( wp_is_mobile() ) {
echo content(20);
} else {
echo content(45);
}
?>
and simply uses the "else" value: echo content(45) on mobile
WordPress doesn't have any functionalities to detect window width. PHP itself cannot do that.
The most promising solution is to use wp_is_mobile():
if ( !wp_is_mobile() ) {
echo "this";
} else {
echo "that";
}
is it possible to have different headers on each page?
I'm currently using this code which works perfectly
<?php if(is_front_page()):?>
<?php echo do_shortcode('[myiamge1]'); ?>
<?php endif;?>
Now I tried to use this code and it doesnt work
<?php if(is_front_page()):?>
<?php echo do_shortcode('[myiamge1]'); ?>
else { ?> (is_page('Contact')){
echo '<img src="image5.jpg" />';
}
<?php endif;?>
Any ideas?
I know nothing about Wordpress - never used it but the above doesn't look right. Is there a reason whya more traditional style syntax is not adopted - more akin to this:
<?php
if( is_front_page() ){
echo do_shortcode('[myiamge1]');
} elseif( is_page('Contact') ){
echo '<img src="image5.jpg" />';
} else {
/*banana for scale - do something else*/
}
?>
I have an webpage where I want to have another html page displayed. I used iframes. The page gets to know what to load is via the get procedure. But there is an fault in this coding I think...
<iframe src="
<?
$file = ($_GET['ti'])
if ($title = '')
echo "information.html";
else echo "$file";
?>
"></iframe>
The url the page would recieve looks like this:
http://www.website.com/reference.html?ti=unlimited.html
http://www.w3schools.com/php/php_if_else.asp
It's your if / else syntax and over all php code. It's not very well written.
<?php
$file = $_GET['ti'];
if ($title = '') {
echo "information.html";
} else {
echo "$file";
}
?>
Need semicolon:
$file = ($_GET['ti']);
Use empty(), like this:
<iframe src="
<?
$file = ($_GET['ti'])
if (empty($title))
echo "information.html";
else echo $file;
?>
"></iframe>
<?php
$possible = array("information.html", "home.html", "test.html");
$file = isset($_GET['ti']) &&
in_array($_GET['ti'], $possible)? $_GET['ti'] : "information.html";
?>
<iframe src="<?php echo $file;?>"></iframe>
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;
}
?>