custom post types – How to exclude html tags and class on search

Question

I wanted my search to exclude html tags so that in the search results page it wont show html tags and class. I’m a beginner in wordpress development so i find it hard to understand some functions. Here is my search.php:

<?php

/*
Template Name: Search Page
*/

get_header() ?>

<main>
    <div class="page-title above-content">
        <?php //$allsearch = new WP_Query("s=$s&showposts=0");
        ?>
        <h1>
            Search Results
        </h1>
    </div>
    <div class="content flex-col">
        <?php

        if (have_posts()) {
            while (have_posts()) {
                the_post();
                if ($s == '' or $s == ' ') {
                    echo '<p><div class="snackbar">No Results found! Please try again.</div></p>';
                    break;
                }
                ?>
                <!-- // Display the results -->
                <h3 class="search-post-title"><a href="<?php echo get_permalink(); ?>" target="_blank">
                        <?php
                                $title = get_the_title();
                                $keys = explode(" ", $s);
                                $title = preg_replace('/(' . implode('|', $keys) . ')/iu', '<strong class="search-excerpt">\0</strong>', $title);
                                echo $title;
                                ?>
                    </a>
                </h3>

                <?php
        
                $searchTxt = explode(" ", $s);

                $content = get_the_content();
                $content = explode("</p>", $content);
                for ($i = 0; $i < count($content); $i++) {
                    for ($j = 0; $j < count($searchTxt); $j++) {
                        if (str_contains($content[$i], $searchTxt[$j])) {
                            echo '<p>';
                            $content[$i] = preg_replace('/(' . implode('|', $searchTxt) . ')/iu', '<strong class="search-excerpt">\0</strong>', $content[$i]);
                            echo $content[$i] . '... <a href="' . get_permalink() . '" target="_blank">Continue Reading.</a></p>';
                            break;
                        }
                    }
                }

                ?>

                <!-- // Pagination -->

                <hr />
                <?php
            }
        } else {
            ?>
            <p>
                <?php //_e('Sorry, no posts matched your criteria.'); 
                    echo '<div class="snackbar">No Results found! Please try again.</div>';
                    ?>
            </p>
            <?php
        }

        ?>

        <div class="pagination">
            <?php
            
            wp_pagenavi(array('query'));

            wp_reset_postdata();
            ?>
        </div>
    </div>


</main>
<?php get_footer() ?>

This is the result for searching a ‘<‘ symbol.

enter image description here

0
Jacob Peattie 3 weeks 2023-05-09T22:09:46-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse