I am making a site on Wordpress and plugging in my custom html and css to the HTML5Blank template. The css file is enqueued using WordPress functions into wp_head... I have tried inserting a conditional php if statement into head and it has no effect because
<?php wp_head(); ?>
is still being called. Removing this and keeping the conditional statement
<?php if( is_home() ) { ?>
<link rel="stylesheet" type="text/css" media="all" href="<?php echo get_stylesheet_directory_uri(); ?>/front-style.css" />
<?php } else { ?>
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( '/style.css' ); ?>" />
<?php } ?>
still doesn't fix it. I want my homepage to call a different css file than the rest of the website. I'll only need potentially 2 css files and have already made another one called frontstyle.css in the same directory as the original css file.
Thanks in advance!
EDIT: have also been trying this but it isn't working either but actually seems the most promising.
?php if( is_home() ) { ?>
<?php get_header(); ?>
<?php } else { ?>
<?php get_header( 'other' ); ?>
<?php } ?>
with files header.php and header-other.php respectively. All I really need to change is the header. I plugged this into the page template and didn't get the response I needed but it did change a bit.
Related
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 have a index.php file which loads (require) 2 different files based on a condition.
Visiting this link will cause;
mysite.com/index.php will load stats.php (require("stats.php");)
Visiting this link will cause;
mysite.com/index.php?auth="jgbsbsbasm" will load encry.php (require("stats_encry.php");)
Done with this code:
<?php
if(isset($_GET['auth'])) require("stats.php");
else require("stats_encry.php");
?>
This works fine. Now my question is; I have a CSS file in the header as static;
<link rel="stylesheet" href="cv.css">
What I want is now to load cv.css file for stats.php and cv1.css for stats_encry.php respectively.
How do I do this?
In your header replace
<link rel="stylesheet" href="cv.css">
with
<?php if(isset($_GET['auth'])){ ?>
<link rel="stylesheet" href="cv.css">
<?php } else { ?>
<link rel="stylesheet" href="cv1.css">
<?php } ?>
You can use this like below syntax
<?php
if(isset($_GET['auth'])){
echo '<link rel="stylesheet" href="cv.css">';
} else {
echo '<link rel="stylesheet" href="cv1.css">';
}
?>
I want to add something like this to my page header, in order to load a js and css file only if NOT an iPhone/iPad:
if(!(navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
{
//Link to js file
//Link to css file
}
The first line works fine, I've tested it by pasting in a script, but I'm not sure how to get the files linked... especially as I'd like the js and css links to start with the WP template tag <?php echo get_stylesheet_directory_uri(); ?>
Thank you in advance for your help!
<?php
if (!preg_match("{iPhone|iPod}i",$_SERVER['HTTP_USER_AGENT']))
{
echo '<script src="yourscript.js"></script>';
echo '<link href="yourstyle.css" rel="stylesheet" type="text/css">';
}
?>
It's better to check the user agent using PHP, since it's easier to then add the right HTML tags to load the right css and js files.
<?php if ( !preg_match( '/iPhone/', $_SERVER['HTTP_USER_AGENT'] ) && !preg_match( '/iPod/', $_SERVER['HTTP_USER_AGENT'] ) ): ?>
<link href="<?php echo get_stylesheet_directory_uri() ?>/style.css">
<script src="<?php echo get_stylesheet_directory_uri() ?>/script.js" type="text/javascript"></script>
<?php endif; ?>
Doing it using just javascript would add unneeded complexity, and need you to use a js loader as RequireJS http://requirejs.org/
Something like that:
<?php if(!(navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) : ?>
<link href="<?php echo get_stylesheet_directory_uri() ?>/main.css" type="text/css" rel="stylesheet" >
<script src="<?php echo get_stylesheet_directory_uri() ?>/main.js" type="text/javascript"></script>
<?php endif ?>
I am using multiple stylesheets and need the pages to differ based on category.
I added the following to my header.php, but shows base theme`s single entry template. Any ideas?
<?php if (is_category('20')) { ?>
<link rel="stylesheet" type="text/css" href="wp-content/themes/tanzaku/style.css" />
<?php } else {?>
<link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url'); ?>/style.css" />
<?php } ?>
I am using the same theme as these sites, but I use two themes on my site compared to these.
http://marioortega.net/
http://atelier6.co.uk/
When you click one of the thumbnails, the single ends up on top and the thumbnails at the bottom.
All:
Thank you for looking into this. In order for me to get this to work, the problem was actually multiple single.php files. This can be solved by having your single.php look like this,
<?php
$post = $wp_query->post;
if ( in_category('20') ) {
include(TEMPLATEPATH . '/single1.php');
} else {
include(TEMPLATEPATH . '/single2.php');
}
?>
I have also edited the question for other people looking for this answer.
Shouldn't you be using echo statement to put the link into your page.
<?php
if (is_category('20')) {
echo '<link rel="stylesheet" type="text/css" href="wp-content/themes/tanzaku/style.css" />';
}
else {
echo '<link rel="stylesheet" type="text/css" href="'.bloginfo('template_url').'/style.css" />';
}
?>
Hey guys. I was wondering how would I make theme options for a cms of mine like tumblr? I understand how to use the code like tumblr, {description}, {text} and variables like that but how would I make a theme switcher like this in php?
Simply put a php variable in your head section. Be sure to create the $userCSSchoice at the beginning of your page or you will break it all! Doom!
<LINK REL=StyleSheet HREF="<?php echo $userCSSchoice; ?>.css" TYPE="text/css" MEDIA=screen>
Nah, seriously, I do this and it works just fine for me. Then you have to create all those css stylesheets, but that's not too hard.
An alternative is to create your CSS page in PHP so you can pass a variable to a single sheet to determine the style.
<link rel=StyleSheet href="styles.php?theme=<?php echo $userCSSchoice; ?>" type="text/css" media=screen>
Then your PHP/CSS page can determine colors/images..
<?php
header("Content-Type: text/css");
if (isset($_REQUEST['theme']))
$theme = $_REQUEST['theme'];
else
$theme="default";
$bgImage="images/bg_".$theme.".png";
if ($theme=="default")
$mainColor="0f8200";
else if ($theme=="green")
$mainColor="009900";
?>
body {
background:url('<?php echo $bgImage; ?>') repeat-x #<?php echo $bg; ?>;
color: #<?php echo $mainColor; ?>;
}