Tagged: ,

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1484085

    Hello,
    we use the masonry element to show / select posts according to the post tags – this works great.

    When somebody clicks on the “selector” (=post tag) offered by the masonry, a “#” is added to the URL.

    Is there a way to have the “valid” / “complete” URL?
    Thus we could use the link to the page with the selected tags of the Masonries, something like this: https://link-to-website/page-with-masonry?%5Bpost-tag%5D
    The result would be the link to the proper web page with the pre-selected post tag.

    Is this possible?

    #1484125

    Hey Jochen,

    Thank you for the inquiry.

    This should be possible with a custom script. Please try this code in the functions.php file:

    add_action( 'wp_footer', 'auto_click_sort_button_by_tag', 100 );
    function auto_click_sort_button_by_tag() {
        ?>
        <script>
        document.addEventListener("DOMContentLoaded", function () {
            const urlParams = new URLSearchParams(window.location.search);
            const tag = urlParams.get("tag");
    
            if (tag) {
                const buttonClass = tag + "_sort_button";
                const button = document.querySelector("." + buttonClass);
    
                if (button) {
                    button.click();
                }
            }
        });
        </script>
        <?php
    }
    

    This should automatically activate the corresponding masonry sorting button based on the value of the ?tag= parameter in the URL. For example:

    https://site.com/masonry?tag=images
    

    Best regards,
    Ismael

    #1484150

    Hello Ismael,
    thanks for your reply, but this doesn’t work as expected.
    When I click on the selevtion of the masonry, I get the correct “filtered” results.
    When I add /masonry?tag=my-tag to the URL, I get a different result.

    I’ve sent you more details in the private section.

    Any idea ?
    Thanks a lot

    #1484185

    Hi,

    Thank you for the update.

    What happens when you replace the URL param with sort_tag or something else other than tag? We may need to access the site to properly test the modification. Please provide the login details in the private field.

    Best regards,
    Ismael

    #1484219

    Hi Ismael,
    thanks for your reply.
    When we change the URL param with e.g. ?tag=sort_tag, we get the unfiltered outout – all posts are shown.

    I inserted the login details in the private section.

    Thanks a lot,
    Jochen

    • This reply was modified 2 weeks, 6 days ago by jochenmaier.
    #1484266

    Hi,

    You may have forgotten to include the password. Please post the info in the private field.
    Have you tried using ?sort_tag=inbound?

    Make sure to update this line with the correct parameter name.

    const tag = urlParams.get("tag");
    

    Best regards,
    Ismael

    #1484293

    Hello Ismael,
    I assumed you were using the “forgot password” function to create your own password. I have stored a password in the hidden section.

    When I change the code in the functions.php to
    const tag = urlParams.get(“inbound”);
    and add ?sort_tag=inbound? to the equivalent URL, it doesn’t work.

    What I would like to achieve:
    We use a masonry that can be filtered according to the tags. This works fine.
    I’d like to able to have a link that directly sets the masonry filtering to the appropriate tag.

    Thanks a lot

    #1484317

    Hi,

    We modified the script in the functions.php file a bit. It should be working correctly now. Please make sure to purge the cache or remove the browser history before testing. (see private field)

    /* Masonry erweitern */
    add_action( 'wp_footer', 'av_click_sort_button_by_tag', 100 );
    function av_click_sort_button_by_tag() {
        ?>
        <script>
       (function ($) {
    	   $(function () {
    			const urlParams = new URLSearchParams(window.location.search);
    			const tag = urlParams.get("sort_tag");
    			console.log(tag);
    
    			if (tag) {
    				const buttonClass = tag + "_sort_button";
    				const button = $("." + buttonClass + " .inner_sort_button")[0];
    				console.log(button);
    
    				if (button) {
    					button.click();
    				}
    			}
    		});
    	})(jQuery);
        </script>
        <?php
    }
    

    Best regards,
    Ismael

    #1484490

    Dear Isamel,

    thanks a lot for the great support – you may close this ticket. 👍

    #1484509

    Hi,

    You’re quite welcome! Let us know if you have more questions. Have a nice day.

    Best regards,
    Ismael

Viewing 10 posts - 1 through 10 (of 10 total)
  • The topic ‘Question on Masonry’ is closed to new replies.