Add a shortcode to archive pages

Hi,

I want to add a search field to archive pages like product-category pages.

So I tried this code snippet:

add_action( ‘x_after_the_content_begin’, ‘add_shortcode’ );
function add_shortcode() {
if(is_archive()){
echo do_shortcode( ‘[wpdreams_ajaxsearchpro id=3]’ );
}
}

But it’s not working. What am I doing wrong?

Kind regards,
Edwin

Hey Edwin,

Thank you for reaching out to us. You should use this code instead for adding a shortcode in archive pages:

add_action( 'x_after_the_excerpt_begin', 'add_shortcode_to_archives' );
function add_shortcode_to_archives() {
	if(is_archive()){
		echo do_shortcode( '[wpdreams_ajaxsearchpro id=3]' );
	}
}

To see all actions and filters in X/Pro please see https://theme.co/apex/forum/t/customizations-actions-and-filters-in-x-pro/208

Hope this helps!

Hi Nabeel,

I tried your code, but it doesn’t work.

On our homepage we have a search field: https://www.rogertrading.nl/

We want the same thing to show up on category archive pages, like this one: https://www.rogertrading.nl/product-categorie/accessoires/?orderby=popularity

Thanks for the help.
Edwin

Hi Edwin,

You can try other hook.

Please try changing your code to this

function add_shortcode_to_archives() {
	if(is_archive()){
		echo do_shortcode( '[wpdreams_ajaxsearchpro id=3]' );
	}
}
add_action( 'x_after_view_global__slider-below', 'add_shortcode_to_archives' );

Hope that helps

Hi Paul,

well the search bar shows when I use tthis code, but I want it to show after the Archive title. With this code is shows right after the header.

So on this page: https://www.rogertrading.nl/product-categorie/accessoires/?orderby=popularity

I want the search bar to appear after the line: ‘Maak jouw scooter helemaal af met accessoires van Roger Trading’

Is there a hook for that?

Kind regards.
Edwin

Hi Edwin,

Sorry, that is not possible as that text is in your content. If you want the search to display below, better add that text in the code as well.

For example.

function add_shortcode_to_archives() {
	if(is_archive()){
                echo '<p>Maak jouw scooter helemaal af met accessoires van Roger Trading</p>';
		echo do_shortcode( '[wpdreams_ajaxsearchpro id=3]' );
	}
}
add_action( 'x_after_view_global__slider-below', 'add_shortcode_to_archives' );

Hope this helps

So how is it possible that the product filters plugin can show the filters there? I just want the search bar to show up just before the filters like on our homepage

I found it!!!

The code should look like this:

function add_shortcode_to_archives() {
if(is_archive()){
echo do_shortcode( ‘[wpdreams_ajaxsearchpro id=3]’ );
}
}
add_action( ‘woocommerce_before_shop_loop’, ‘add_shortcode_to_archives’ );

Glad to hear that. :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.