• larahe

    (@larahe)


    what is the correct way to link a search

    i would like to link text to search which should be /?s=search-term

    but how do i do this to work on any domain name if i change the page url? (not having to go back)

    for example ./?s=test or /./?s=test

Viewing 12 replies - 1 through 12 (of 12 total)
  • Moderator bcworkz

    (@bcworkz)

    The answer depends on where you want the link to appear. If PHP is appropriate, you could do something like:
    echo get_site_url( null, '?s=search-term');

    Thread Starter larahe

    (@larahe)

    The answer depends on where you want the link to appear
    – in a page or post

    If PHP is appropriate, you could do something like:
    echo get_site_url( null, ‘?s=search-term’);
    – do this where?

    Moderator bcworkz

    (@bcworkz)

    Where to place the code depends on whether you use a classic or block theme. For classic themes, PHP can go on the post or page’s template file. Where in the file? Where ever you want the link to appear. For block themes, introducing PHP code is more involved. You would create a custom pattern. The linked article focuses on the home page, but patterns can be applied to any template.

    Thread Starter larahe

    (@larahe)

    im using 2024, i think thats a block theme

    just to be clear im trying to add a link to text that will trigger a search. but the url needs to be excluded so it will work on any domain name link: ?my domian /?s=search-term

    Moderator bcworkz

    (@bcworkz)

    Any domain name, but the domain of the current site, yes? If not the current site, then how would code know which domain to use?

    Yes, 2024 is a block theme. You cannot place PHP code in a block theme template. I’m not too familiar with modifying block theme templates, but I believe you’d need to register a custom pattern that uses appropriate PHP, then reference the pattern on the post or page’s template?

    Thread Starter larahe

    (@larahe)

    so again. i do not want to add links with a fixed domain name as the domain can change if the site if migrated to a new domain name which would break all the links

    Moderator bcworkz

    (@bcworkz)

    Yes, get_site_url() returns the URL of the current site, whatever its domain name is. It comes from the value entered in site and home URL settings. If the setting values change during migration, the function’s URL changes accordingly.

    Thread Starter larahe

    (@larahe)

    so do i put this in the link?? ?get_site_url()?s=test

    does not work

    • This reply was modified 2 months, 3 weeks ago by larahe.
    Moderator bcworkz

    (@bcworkz)

    echo get_site_url( null, '?s=test'); somewhere on a PHP template should work.

    Thread Starter larahe

    (@larahe)

    php template?

    my question was about making a text link? i dont know php

    Moderator bcworkz

    (@bcworkz)

    I don’t know if you use a classic or block theme. Introducing PHP in a block theme is rather convoluted. Classic theme templates are PHP based and are easier to introduce custom PHP code into. Even with the more accessible (regarding PHP) classic templates, you’ll need to determine where the search link should go in relation to other page content. Once you find a likely spot, you could do something like:
    <a href="<?php echo get_site_url( null, '?s=Foo'); ?>">Search for "Foo"</a>

    Be mindful of existing PHP code that’s demarcated with <?php ?>. The above suggestion belongs outside of any existing PHP code, it should be placed where normal HTML already exists. If you need to add it within existing PHP code, break out of it with ?>, insert the above code, then restart PHP afterwards with <?php. You cannot break out just anywhere, do it after a PHP terminal ;

    If you use a block theme, I believe custom PHP needs to be introduced via a pattern.

    Thread Starter larahe

    (@larahe)

    i already told you theme is 2024

    <a href="<?php echo get_site_url( null, '?s=Foo'); ?>">Search for "Foo"</a>

    This block contains unexpected or invalid content.

    i dont know how your result is expected to work as a link on text though the site

Viewing 12 replies - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.