Reputation: 15
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.
Upvotes: 0
Views: 95
Reputation: 15
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
Upvotes: 0
Reputation:
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>";
Upvotes: 0
Reputation: 7515
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>';
}
?>
Upvotes: 1
Reputation: 91
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.
Upvotes: 0