Alright, so I struggled with this for days. I'm still in the process of learning PHP. Meanwhile, I'm building a site that I need to make a custom blog template so multiple authors can post blogs on it. The authors know nothing about any code language at all, and therefore, the easiest way out of this (have them manually apply CSS classes) is out of the question. Therefore, I need to apply CSS to all new blog posts but not to any other kind of page dynamically. I've scoured the interwebs trying to find a way out and can't find any solution that works.
Heres what I've tried to implement into my functions page so far:
if (is_singular('post')) {
echo '<link rel="stylesheet" type="text/css">#primary p{margin-bottom: 10px; font-family: Alice; color: #fff; text-align: justify;
text-indent: 50px;}';
}
I know in advance that this code is butchered. I also know that this question is a bit unclear. When the answers start rolling in I'll be glad to clarify in any way I can. I'm still a student so bear with me. Thanks, all.
UPDATE: After a lot of digging around and trying different things I got it figured out. I couldn't understand why so much of my code wasn't working the way the codex said it should. After much frustration, I came to see that I was placing my code in the wrong place. I was trying to work from within functions.php (outside of the loop) but finally got it to work as intended from within it.
For any other students out there just now learning to code within WordPress, the loop is, in fact, the same as the main() within other programs. Many codes only work right from within it. Valuable lessons learned. Thanks to everyone for the help! Trust me, it is appreciated.
OK .. So your code IS butchered ... But not to worry we can work with that.
I am going to break the tags down into separated echo statements so they are better understood. First off you don't need the link tag as you are not calling to a separated CSS file. Use the style tag instead (Of which you need a start tag and an end tag!).
<?php
if (is_singular('post')) {
echo '<style>';
echo ' #primary p{';
echo 'margin-bottom: 10px;';
echo 'font-family: Alice;';
echo 'color: #fff;';
echo 'text-align: justify;';
echo 'text-indent: 50px;';
echo '}';
echo '</style>';
echo '</head>';
}
?>
I think it will be better if you define your CSS class/code in the main CSS file of the theme (usually this is style.css).
Then you can find the render for the posts in your theme (check wp-content/theme/mytheme) and apply the CSS class depending of the conditions.
Using <link rel="stylesheet"> is for linking to an external stylesheet file. You could try something like this:
echo "<link rel='stylesheet' type='text/css' href='author1.css'>";
However, if you wanted to include all of the CSS properties within that same file, you could use a <style> tag. I would recommend against this as it can be difficult to work with since all of the CSS is not formatted/inline.
echo "<style>#primary p { margin-bottom: 10px; font-family: 'Alice'; color: #fff; text-align: justify; }</style>";
Zak! Thank you so much for your help. This is just what I needed. The post is now active on the page. Your time is well appreciated. I have, however, ran into another issue in implementing the solution.
The issue is that even though I was using the is_singular() command to differentiate between post types I kept seeing the same css active on my main page. After some digging, I figured out that this is because I'm using the Beaver Builder plugin. The plugin is a visual editor and uses posts within posts so-to-speak. To fix this issue I have moved to trying to display the css according to author, as all the blogs are published by different authors than the pages.
In my attempt to modify the above if-statement I have looked into the WordPress Codex, finding the get_the_author() and get_the_author_meta() commands. The new issue is so-far that while I've looked up syntax for these commands, I have only been successful in crashing my site, or affecting no change at all. Any help you can offer me in this regard would be a big help. Thank you.
Link
Related
In one of my php functions I add some quite simple css. It worked very well until today. Actually the css is still doing his job, but in addition it prints in the middle of my layout whatever is written between the <style></style> tags - in my case it shows .element {display: none !important;} .
My code:
echo'<style>.element {display: none !important;}</style>';
Has there been any update to php or WP that doesn't allow this anymore? Is there any other way to do this?
Thanks a lot for your help!
I am not sure if it might change anything, but can you try print?
Also I think if <style> is not within <head></head> it will not work.
I found a working solution. Previously, I had a function in functions.php, which did some user type specific PHP and also CSS. Now I removed the CSS from functions.php and added it to header.php just before </head>.
The function is in two places now, which I wouldn't prefer, but it works very well.
Still a riddle to me is, how the echo "<style>...</style>"; used to work in functions.php for approx. the last 6 months or so and then suddenly yesterday created mentioned issue.
Btw, I also tried ?><style>...</style><?php and print "<style>...</style>";. All created the same issue. Again, the "<style>" was not shown on the browser, only whatever was inside. Also, the CSS worked. Whatever, statements I entered was interpreted as correct CSS, but in addition also shown as text right between the layout. Here is an example source code at the browser at my example page :
source code at browser
if you wanna to add a custom style to your WordPress theme (that probably you do not design it) its better to add it to custom styles from your admin area.
I need some idears or informations about the following situation.
I have a page with a own css style-file (Let us call it "Motherpage")
In this Motherpage i load a second html file file by php ("let us call it "Childpage").
<?php
$childpage = file_get_contents($pathtoloaclfile);
echo $childpage;
?>
This works fine.
In the Chilpage are one or more own css-styles and css-files.
Now i have trouble with the two(or more) different css-files and styles.
Some styles broken, caused some styles from the chilpage will overrite or add or inherit to the motherstyles :-(
in the motherfile i use a prefix for every class
.motherpage body{ ... }
But it seems that that is not enought.
What i can do to make sure, that the motherstyles (also the child-styles for himself too) will work on the elements only defined in the motherstyle css.
For example, in the motherfile is no "body" Style defined, in the childstyle, is it defined. So this will affect to a lot of styles, by inherit
I hope, i could explain, whats the problem, and whats the quetion.
Thanks a lot to inspire and helping.
What you can do is (also suggested by #jeff, in comments above), load the styles in this order, in the page where you are having styling issues.
motherpage_styles.css
childpage_styles.css
And override any specific styles required by the page in the childpage_styles.css.
Let's say motherpage_styles.css has following CSS
body {
background-color: #eee;
}
And in the childpage_styles.css you can override and change the background-color like this.
body {
background-color: #ff0000;
}
If the order is like above, then this will work without needing of !important. However, the class you are overriding has !important in the motherpage_styles.css, then you will have to use the !important flag in the overriding class in the childpage_styles.css as well.
Hope you will find this helpful. Cheers.
You could try loading the new page in an iframe so it doesn't interfere with the CSS styles.
Picture to get understanding of my question!
So, this is the site I made for fun and It's a little dice game where you can guess the outcome of the dice. Anyways, as you can see there is a border under the other div. That's where the output comes from the dice game.
After rolling the dice.
So my question is:
Is there a way to make the CSS style before doing the 'game' different than after playing the game? (Border width: 0px; before and 1px after) Or is there a better way to do this instead of changing the CSS??
Edit by Martijn: The code given in the question makes to question obfuscated and didn't really need to be added. IMO it decreased the value of this question.
Yes, but first no: PHP can not change css. PHP is serverside, meaning it's build on a server and the result gets send to your browser. CSS styles the htmlpage on your computer, your computer´s browser calculates how big everything should be etc.
You can however, add a class to the html, depending on the result. This class can be styles.
You have not provided code, so´ll write you a small demo.
$indicationClass = ""; // not good or bad, so no class
if( $Guess=="correct" ){ $indicationClass = "CorrectAnswer"; }
elseif( $Guess=="wrong" ){ $indicationClass = "WrongAnswer"; }
<div id="ImTheResultDisplayer" class="<?=$indicationClass;?>">
The color of my text will change!
</div>
This isn't perfect code, but it does demonstrate my point.
If this is done via AJAX or Javascript (meaning the page never refreshes, you can use the same principle.
There is no way to modify your CSS with PHP
Try to learn Javascript add "movement" and other things to your web page, this language allows you to modify the style of your website after this is served by the server
Its a long way on web development but I wish you luck
How do you link to relative links in wordpress?
For example I'm building a site onthisdomain.co.uk but the site will actually be sent live tothisdifferentdomain.co.uk.
Using the a tag brings in the full URL which I don't want as it'll cause problems when I move the sites over. Is there a php snippet I can use? I've googled it and found an image one, <img src="<?php echo get_template_directory_uri(); ?>/... " alt="" />, but could do with an anchor one
Many thanks
I'm sorry for my bad English. I hope you will understand it.
I think you are a beginner in WordPress theme development. Best of luck to you. I think you know how to use CSS. What I mean, when we use background-image to our class. we have to think where is the image is and where is my css file is. That's how we link images to css class. If I am right. Just like that in WordPress PHP we can use.
So, we can use css files in any website or any domain name. if we use like see below.
FOLDER STRUCTURE
>css->style.css
>js
>img->hello.jpg
index.php
Just imagin. If we use css class to hello.jpg
.myimg {
background-image: url('css/hello.jpg');
height: 100px;
width: 200px;
}
So now we can use this class in any domain. But that folder structure need to be same. If I am right. Simply use, get_template_directory_uri(). To more info read reference.
CODEX
Theme Development
Contact me by searching yeshansachithak in any social network. Thanks
The relative links are returned from the get_template_directory_uri() function.
It probably returns a string that looks like this : "/path/to/file.jpg"
I have no experience in wordpress but making relative links isn't specifically a php thing.
I use:
"> to link directly to a page ID.
to link to your theme root
to link to the root
I am trying to customize the links on my site that are inserted via php. The reason they are inserted via php is for checking user login and editing the log options (the links in question) depending on their status so removing them from php is a no go as far as I can see. I've tried inline and external styling, and though, if I remember correctly, it has worked in the past for other things, it just will not work for these links. Anyone have any good ideas?
Here is the (immediate) code:
$logOptions = $PM_envelope . ' home profile settings logout' ;
The styles are this:
.loginmenulinks a:link {
color:#09C;
text-decoration:none;
font-family:GeosansLight, sans-serif;
font-size:12px;
}
same for hover, etc.
I call for this in a div in the header:
<div><?php echo $logOptions; ?></div>
Why in the world is there "no way to customize" the CSS of a link that PHP generated? PHP generates HTML, HTML and CSS are on the browser side. The browser has no way of knowing what came from PHP and what didn't, so how can it discriminate against such dynamic content?
<?php echo "Text"; ?>
CSS:
.blah {color: orange;}
Suddenly, an orange link appears.
Are you forgetting to maybe specify any styles in the first place?
You may add additional classes or id's to the a-tags to gain the ability to add your stylessheets from an external resource.
However it is not a good Idea to keep such things in your PHP Code, you should use some seperation between a view and a logical layer in your application.
Furthermore you should not use but css to gain spacing, as it is not intended to do that.