Hello, how can code the php in wp so a custom page will call on specfic css & not the style.css?
or is this something you could do w/js?
thx
D
Using conditional tags, you can do something like this between your <head></head>
HTML tag:
<?php if(is_front_page()): ?>
<link rel="stylesheet" href="styles/front-page.css">
<?php elseif(is_home()): ?>
<link rel="stylesheet" href="styles/home.css">
<?php elseif(is_single()): ?>
<link rel="stylesheet" href="styles/single.css">
<?php endif; ?>
Conditional tags will let you check if itâs on the front page is_front_page()
, the blog landing is_home()
, a single blog post is_single()
, or has specific templates etc.
thank you labofoz so i could also do:
<?php if(is_funny_page()): ?>
<link rel="stylesheet" href="styles/funny-page.css">
?>
it doesntâ just have to be the reg wp pages correct?
D
No you canât make up your own conditional tag, but you can use is_page(); with the desired parameters to target the page you want.
http://codex.wordpress.org/Function_Reference/is_page
There is also is_single(), etc. Just look up conditional tags in the WordPress codex for more.
I see, thank you for example
<?php if(is_page('funnyPage')): ?>
<link rel="stylesheet" href="styles/front-page.css">
<?php endif; ?>```
that should fly
tweaking it but that did not quite fly.
i got a single actually titled â/urchin-cove/â but tried also using a page.
so i put this in the header
<?php
<?php if(is_single('urchin-cove')): ?>
<link rel="stylesheet" href="css/wikiPage.css">
<?php elseif(is_page('urchin-cove')): ?>
<link rel="stylesheet" href="scss/wikiPage.css">
<?php endif; ?>
<?php wp_head(); ?>
but no dice there either, am i doing this incorrectly?
thx
D
Here are some examples from the WordPress Codex -
is_page();
// When any single Page is being displayed.
is_page( 42 );
// When Page 42 (ID) is being displayed.
is_page( âContactâ );
// When the Page with a post_title of âContactâ is being displayed.
is_page( âabout-meâ );
// When the Page with a post_name (slug) of âabout-meâ is being displayed.
is_page( array( 42, âabout-meâ, âContactâ ) );
// Returns true when the Pages displayed is either post ID 42, or post_name âabout-meâ, or post_title âContactâ. Note: the array ability was added at Version 2.5.
You have to be careful to use a parameter type that is acceptable. Did you use page.php or single.php for the pages you want to target? You donât need to use both is_page()
and is_single()
in the conditional.
Make sure urchin-cove
really is the page slug, or funnyPage
really is the page title (ie what displays if you use > the_title(); in your loop.
Thank you webMachine. this is what i have.
the page created trough the wp-admin area is:
/urchin-cove/ I have selected for it the custom template :
urchinCoveWiki.php
the code i place in the header.php is
<?php if(is_page('urchin-cove')): ?>
<link rel="stylesheet" href="css/wikiPage.css">
<?php endif; ?>
<?php wp_head(); ?>
I had tried enqueing it first. but as i am targeting the body & html tags it over rides the settings for all the site.
D
Actually, if you want to make a custom template, your should really name it page-urchinCoveWiki.php and then you can use the is_page(); conditional tag.
You might want to take a look at the explanation of the WordPress template system:
http://codex.wordpress.org/Template_Hierarchy
well. will look it up. doesnât look very clear to me. thx
D
but shouldnât have the code i posted above have worked?
D
No it wonât work, I think, because your template is not named as a page. I may be wrong, though. Basically the template system is this: if you make a page template named page-example.php for example, the website will look for that template first, and if it canât be found, the website will automatically look for your page.php file and display that one in its place and if it canât find page.php it will display the default index.php.
Another thing that might be the problem is that WordPress deals with absolute paths, not relative paths. So your href=âcss/wikiPage.cssâ needs to be changed to use the full path.
i think wordpress actually hates me.
yeah so far in all my experiences for templage pages. i have always just created a page.
then at the top of the new php added
*Template Name: whatever
?>```
And that did it.
How about the conditional tag is_page_template( 'urchinCoveWiki.php' )
Then you donât need to use a page.php template.
If that doesnât work, still try the absolute path to the stylesheet -
Did you try changing the path of the stylesheet to an absolute path? Try something like
<link rel="stylesheet" href="<?php blog_info('url'); ?>/css/wikiPage.css">
i was trying this, directly in the head of the custom page
<?php
/*
Template Name: urchinCoveWiki
*/
<?php if(is_page('urchin-cove')): ?>
<link rel="stylesheet" href="<?php get_template_directory_uri(); ?> /css/wikiPage.css">
<?php endif; ?>
get_header(); ?>
and going no where. just tried this both in the head of the custom & then the header.php
<?php if(is_page_template( 'urchinCoveWiki.php' )): ?>
<link rel="stylesheet" href="<?php blog_info('url'); ?>/css/wikiPage.css">
<?php endif; ?>
with same results.
but thank you for the suggestion however.
D
ps i also tried to w/the wp-admin page name. Next was going to try the shortlink #
the code looks correct. should workâŚ
D
ok. gv up and put the style direct in the page. But that is pretty crude. would love to find out how to do it right?
D
Can you try adding this to your file (anywhere):
<table>
<tr>
<td>Tag</td>
<td>Pass?</td>
</tr>
<tr>
<td>is_page()</td>
<td><?= is_page() ? 'X' : '' ?></td>
</tr>
<tr>
<td>is_page('uchin-cove')</td>
<td><?= is_page('urchin-cove') ? 'X' : '' ?></td>
</tr>
<tr>
<td>is_single()</td>
<td><?= is_single() ? 'X' : '' ?></td>
</tr>
<tr>
<td>is_single('urchin-cove')</td>
<td><?= is_single('urchin-cove') ? 'X' : '' ?></td>
</tr>
<tr>
<td>is_page_template('urchin-cove')</td>
<td><?= is_page_template('urchin-cove') ? 'X' : '' ?></td>
</tr>
</table>
How many Xâs are you seeing. If youâre not seeing a table with exactly 3 Xâs then somethings wrong.
Oh ok actually thatâs good, I noticed I had a typo for that last one, should be is_page_template('urchinCoveWiki.php')
or whatever the filename isâŚbut thatâs ok.
Anyways, this helped me find the potential problem - there was a typo in what you entered for the stylesheet. It should be:
<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?> /css/wikiPage.css">
Notice the echo