这段代码运行良好:
function register_custom_patterns() {
$vari = 5;
register_block_pattern(
'twentytwentyfive/asif', // The pattern slug
array(
'title' => __( 'Test Pattern', 'twentytwentyfive' ),
'description' => __( 'A custom test pattern for the theme.', 'twentytwentyfive' ),
'content' => file_get_contents( get_template_directory_uri() . '/patterns/asif.php?id='. $vari), // Path to your custom PHP file
)
);
}
add_action( 'init', 'register_custom_patterns' );
但它不起作用:
function register_custom_patterns() {
$vari = "Send me";
register_block_pattern(
'twentytwentyfive/asif', // The pattern slug
array(
'title' => __( 'Test Pattern', 'twentytwentyfive' ),
'description' => __( 'A custom test pattern for the theme.', 'twentytwentyfive' ),
'content' => file_get_contents( get_template_directory_uri() . '/patterns/asif.php?id='. $vari), // Path to your custom PHP file
)
);
}
add_action( 'init', 'register_custom_patterns' );
错误:
http://localhost/asif_wp/wp-content/themes/twentytwentyfive/patterns/asif.php?id=Send me): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in C:\xampp\htdocs\asif_wp\wp-content\themes\twentytwentyfive\functions.php on line 168
简而言之:
参数
$vari = 5; // Working
$vari = "Show me"; //Error
为什么以及如何解决?
值 Send me 包含空格。当作为 URL 的一部分传递时,空格不会被编码,从而导致 URL 无效。空格必须替换为 %20(空格的 URL 编码)。