Live website encounters unexpected elements like <p> and <a> in Wordpress

Just starting out with website development on Wordpress and tinkering with my home page to improve its appearance. Encountered an issue while attempting to enclose my div within an a tag to make the entire box clickable as a link.

The code for in-page editing looks like this:

<a href="random_link1">
  <div>
    <img src="random_image1"/><br>
    random text 1
  </div>
</a>
<a href="random_link2">
  <div>
    <img src="random_image2"/><br>
    random text 2
  </div>
</a>

Multiple divs in a row are supposed to be side by side (using CSS display: inline-block)

However, on the live site, these divs end up stacking vertically. Upon inspection using developer tools (Chrome), I noticed that the code includes an extra p element with the same a tag in the middle of the divs:

<a href="random_link1">
  <div>
    <img src="random_image1"/><br>
    random text 1
  </div>
</a>
<p>
  <a href="random_link1"></a>
  <a href="random_link2"></a>
</p>
<a href="random_link2">
  <div>
    <img src="random_image2"/><br>
    random text 2
  </div>
</a>

Does anyone know how to resolve this issue?

Answer №1

WordPress has a built-in filter called wpautop that automatically converts line breaks from the editor into HTML spacing. To disable this globally, you can add the following code to your functions.php file:

remove_filter( 'the_content', 'wpautop' );

If you prefer more control over which content gets auto-formatted, you can explore plugins that offer fine-tuned options.

Another handy trick is to hide empty <p> tags using CSS:

p:empty {
  display: none;
}

When it comes to links, WordPress also wraps them in <p> elements using the same wpautop filter. One solution is to either enclose your <a> tags inside <div> tags or create a custom filter function to reverse what wpautop does after applying it. For more information, refer to this discussion.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The HTML element in the side menu is not adjusting its size to fit the parent content

Update: What a day! Forgot to save my work... Here is the functioning example. Thank you for any assistance. I have set up a demo here of the issue I'm facing. I am utilizing this menu as the foundation for a page I am developing. The menu is built ...

Creating an icon menu using Bootstrap 4: A step-by-step guide

I'm in the process of creating a navigation bar that includes SVG icons. What's the most effective method to achieve this using Bootstrap 4? This is my desired outcome: The closest I've come (without specifying a fixed size for the icons ...

Guide on how to submit an image along with text using AJAX, PHP, and HTML

Hey there! I'm currently working on a project where I need to upload an image with comments added into a database using a combination of PHP, AJAX, and HTML. Let me show you the HTML part first: <form name="form1" enctype="multipart/form-data" ac ...

Calculate the Pythagorean Theorem with HTML and JavaScript

I'm currently attempting to create a Pythagorean theorem calculator using HTML and Javascript. The goal is for the calculator to determine any side when given two side values. I've implemented if statements in my code, but for some reason, it doe ...

Manipulating images separately using the canvas

Currently, I am utilizing an HTML5 canvas to generate a collection of trees alongside their corresponding shadows. My objective is to ensure that each shadow corresponds with the position of a designated light source. While I have successfully drawn each t ...

properly referencing files

I'm having trouble referencing the correct files in my c9 project: The first issue is with a picture I want to reference. This is what my current code looks like: <img src="pic1.jpg" class="d-block w-100" alt="...">. Here is my folder structu ...

What is the best way to incorporate a minimum width and maximum width in CSS that add up to 100% when necessary?

Can anyone help me find CSS rules that can set a fixed width for an element (width: 500px) but also allow it to be responsive with max-width: 100% if the container is narrower than the element? I saw this example and it works perfectly: .elem { width: 60 ...

Craft unique looks for both even and odd Tumblr posts

I recently came up with a unique idea for a Tumblr theme where the design of posts changes based on whether they are even or odd in the sequence. While I have experience with basic HTML coding, delving into Tumblr customization is new to me. Here's wh ...

Eliminate the content located between two specific words in the img src URL and then render the file from the new source by utilizing ht

My img tags have parameters in the url for ajax image crop functionality. Here's an example: <img src="/resize/45-800/files/myImage.jpeg" alt="Chania"> Add /resize/45-800/ before the url to instruct the ajax script to fetch the file from the f ...

elements positioned next to each other with gaps in between

Why is it so challenging to position two divs next to each other with a small space between them? I've attempted <div class="wrapper"> <div class="box" style="background-color: pink;">First Div</div> <div class="spacer">& ...

How do you fix a re-declare error in Wordpress functions?

Although I have experience with PHP, WordPress is a new realm for me. I'm simply looking to make a quick change without delving too deeply into it. The issue arises when declaring a function in the file: var/www/wp-content/plugins/woocommerce/templa ...

Utilizing AJAX to call an HTML file that incorporates JavaScript and CSS components

Currently, I am incorporating AJAX to fetch an HTML file that contains jQuery and JavaScript. Despite already including these scripts on the page, the JavaScript (and CSS) are not running as expected. Any suggestions on how to make my JavaScript function ...

Implement a counter in a JavaScript table, initializing it to zero

I have successfully written my code, but there is one issue. The first row is starting with the number one instead of zero. I'm looking for suggestions on how to start from zero. Any help would be greatly appreciated. Thanks! <script> var tabl ...

issue with uploading files in firefox

Within my asp.net page, I have the following code: <input id="MyTextBox" runat="server" type="text" name="T1" size="20"/> <asp:Button ID="UploadFileButton" runat="server" Text="Upload" /> <input id="FileUpload" runat="server" type="file ...

Need help adding key:value pairs to a JavaScript dictionary? Here's how!

I am facing an issue with my JavaScript code. I have two HTML text input elements and a paragraph element. In my script, I have an empty JavaScript array and a function that is supposed to push the values of these two input elements as key-value pairs into ...

Bootstrap overrides the color of the calendar year in jQuery UI

I am facing an issue where the CSS styles of my jQuery UI calendar are being overridden by the Bootstrap CSS styles. Take a look at the screenshot provided. As you can see, the text color of the calendar's year is not black as intended. https://i.ss ...

Results of Vue.JS Quiz: 2 correct answers, while 3 were incorrect

Creating an interactive Quiz or survey with scoring functionality in Vue.js. In this sample, there is only one correct answer for each question. At the end, the total score is displayed and users are redirected to another page (not shown in this demo). I ...

The scrolling feature induces a contraction to the left side

Recently, I implemented the code below to fix my menu when scrolling the page: var num = 5; $(window).bind('scroll', function () { if ($(window).scrollTop() > num) { $('.scroll').css({'position':'fixed&apo ...

Retrieving the value of a formControl within a formArray is made possible through a reactive form in

How can I retrieve the value of ItemName in my HTML code? When I attempt to use {{invoiceForm.controls[i].items.controls.itemName.value | json}}, it returns as undefined. <form [formGroup]="invoiceForm"> <div formArrayName="items" *ngFor="let ...

In Bootstrap 4, IE 11 struggles with images overflowing within a d-flex div containing an img tag

My webpage utilizes the bootstrap 4 grid system and appears correctly on most browsers, except for IE 11. If you check out this snippet on jsfiddle in IE 11, you'll notice that the image gets stretched inside a d-flex container. How can I resolve this ...