Site logo

[Support request] Trying to insert navigation search in top bar (instead of in navigation)

Home Forums Support [Support request] Trying to insert navigation search in top bar (instead of in navigation)

Home Forums Support Trying to insert navigation search in top bar (instead of in navigation)

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2552226
    Lars

    Hi!

    Using this to create navigation search short code:

    function generate_navigation_search_shortcode() {
      // Call the theme's built-in function
      $navigaion_button = generate_do_navigation_search_button(); 
      $search_form = generate_navigation_search();
      $output = $navigaion_button .$search_form;
    
       echo $output;
     
    }
    add_shortcode( 'gp_navigation_search', 'generate_navigation_search_shortcode' );

    Added this inside of a container of generateblocks in a custom created top bar element hook. Web page: https://nyatidensmedicin.se

    It’s adding itself outside of the container for some reason. How can I make it be added inside of the top bar container?

    #2552514
    David
    Staff
    Customer Support

    Hi there,

    shortcodes should return their data and not echo them.
    However those functions will need to printed to the screen, so in that case you can use object buffering.
    Change the shortcode to:

    function generate_navigation_search_shortcode() {
      // start object buffer
      ob_start();
      // Call the theme's built-in function
      $navigaion_button = generate_do_navigation_search_button(); 
      $search_form = generate_navigation_search();
      $output = $navigaion_button .$search_form;
      echo $output;
      // return buffer
      return ob_get_clean();
    }
    add_shortcode( 'gp_navigation_search', 'generate_navigation_search_shortcode' );
    #2553634
    Lars

    I am not exactly sure how this code made it work, but it did! I hope to level my PHP-game by the end of the year, or the next one!

    I am using an overlay search with another code you have been giving me before in another earlier thread: https://generatepress.com/forums/topic/moving-search-field-and-icon/#post-1540468 but it turns out a bit funny now, this particular time.

    You can see the results here:
    Shared Image

    Or directly through the website (if you click the search icon in the upper right corner):
    https://nyatidensmedicin.se

    There seem to be some kind of duplication happening and I can’t locate it in the source.

    #2553912
    David
    Staff
    Customer Support

    Ah, its because the original search form is still being output.
    Instead make your shortcode just output the button.

    function generate_navigation_search_shortcode() {
      // start object buffer
      ob_start();
      // Call the theme's built-in function
      $navigation_button = generate_do_navigation_search_button(); 
      echo $navigation_button;
      // return buffer
      return ob_get_clean();
    }
    add_shortcode( 'gp_navigation_search', 'generate_navigation_search_shortcode' );

    And then style the original form.

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