[英]How to catch a URL parameter and display a custom page title in wordpress
As a novice , I asked this to lots of paid developers who either said No or quoted exorbitant price or said Not achievable . 作为一个新手,我问了很多有偿开发商,他们要么说“ 不” ,要么说价格过高,或者说“ 无法 实现” 。
But I have achieved few thing on my site by going through all the interactions here. 但是,通过这里的所有交互,我在网站上几乎没有取得什么成就。 So thought of taking a second opinion.
因此想到了第二点意见。
A custom gateway payment plugin returns to Woocommerce Thank you page , if the transaction is successful but returns to a normal wordpress page -failed-payment, if payment is unsuccessful. 如果交易成功,则自定义网关付款插件将返回Woocommerce谢谢页面,但如果付款失败,则会返回到正常的wordpress页面-failed-payment。
For the unsuccessful payment the return URL is : 对于失败的付款,返回URL为:
mysite.com/failed-payment/?b_name=Jack.
On the failed-payment page, I want to display a personalised Page Title as Sorry, Jack, Your payment was unsuccessful. 在失败的付款页面上,我想显示个性化的页面标题,对不起,杰克,您的付款失败。
Creating a new page template or modifying page template file is not an option. 创建新页面模板或修改页面模板文件不是一种选择。 Has to be done via theme function.
必须通过主题功能来完成。 php .
php 。
To write the page title , I have thought of the following to be written in theme function. 为了写页面标题,我想到了以下主题功能。 php :
的PHP :
function assignPageTitle( $title ){
if ( is_page(3062) ) {
$title = sprintf("Sorry, %s, Your payment was unnsuccessful", b_name);
}
return $title;
}
add_filter('wp_title', 'assignPageTitle');
Is it achievable or impossible ? 是可以实现的还是不可能的? If achievable , how should I approach doing it??
如果可以实现,我应该如何做呢?
Thanks. 谢谢。
To catch b_name from URL parameter and assign it to php variable is as following: 要从URL参数中捕获b_name并将其分配给php变量,如下所示:
$b_name = $_GET['b_name'];
Now you can use it in your code eg: 现在,您可以在代码中使用它,例如:
$title = sprintf("Sorry, %s, Your payment was unnsuccessful", $b_name);
Also I will modify your code a little bit to alter title only when b_name is available (note: I have not tested your WP code so using as it is:) 另外,我将稍微修改您的代码以仅在b_name可用时更改标题(注意:我尚未测试您的WP代码,因此按原样使用:)
function assignPageTitle( $title ){
if ( is_page(3062) && isset($_GET['b_name']) ) {
$b_name = $_GET['b_name'];
$title = sprintf("Sorry, %s, Your payment was unnsuccessful", $b_name);
}
return $title;
}
add_filter('wp_title', 'assignPageTitle');
See, how I have added condition isset() for GET parameter, so that it tries to alter page title only when ?b_name= is available in the url. 请参阅,我如何为GET参数添加条件isset(),以便仅当URL中有?b_name =时,它才尝试更改页面标题。
By below code put into your theme function.php you can modify titles of your page without change / modify any template files: 通过在主题函数.php中添加以下代码,您可以修改页面标题,而无需更改/修改任何模板文件:
//modify page title:
function assignPageTitle($title){
$title = $_GET['b_name'];
if ( is_page(3062) ) {
return $title;
}
else{
return $title;
}
}
add_filter('pre_get_document_title', 'assignPageTitle');
//Modify entry title
add_filter( 'the_title', 'suppress_title', 10, 2 );
function suppress_title($title, $id = null){
$b_name = $_GET['title'];
if ( is_page(3062) ) {
$title = sprintf("Sorry, %s, Your payment was unnsuccessful", $b_name);
}
return $title;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.