I need to make the pagetitle in the header of the homepage different from that of the interior pages. It seems like a simple php fix but it is over my head. This seemed to be the solution but implementing it gave an error code : PHP if URL equals this then perform action
This is the code I have:
<?php
/**
* The Header for our theme.
*/
?><!DOCTYPE html>
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=GFS+Didot'
rel='stylesheet' type='text/css'>
<title><?php wp_title('|', true, 'right'); ?></title>
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<div>
<header class="pagetitle">
<h1>
<a href="<?php echo esc_url(home_url('/')); ?>">
<?php bloginfo('name'); ?>
</a>
</h1>
<nav>
<?php wp_nav_menu(array('theme_location' => 'primary')); ?>
</nav>
</header>
Can someone tell me the if/else statement that will give me one on the homepage and a different on all other pages?
Try this:
<title>
<?php
if( is_home() ) {
echo 'Homepage Title';
}
else {
echo 'Other Pages Title';
}
?>
</title>
Reference: http://codex.wordpress.org/Function_Reference/is_home
Here's what I use on my own site to display just the site title on the homepage, but the article title on subpages:
<title>
<?php wp_title(''); ?><?php if(wp_title('', false)) { echo ' |'; } ?> <?php bloginfo('name'); ?>
</title>
Reference: http://codex.wordpress.org/Function_Reference/wp_title
if( is_home() || is_front_page() ) {
echo 'HOMEPAGE';
}
else {
echo 'INTERIOR PAGES';
}
The most recent edit on the question you referred to shows the correct if/else statement:
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($host == 'your.homepage.com/url/etc') {
echo "Homepage header text";
}
else {
echo "Other header text";
}
A wordpress-specific solution for checking if the user is on your home page is the function is_home() which returns a boolean if the main blog page is being displayed. More details about the is_home() function found here. There are also other "conditional tags" in wordpress that come with their own functions. A list of those can be found here.
So for example, you could also rewrite the above PHP code block with the is_home() function:
if (is_home()) {
echo "Homepage header text";
}
else {
echo "Other header text";
}
Related
The problem is that on my one pager website the title tag doesn't get populated.
page.php looks like this:
<?php
locate_template( 'page-home.php', true );
exit();
page-home.php looks like this:
<?php get_header(); ?>
<?php get_template_part('template-sections/slider'); ?>
<?php get_template_part('template-sections/services'); ?>
<?php //etc. ?>
<?php get_footer();
header.php looks like this:
<!doctype html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="profile" href="http://gmpg.org/xfn/11"/>
<base href="<?php echo get_site_url(); ?>/">
<?php wp_head(); ?>
</head>
<body itemprop="hasPart" itemscope="" itemtype="http://schema.org/WebPage" <?php body_class(); ?>>
<!-- etc -->
And footer.php looks like this:
<?php wp_footer(); ?>
</main>
<aside class="aside">
<div class="aside__content">
</div>
</aside>
</div>
</body>
</html>
Yes. There is a page present called home in the backend. And I would like Wordpress to pick that title up and use it as a title tag in header.php.
Now as far as I know, Wordpress normally automatically populates the title tag. So what's the problem here? Thanks!
PS: I am not using the wp_title filter anywhere in my custom Wordpress theme.
Ok, the solution I found might help others. So I felt free to post it here.
Part 1. Comparing with the standard twentyseventeen theme. I found that Wordpress needs add_theme_support( 'title-tag' ); to be able to manage the title. (Otherwise you should just add a <title> tag yourself in header.php, I guess.
Part 2. I needed to add a custom filter to functions.php to display the title in a format that is desired. For example (using a SEO plugin):
function custom_title( $title_parts ) {
$page_id = site_get_page_id(); // custom function, you might want to use global $post here
$seo_title = #get_post_meta( $page_id, '_aioseop_title' );
if ( isset( $seo_title[0] ) ) {
$title = $seo_title[0];
} elseif ( isset( $page_id ) ) {
$title = get_the_title( $page_id );
}
$page_title = isset( $title ) ? $title : 'Page not found in backend';
$title_parts['title'] = $page_title;
return $title_parts;
}
My wordpress site was not showing blog posts content and not the single post content, so I have created a blog template and used a post grid plugin shortcode. So now the blog is showing fine, but when I click on "Read more" it directs to single post, which shows empty page, I have disabled and enabled plugins and checked my htaccess file also. Thank you.
<?php
/*
* Template Name: Blog Post
*/
?>
<?php get_header(); ?>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>
<?php wp_title('|', true, 'right'); ?>
</title>
<link href="<?php echo get_template_directory_uri(); ?>/css/bootstrap.min.css" rel="stylesheet">
<link href="<?php echo get_template_directory_uri(); ?>/js/owl.carousel/owl.carousel.min.css" rel="stylesheet">
<?php wp_head();?>
</head>
<?php the_title(); ?>
<?php echo do_shortcode("[post_grid id='605']"); ?>
<?php get_footer(); ?>
It looks like you are missing the <body> tag and also may be missing the WordPress loop in between the <body>...</body> tags.
Try this:
<body>
<?php while ( have_posts() ): the_post(); ?>
<?php the_title(); ?>
...
<?php endwhile; ?>
</body>
Additionally the <?php get_header(); ?> already contains the HTML <head></head> and all the links and meta within, so you are essentially duplicating/overwriting it.
Try doing this:
<?php
/*
* Template Name: Blog Post
*/
?>
<?php get_header(); ?>
<body>
<?php while ( have_posts() ): the_post(); ?>
<?php the_title(); ?>
...
<?php endwhile; ?>
</body>
<?php get_footer(); ?>
Do also make sure that your header.php file doesn't actually contain the opening <body> tag out of the box though (footer.php should contain the closing tag), as this will determine whether you need the <body></body> tags in my code above.
I need help for something please. I'm working on my structure, but I've some problems...
I created a page called "page-container.php" and the content is:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?= $page_title; ?></title>
<link href="css/style.css" rel="stylesheet">
<link href="css/fonts/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Patrick+Hand" rel="stylesheet">
</head>
<body id="<?= $page_id; ?>">
<?php include "includes/header.php"; ?>
<div class="wrapper">
<div class="page-content">
<?php if(isset($sidebar)): ?>
<div class="main-container">
<div class="main-content">
<?php endif; ?>
<h1 class="page-title"><?= $page_title; ?></h1>
I WANT TO HAVE MY CONTENT HERE FOR ALL MY PAGES
</div>
</div>
</body>
</html>
You can see the "I WANT TO HAVE MY CONTENT HERE FOR ALL MY PAGES".
What I mean is that when I write the content in my other pages (for example, my registration page), I want that all the content is placed at this location, like this:
http://prntscr.com/a3oz5k
All my other pages are like this (exemple with index.php):
<?php
$page_title = "Home";
$page_id = "home";
?>
<?php require "includes/page-container.php"; ?>
If I want to write some content in this page, I need to do like this:
<?php
$page_title = "Home";
$page_id = "home";
?>
<?php require "includes/page-container.php"; ?>
<div id="mycontent">
<p>Heellloo</p>
</div>
But look the result: http://prntscr.com/a3p32y
In the code source this is not normal... http://prntscr.com/a3p3kg
My content is below the html and body tag and I want it to be placed just below my h3 title. How can I do please?
Simple example using ob_start(), ob_get_clean().
<?php
ob_start();
?>
<div id="mycontent">
<p>Hello</p>
</div>
<?php
$page = ob_get_clean();
require "includes/page-container.php";
?>
And then in page-container.php just do echo $page;.
Your code will be still formatted as HTML in your editor instead if it would be in a variable.
You can also split your page-container.php into two parts and include the first part before the content and the second one after.
Reference: ob_start(), ob_get_clean()
You can keep another vartiable say $page_content and use as below :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?= $page_title; ?></title>
<link href="css/style.css" rel="stylesheet">
<link href="css/fonts/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Patrick+Hand" rel="stylesheet">
</head>
<body id="<?= $page_id; ?>">
<?php include "includes/header.php"; ?>
<div class="wrapper">
<div class="page-content">
<h1 class="page-title"><?= $page_title; ?></h1>
<?= $page_content; ?>
</div>
</div>
</body>
</html>
And for some content in another page
<?php
$page_title = "Home";
$page_id = "home";
$page_content = "content";
?>
<?php require "includes/page-container.php"; ?>
`
I may be asking a silly or stupid question. But, i am curious as i am beginner in php. I would like to ask whether this way is possible to happen.
Here goes the question:
I want something do like how wordpress do. But, i'm not going to use wordpress in this site. Like wordpress has the header.php in its path. And then, we can use wordpress php code to show or hide the content we like to display or hide as below.
Below is the php code i copied from Wordpress as an example:
<?php if (is_front_page()) { ?><?php } elseif (is_home() || is_single() || is_page() || is_archive() || is_404() || is_search()) { ?>
<?php
/* We add some JavaScript to pages with the comment form
* to support sites with threaded comments (when in use).
*/
if ( is_singular() && get_option( 'thread_comments' ) )
wp_enqueue_script( 'comment-reply' );
/* Always have wp_head() just before the closing </head>
* tag of NCC, or you will break many plugins, which
* generally use this hook to add elements to <head> such
* as styles, scripts, and meta tags.
*/
wp_head();
?>
<?php } ?>
Because, i going to create a simple website implemented with simple php coding. I am going to create a few pages -> page1.php, page2.php, page3.php and header.php. The header.php is to manage the head section of webpage(like how wordpress doing). Is it possible to get it done with simple php code?
Example:
When i visiting page2.php, the header.php will only display the content that i wanted to display.
Is it possible to change the is_single() to an url? Any suggestion? Or any other better solution can do this easier?
Updated:
header.php
<?php
function includeFile($file){
if($file == "index.php"){
Page Title for index.php
}elseif($file == "page.php"){
Page Title for page.php
}elseif($file == "page1.php"){
Page Title for page1.php
}
}
?>
In my index.php
<?php session_start(); /* Starts the session */
if(!isset($_SESSION['UserData']['Username'])){
header("location:login.php");
exit;
}
?>
<?php include('functions.php'); ?>
<!doctype html>
<html>
<head>
<title>
<?php
require_once('header.php');
includeFile(basename(__FILE__));
?>
</title>
<link href="style.css" rel="stylesheet" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<link href='http://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet' />
<link rel="shortcut icon" href="http://www.example.com/img/favicon.ico" type="image/x-icon">
</head>
<body>
Content...
</body>
</html>
Updated:
Yes, you can certainly do that using PHP magic constant __FILE__ and basename().
header.php
function includeFile($file){
if($file == "page1.php"){
// include stuff for page1.php
}elseif($file == "page2.php"){
// include stuff for page2.php
}elseif($file == "page3.php"){
// include stuff for page3.php
}
}
And in each one of your pages include the header file and call the function includeFile(), like this:
page1.php
<?php
require_once('header.php');
includeFile(basename(__FILE__));
?>
page2.php
<?php
require_once('header.php');
includeFile(basename(__FILE__));
?>
page3.php
<?php
require_once('header.php');
includeFile(basename(__FILE__));
?>
I want to have dynamic header.php in all of my website pages, and there are many CSS files for loading. is there any way to load different CSS files for each page ? for example:
<?php if($title == "title") { ?>
<link href="mycss.css" >
<script src="test.js"></script>
<?php } ?>
You can create a function (or class) to echo your header and feed parameters as settings. Something like this may work:
function.get_header.php
// This function will render the header
function get_header($settings = false)
{
ob_start();
include("header.php");
$data = ob_get_contents();
ob_end_clean();
return $data;
}
function.get_page_css.php
// This function will act like a pseudo database return
// and will return a series of css links based on input
function get_page_css($var = false)
{
$css['title'][] = "/css/style1.css";
$css['other'][] = "/css/style2.css";
$css['title'][] = "/css/style3.css";
if(!empty($css[$var]))
return $css[$var];
}
header.php
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title><?php echo (!empty($settings['title']))? $settings['title'] : "Untitled Page"; ?></title>
<head>
<?php if(!empty($settings['css']) && is_array($settings['css'])) {
foreach($settings['css'] as $link) {
?>
<link type="text/css" rel="stylesheet" href="<?php echo $link; ?>" />
<?php }
}
?>
</head>
index.php
<?php
// Include the header function
include("function.get_header.php");
// Include the css return function
include("function.get_page_css.php");
// Write the header to browser using the get_page_css() function
echo get_header(array("title"=>"This Great Page!","css"=>get_page_css('title')));
?>
<body>...etc.
In a simple way, you can used this method which is given below...
<?PHP
$page_name= $_SERVER['PHP_SELF'];
if($page_name=='about.php'){
echo '<link href="about_css.css" type="text/css">
<script src="about_test.js"></script>';
}
if($page_name=='contact.php'){
echo '<link href="contact_css.css" type="text/css">
<script src="contact_test.js"></script>';
}
?>
first check this url path
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
?>
or
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$url1 = "http://example.com/home";
$url2 = "http://example.com/about";
if (strpos($url1,'home') !== false) { ?>
<link href="mycss.css" >
<?php
} else if(strpos($url2,'about') !== false){ ?>
<link href="mycss2.css" >
<?php }else { ?>
// your defult css file
<?php } ?>
I hope you understood
Put this index.php file in your css folder
<?
$title = $_GET['title'];
header('Content-Type: text/css');
switch ( $title ) {
case 'title_1':
include('style_1.css');
case 'title_2':
include('style_2.css');
default:
include('default.css');
}
exit();
?>
Then whenever you want to call your css :
<?
echo '<link href="path/to/css/folder/?title='.$variable.'" rel="stylesheet">'
?>
The index.php in the css folder will be controller for css needs to be included
For simplicity, you can assign a page code for every page. Create the script with that name. So you can import it like this:
In the page header:
$pageCode = "INDEX";
in the footer:
<?php
$scriptFileName = "your/path/to/file/".$pageCode.".js";
if(file_exists($scriptFileName)){
echo "<script src='$scriptFileName'></script>";
}
apply the same for CSS if needed. But in header!
This worked for me just fine, I added it to the _header.php file which is included on on all my pages:
<?php
$page_name = $_SERVER['PHP_SELF'];
if($page_name =='/recepiepg/admin/index.php'){
echo '<link rel="stylesheet" href="../css/admin.css" type="text/css"/>';
}
?>