0

I am trying to set up blog posts so that when an image is added it is wrapped in one div set to float right and when text is added it is wrapped in another div set to float left. So that all images added to posts in the blog will always have images right and text left.

have tried using jQuery to wrap all p tags in a div like so jQuery('p').wrap("<div class='post-txt'></div>");

But I cant work out which PHP file to add this to, so nothing appears to be happening.

Any help to point me to the correct PHP file within wordpress or any other options would be very much appreciated.

All the best

Harry

1
  • 1
    This actually is a XY problem. Please edit your question and explain clearly what part of the content your want to have wrapped in divs with this class. You don't need jQuery.
    – kaiser
    Feb 12, 2014 at 13:06

1 Answer 1

1

You need to add it to a JS file and then enqueue it in your functions.php

jQuery(document).ready(function($){
  $('p').wrap('<div class="post-txt" />');
});

If you already have a js file, you can add it to that file and you're done.

Else, save the above snippet in a new file. Put that file in a folder called js in your theme folder(for the sake of clean code). Let's say you called that file wrap.js.

Add the following in your functions.php file:

function wpse_134325_scripts() {
    wp_enqueue_script( 'wrapper', get_template_directory_uri() . '/js/wrap.js', array('jquery') );
}

add_action( 'wp_enqueue_scripts', 'wpse_134325_scripts' );

You can read more about enqueing scripts on the Codex.

1
  • that's a good way to not break literally everything else on the site (not).
    – Joel M
    Feb 1, 2020 at 4:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.