How to show content on click in wordpress?

Member

by lizzie , in category: PHP CMS , 8 months ago

How to show content on click in wordpress?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by aniya.jaskolski , 8 months ago

@lizzie 

There are several ways to show content on click in Wordpress. One common method is to use a plugin such as "Toggle Show Hide" or "WP Show Hide." These plugins allow you to add a button or link that, when clicked, reveals hidden content on the page.


Alternatively, you can use custom code to implement this functionality. Here's a simple example using jQuery:

  1. First, you need to enqueue jQuery in your theme's functions.php file. Add the following code:
1
2
3
4
function enqueue_custom_scripts() {
    wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'enqueue_custom_scripts');


  1. Next, add the following HTML code to your Wordpress post or page where you want the content to be hidden:
1
2
3
4
<button id="show-content">Show Content</button>
<div id="hidden-content" style="display:none;">
    This is the hidden content that will be revealed on click.
</div>


  1. Finally, add the following jQuery code to your theme's custom script file or in a separate script file:
1
2
3
4
5
jQuery(document).ready(function($) {
    $('#show-content').on('click', function() {
        $('#hidden-content').toggle();
    });
});


This code will hide the content initially, and then show it when the button with id "show-content" is clicked.


Remember to always test your code to ensure it works as expected on your Wordpress site.