admin管理员组

文章数量:1291144

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

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

Share Improve this question asked Feb 12, 2014 at 11:40 Harry RookHarry Rook 12 bronze badges 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 Commented Feb 12, 2014 at 13:06
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: phpWrap posts p tags in div