admin管理员组文章数量:1424437
I am creating a dynamic page, in a WordPress environment. The page is created by a default standard WordPress page and then using shortcode. Combined with URL parameters in order to retrieve the necessary data.
The problem is, this page, including the URL parameters, is "shareable" to facebook and other social media. Thus the description that comes up is too generic. I assume this is because the default page, on which the shortcode resides, is also generic. That is what I want to change based on the data I am retrieving for display.
I have tried the code below and a few other variations, with no luck. In fact the filter is not even executed. Tried embedding the filter into an add_action call..the action call is done, but the filter remains executed.
add_filter('wp_title','ChangeTitle', 10, 2);
function ChangeTitle($title, $id)
{
if ( $title== 'Details' )
{
$BusinessName = $_GET['Name'];
return $BusinessName;
}
return $title;
}
Does anyone have a clever way to do this? part two of the question, would be how to assign a logo to the share.... but one thing at a time.
I am creating a dynamic page, in a WordPress environment. The page is created by a default standard WordPress page and then using shortcode. Combined with URL parameters in order to retrieve the necessary data.
The problem is, this page, including the URL parameters, is "shareable" to facebook and other social media. Thus the description that comes up is too generic. I assume this is because the default page, on which the shortcode resides, is also generic. That is what I want to change based on the data I am retrieving for display.
I have tried the code below and a few other variations, with no luck. In fact the filter is not even executed. Tried embedding the filter into an add_action call..the action call is done, but the filter remains executed.
add_filter('wp_title','ChangeTitle', 10, 2);
function ChangeTitle($title, $id)
{
if ( $title== 'Details' )
{
$BusinessName = $_GET['Name'];
return $BusinessName;
}
return $title;
}
Does anyone have a clever way to do this? part two of the question, would be how to assign a logo to the share.... but one thing at a time.
Share Improve this question asked Jun 8, 2019 at 6:38 Debbie KurthDebbie Kurth 4323 silver badges14 bronze badges 5- What your shortcode looks like? Do you need to change the title or the description? – Max Yudin Commented Jun 8, 2019 at 7:28
- Simple shortcut that just calls a function with just the default parameters. I need to change the title and the description, but I can live with just the title. [LISTING_DETAILS] – Debbie Kurth Commented Jun 8, 2019 at 7:43
- The title and the description will be based on the data I am retrieving from the database. It is not fixed. In essence, it could be hundreds of different pages, since it is dynamically created. – Debbie Kurth Commented Jun 8, 2019 at 7:48
- Where are you putting this code? If it's never getting called, then it might be in the wrong place. Also, that filter has to be supported by whatever theme you're using... if it doesn't run the filter, your code will never be run – Paul G. Commented Jun 8, 2019 at 11:22
- Paul - if I switch it to a add_action, it is called. I am beginning to suspect the SEO plugins are stopping it. – Debbie Kurth Commented Jun 8, 2019 at 16:42
2 Answers
Reset to default 1So depending on your theme, you'll need to take a different approach. By the sounds of it, you're using a theme that adds WordPress theme support for title-tag
e.g. twentynineteen. In this case, your code would look like this:
function add_dynamic_title( $aTitleParts ) {
if ( isset( $aTitleParts[ 'title' ] ) && $aTitleParts[ 'title' ] == 'Details' ) {
$aTitleParts[ 'title' ] = $_GET[ 'Name' ];
}
return $aTitleParts;
}
add_filter( 'document_title_parts', 'add_dynamic_title', 20 );
This filter uses an array where it contains, amongst other things, a key title
and perhaps tagline
. So in this case you set the array key value for title
to whatever you'd like and return the whole array.
The following code gives you the ability to use [facebook_title]
shortcode in the title and [facebook_description]
respectively.
If you use any of Yoast SEO, SEOPress or All in One SEO Pack plugins, add these shortcodes primarily there.
Remove lines activating shortcodes in unused plugins.
// get your special title
function my_facebook_title() {
return get_post_meta( get_the_ID(), 'post_meta_facebook_title', true );
}
// get your special description
function my_facebook_description() {
return get_post_meta( get_the_ID(), 'post_meta_facebook_description', true );
}
// add hooks to shortcodes
add_shortcode( 'listing_title', 'my_facebook_title' );
add_shortcode( 'listing_description', 'my_facebook_description' );
// activate shortcode in the post headline
add_filter( 'the_title', 'do_shortcode' );
// activate shortcode for the single post <title>
add_filter( 'single_post_title', 'do_shortcode' );
//// Plugins (optional)
// activate shortcodes in SEOPress
add_filter( 'seopress_titles_title', 'do_shortcode' );
add_filter( 'seopress_titles_desc', 'do_shortcode' );
// activate shortcodes in Yoast SEO
add_filter( 'wpseo_title', 'do_shortcode' );
add_filter( 'wpseo_metadesc', 'do_shortcode' );
// activate shortcodes in All in One SEO Pack
add_filter( 'aioseop_title', 'do_shortcode' );
add_filter( 'aioseop_description', 'do_shortcode' );
Update: SEOPress and All in One SEO Pack support added.
本文标签: phpWordPress Environment Dynamic Page using shortcodehow to change the page name for sharing
版权声明:本文标题:php - WordPress Environment: Dynamic Page using shortcode - how to change the page name for sharing 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745422803a2657977.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论