此代码将执行您想要的操作。如果您添加,它将重定向到添加的最新产品?latest_product
到您网站的URL。所以动态链接是http://example.com/?latest_product
function wpse_274155_redirect_latest_product() {
// If URL has ?latest_product query string.
if ( isset( $_GET[\'latest_product\'] ) ) {
// Get latest product.
$latest_products = get_posts( array(
\'posts_per_page\' => 1,
\'post_type\' => \'product\',
\'no_found_rows\' => true,
\'orderby\' => \'date\',
\'order\' => \'DESC\',
) );
// Make sure product exists.
if ( isset( $latest_products[0] ) ) {
// Redirect to product.
wp_redirect( get_permalink( $latest_products[0] ) );
exit();
}
}
}
add_action( \'template_redirect\', \'wpse_274155_redirect_latest_product\' );