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










share|improve this question









New contributor




southbound is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    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










    share|improve this question









    New contributor




    southbound is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      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










      share|improve this question









      New contributor




      southbound is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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






      share|improve this question









      New contributor




      southbound is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      southbound is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Nov 10 at 11:25









      qiAlex

      1,960420




      1,960420






      New contributor




      southbound is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 10 at 10:46









      southbound

      63




      63




      New contributor




      southbound is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      southbound is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      southbound is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          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. */






          share|improve this answer






















          • 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










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          southbound is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          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






























          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. */






          share|improve this answer






















          • 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














          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. */






          share|improve this answer






















          • 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












          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. */






          share|improve this answer














          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. */







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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
















          • 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










          southbound is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          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.













           


          draft saved


          draft discarded














          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














































































          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3