Using an asset directive inside the_posts_navigation

I am trying to simply replace WP default navigation text (next/previous) with SVG chevrons icons

But this does not work :

the_posts_navigation(
    array(
        'prev_text' => __('<img src="@asset('images/chevron-left.svg')">'),
        'next_text' => __('<img src="@asset('images/chevron-right.svg')">'),
        'screen_reader_text' => __('Posts navigation')
    )
  );

What would be the proper way to get the dist path with @asset inside the WP navigation function?

Many thanks.

Try asset_path('images/chevron-right.svg') instead of @asset('images/chevron-right.svg')

I tried it but the PHP is not being transform into a path. It just stays the same when I inspect my page. In fact, any PHP seems rejected in my array. If it’s not possible to use that inside an array, how can I channel this @asset file URL inside a WP function? I could find my local site’s path and add it manually but then the links my staging environment would not function. And vice-versa.

I’m sorry! I am very confused.

Those arguments are expecting a string, and if you just put PHP inside those quotes it will be considered a string and not executed.

No

the_posts_navigation(
    array(
        'prev_text' => __('<img src="<?php asset_path('images/chevron-right.svg') ?>">'),
        'next_text' => __('<img src="<?php asset_path('images/chevron-left.svg') ?>">'),
        'screen_reader_text' => __('Posts navigation')
    )
  );

Yes

the_posts_navigation(
    array(
        'prev_text' => __('<img src="' . asset_path('images/chevron-right.svg') . '">'),
        'next_text' => __('<img src="' . asset_path('images/chevron-left.svg') . '">'),
        'screen_reader_text' => __('Posts navigation')
    )
  );
1 Like

Thanks you so much, I understand much better now.

In my case, the code below worked for me. I needed to add " \app\ " to asset_path function.

the_posts_navigation(
      array(
        'prev_text' => __('<img src="' . \app\asset_path('images/chevron-left.svg') . '">'),
        'next_text' => __('<img src="' . \app\asset_path('images/chevron-right.svg') . '">'),
        'screen_reader_text' => __('Posts navigation')
      )
);

This topic was automatically closed after 42 days. New replies are no longer allowed.