Обновлено: В следующем примере отобразится кнопка, связанная со случайным продуктом:
// Get a random product id (array)
$random_product_array = get_posts( array( 'posts_per_page' => 1, 'post_type' => 'product', 'orderby' => 'rand', 'fields' => 'ids' ) );
$random_product_id = reset($random_product_array); // Get the random product ID
// Display a linked button to the random product
echo '<a href="' . get_permalink($random_product_id) . '" class="button alt">' . __("Go to a random product") . '</a>';
Вы даже можете встроить это в функцию шорткода, например:
add_shortcode( 'random_product_button', 'shortcode_random_product_button' );
function shortcode_random_product_button( $atts ) {
// Get a random product id (array)
$random_product_array = get_posts( array( 'posts_per_page' => 1, 'post_type' => 'product', 'orderby' => 'rand', 'fields' => 'ids' ) );
$random_product_id = reset($random_product_array); // Get the random product ID
// Display a linked button to the random product
return '<a href="' . get_permalink($random_product_id) . '" class="button alt">' . __("Go to a random product") . '</a>';
}
Код помещается в файл function.php вашей активной дочерней темы (или активной темы).Протестировано и работает.
ИСПОЛЬЗОВАНИЕ:
1) Внутри страницы или публикации текстового редактора (или текстового виджета) :
[random_product_button]
2) На php шаблоне или коде:
echo do_shortcode("[random_product_button]");