• I’m trying to place a second search bar, along with the main navigation search bar, on a specific page within a site so that I can search within just that page. Per the recommendation of someone, I placed the following code in my functions.php file.

    add_shortcode( 'search', 'tu_search_shortcode' ); function tu_search_shortcode() { ob_start(); get_search_form(); $ret = ob_get_contents(); ob_end_clean(); return $ret; }

    and then added the shortcode [search] to the page.

    That worked for putting the search bar on that page where I wanted it, but it still searches the entire site like the general search bar does.

    Does anyone know if I can limit the search to that specific page?

    Or, since this is going to be a gallery of images with titles or captions below the images that link to a page with more info, is there a gallery plugin that has a search option that I could use?

    Hope all of this makes sense, but please let me know if it doesn’t and I’ll try to explain better.

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You are still using the single site search form tho :
    get_search_form
    I think the easiest path would be to create a search form template for that page :
    get_template_part( "search-form-{$name}.php" );

    Neither are great ideas imo.

    If it is for an image gallery, I would encourage you to use Tags + Filters so the user has the ability to sort the images based on their interestes (if that is what you are trying to achieve). Like this :
    https://wp-modula.com/demo/filterable-wordpress-galleries/
    https://en-ca.wordpress.org/plugins/modula-best-grid-gallery/

    It creates a lot more flexibility bc you can assign multiple tags to a single image.

    https://awplife.com/demo/portfolio-filter-gallery-free-wordpress-plugin/

    Thread Starter dsb0328

    (@dsb0328)

    Interesting. Don’t know anything about the search form template that you mentioned, but if it shows results from just that page, that might be the right result. Can you expand?

    Although, I also kind of like the A WP Life plugin idea too as it seems to be able to search for tags or something that could be applied to the photos. I assume it’s only a paid plugin, correct?

    I don’t think a regular gallery filter will work because each image/title will be a link to its own page with more info about a project. So, while creating a tag for each image in the gallery during the process may not be too bad, having 80+ filters at the top of the page doesn’t seem logical. It would be almost like having links that take you to an anchor point on the page.

    Moderator bcworkz

    (@bcworkz)

    Are you trying to replicate the text search that browsers already provide (usually via Ctrl-F)? You probably cannot use the standard search form because it’ll have the same ID attribute as the site search form, which causes the HTML to fail validation.

    Since the search does not need to involve sending data via HTTP, you simply need a text input field that has a unique ID attribute. The search is best done via JavaScript. You ought to be able to find any number of examples through an internet search. To highlight found text like Ctrl-F does, you are really doing a search and replace in order to insert span tags that will style the found text to have a different background color.

    Thread Starter dsb0328

    (@dsb0328)

    Sort of. I will have 80 images with unique titles and/or tags for each of the images that will basically be a descriptor for the image. I want the user to be able to search for a subject that they are interested in, and the result will be the title/tag/descriptor and the image of their search. Then they can click on the image or title and be taken to a page that has the full story about the image/title they just found. Does that make sense?

    Moderator bcworkz

    (@bcworkz)

    Sure. As long as the search term is to be found in the page’s HTML, JavaScript can find it. In fact, if you were to implement image tags as element class names, JavaScript can locate such elements quickly with getElementsByClassName(). Once such an element is found, script can scroll the page to its position and change its CSS styling to highlight it as a matching element.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Search a single page’ is closed to new replies.