Wordpress change HTML content of :after element for specific post category
up vote
1
down vote
favorite
I'm working on a Wordpress site and I would like to select and change the content of a :after
pseudo element ONLY in posts with a specific category tag.
My pseudo-code would go something like this:
- get the post category
- if post category == 'myCategory'
.myElement:after
content: "new content here";
I found the below code on another post and tried to adapt it to my use but with no luck.
function change_next_button()
$cat = get_the_category();
if ( ! empty( $cat ) )
$cat_name = $cat[0]->name;
switch ($cat_name)
case 'nyhet':
document.getElementsByClassName("button:after").style.content="next article";
break;
Much appreciated if anyone could help out with a solution
javascript css wordpress
New contributor
add a comment |
up vote
1
down vote
favorite
I'm working on a Wordpress site and I would like to select and change the content of a :after
pseudo element ONLY in posts with a specific category tag.
My pseudo-code would go something like this:
- get the post category
- if post category == 'myCategory'
.myElement:after
content: "new content here";
I found the below code on another post and tried to adapt it to my use but with no luck.
function change_next_button()
$cat = get_the_category();
if ( ! empty( $cat ) )
$cat_name = $cat[0]->name;
switch ($cat_name)
case 'nyhet':
document.getElementsByClassName("button:after").style.content="next article";
break;
Much appreciated if anyone could help out with a solution
javascript css wordpress
New contributor
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm working on a Wordpress site and I would like to select and change the content of a :after
pseudo element ONLY in posts with a specific category tag.
My pseudo-code would go something like this:
- get the post category
- if post category == 'myCategory'
.myElement:after
content: "new content here";
I found the below code on another post and tried to adapt it to my use but with no luck.
function change_next_button()
$cat = get_the_category();
if ( ! empty( $cat ) )
$cat_name = $cat[0]->name;
switch ($cat_name)
case 'nyhet':
document.getElementsByClassName("button:after").style.content="next article";
break;
Much appreciated if anyone could help out with a solution
javascript css wordpress
New contributor
I'm working on a Wordpress site and I would like to select and change the content of a :after
pseudo element ONLY in posts with a specific category tag.
My pseudo-code would go something like this:
- get the post category
- if post category == 'myCategory'
.myElement:after
content: "new content here";
I found the below code on another post and tried to adapt it to my use but with no luck.
function change_next_button()
$cat = get_the_category();
if ( ! empty( $cat ) )
$cat_name = $cat[0]->name;
switch ($cat_name)
case 'nyhet':
document.getElementsByClassName("button:after").style.content="next article";
break;
Much appreciated if anyone could help out with a solution
javascript css wordpress
javascript css wordpress
New contributor
New contributor
edited Nov 10 at 11:25
qiAlex
1,960420
1,960420
New contributor
asked Nov 10 at 10:46
southbound
63
63
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You cannot change ::before
and ::after
pseudo-elements with JavaScript/jQuery as they aren't a part of the DOM.
What you can do is add a class in your case and do your styling on the stylesheet.
switch ($cat_name)
case 'nyhet':
echo '<script type="text/javascript">document.querySelector(".yourbutton").classList.add("newclass");'</script>
break;
Then in your stylesheet;
.newclass::after
/* Your new styles */
/* You may need to use !important - I don't know your CSS structure. */
Abdul, thanks for your answer, Everything seems to work, except the line where I change the class: 'document.querySelector('yourelement').classList.add('newclass'); This line gives me the following error: Fatal error: Uncaught Error: Call to undefined function querySelector() in /homepages
– southbound
2 days ago
It's because your code is php. What I posted is JavaScript. I'll update the answer.
– Abdul Sadik Yalcin
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You cannot change ::before
and ::after
pseudo-elements with JavaScript/jQuery as they aren't a part of the DOM.
What you can do is add a class in your case and do your styling on the stylesheet.
switch ($cat_name)
case 'nyhet':
echo '<script type="text/javascript">document.querySelector(".yourbutton").classList.add("newclass");'</script>
break;
Then in your stylesheet;
.newclass::after
/* Your new styles */
/* You may need to use !important - I don't know your CSS structure. */
Abdul, thanks for your answer, Everything seems to work, except the line where I change the class: 'document.querySelector('yourelement').classList.add('newclass'); This line gives me the following error: Fatal error: Uncaught Error: Call to undefined function querySelector() in /homepages
– southbound
2 days ago
It's because your code is php. What I posted is JavaScript. I'll update the answer.
– Abdul Sadik Yalcin
yesterday
add a comment |
up vote
0
down vote
You cannot change ::before
and ::after
pseudo-elements with JavaScript/jQuery as they aren't a part of the DOM.
What you can do is add a class in your case and do your styling on the stylesheet.
switch ($cat_name)
case 'nyhet':
echo '<script type="text/javascript">document.querySelector(".yourbutton").classList.add("newclass");'</script>
break;
Then in your stylesheet;
.newclass::after
/* Your new styles */
/* You may need to use !important - I don't know your CSS structure. */
Abdul, thanks for your answer, Everything seems to work, except the line where I change the class: 'document.querySelector('yourelement').classList.add('newclass'); This line gives me the following error: Fatal error: Uncaught Error: Call to undefined function querySelector() in /homepages
– southbound
2 days ago
It's because your code is php. What I posted is JavaScript. I'll update the answer.
– Abdul Sadik Yalcin
yesterday
add a comment |
up vote
0
down vote
up vote
0
down vote
You cannot change ::before
and ::after
pseudo-elements with JavaScript/jQuery as they aren't a part of the DOM.
What you can do is add a class in your case and do your styling on the stylesheet.
switch ($cat_name)
case 'nyhet':
echo '<script type="text/javascript">document.querySelector(".yourbutton").classList.add("newclass");'</script>
break;
Then in your stylesheet;
.newclass::after
/* Your new styles */
/* You may need to use !important - I don't know your CSS structure. */
You cannot change ::before
and ::after
pseudo-elements with JavaScript/jQuery as they aren't a part of the DOM.
What you can do is add a class in your case and do your styling on the stylesheet.
switch ($cat_name)
case 'nyhet':
echo '<script type="text/javascript">document.querySelector(".yourbutton").classList.add("newclass");'</script>
break;
Then in your stylesheet;
.newclass::after
/* Your new styles */
/* You may need to use !important - I don't know your CSS structure. */
edited yesterday
answered Nov 10 at 12:14
Abdul Sadik Yalcin
7332827
7332827
Abdul, thanks for your answer, Everything seems to work, except the line where I change the class: 'document.querySelector('yourelement').classList.add('newclass'); This line gives me the following error: Fatal error: Uncaught Error: Call to undefined function querySelector() in /homepages
– southbound
2 days ago
It's because your code is php. What I posted is JavaScript. I'll update the answer.
– Abdul Sadik Yalcin
yesterday
add a comment |
Abdul, thanks for your answer, Everything seems to work, except the line where I change the class: 'document.querySelector('yourelement').classList.add('newclass'); This line gives me the following error: Fatal error: Uncaught Error: Call to undefined function querySelector() in /homepages
– southbound
2 days ago
It's because your code is php. What I posted is JavaScript. I'll update the answer.
– Abdul Sadik Yalcin
yesterday
Abdul, thanks for your answer, Everything seems to work, except the line where I change the class: 'document.querySelector('yourelement').classList.add('newclass'); This line gives me the following error: Fatal error: Uncaught Error: Call to undefined function querySelector() in /homepages
– southbound
2 days ago
Abdul, thanks for your answer, Everything seems to work, except the line where I change the class: 'document.querySelector('yourelement').classList.add('newclass'); This line gives me the following error: Fatal error: Uncaught Error: Call to undefined function querySelector() in /homepages
– southbound
2 days ago
It's because your code is php. What I posted is JavaScript. I'll update the answer.
– Abdul Sadik Yalcin
yesterday
It's because your code is php. What I posted is JavaScript. I'll update the answer.
– Abdul Sadik Yalcin
yesterday
add a comment |
southbound is a new contributor. Be nice, and check out our Code of Conduct.
southbound is a new contributor. Be nice, and check out our Code of Conduct.
southbound is a new contributor. Be nice, and check out our Code of Conduct.
southbound is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238166%2fwordpress-change-html-content-of-after-element-for-specific-post-category%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password