[英]Is there any way to get yoast title inside page using their variable ( i.e. %%title%%)
我需要創建一個函數,以便我可以在 wordpress 常規頁面之外的任何頁面中使用它。 我的意思是 wp_head() 不會放在那里。 我需要它是有目的的。
目的是用於 amp(ampproject.org) 頁面,我不能在其中使用任何 css 或 js。 這就是為什么我需要這個。 我需要在 wp 標題處放置一個函數,以便將 yoast 標題放置在那里。
我需要這樣的東西:
function yoastVariableToTitle($variable){
return yoast_vaialble_to_show_title($variable);
}
默認情況下Yoast需要的格式
%%title%% %%page%% %%sep%% %%sitename%%
,而在商店wp_postmeta
下表_yoast_wpseo_title
關鍵。
只獲取頁面/帖子的標題:
function yoastVariableToTitle($post_id) {
$yoast_title = get_post_meta($post_id, '_yoast_wpseo_title', true);
$title = strstr($yoast_title, '%%', true);
if (empty($title)) {
$title = get_the_title($post_id);
}
return $title;
}
SEO 標題可能有 2 種可能性
案例 I :管理員在SEO 標題字段中輸入%%title%% %%page%% %%sep%% %%sitename%%
然后上面的代碼將返回 Post/Page default title 。
案例二:管理員在SEO 標題字段中輸入My Custom Title %%page%% %%sep%% %%sitename%%
然后上面的代碼將返回我的自定義標題。
要獲取頁面/帖子的完整元標題:
function yoastVariableToTitle($post_id) {
$yoast_title = get_post_meta($post_id, '_yoast_wpseo_title', true);
$title = strstr($yoast_title, '%%', true);
if (empty($title)) {
$title = get_the_title($post_id);
}
$wpseo_titles = get_option('wpseo_titles');
$sep_options = WPSEO_Option_Titles::get_instance()->get_separator_options();
if (isset($wpseo_titles['separator']) && isset($sep_options[$wpseo_titles['separator']])) {
$sep = $sep_options[$wpseo_titles['separator']];
} else {
$sep = '-'; //setting default separator if Admin didn't set it from backed
}
$site_title = get_bloginfo('name');
$meta_title = $title . ' ' . $sep . ' ' . $site_title;
return $meta_title;
}
希望這可以幫助!
你有非常困難的決定。 我有一個更簡單的解決方案:
function get_post_title( WP_Post $post ): string {
$yoast_title = get_post_meta( $post->ID, '_yoast_wpseo_title', true );
if ( empty( $yoast_title ) ) {
$wpseo_titles = get_option( 'wpseo_titles', [] );
$yoast_title = isset( $wpseo_titles[ 'title-' . $post->post_type ] ) ? $wpseo_titles[ 'title-' . $post->post_type ] : get_the_title();
}
return wpseo_replace_vars( $yoast_title, $post );
}
並進行說明:
function get_post_description( WP_Post $post ): string {
$yoast_post_description = get_post_meta( $post->ID, '_yoast_wpseo_metadesc', true );
if ( empty( $yoast_post_description ) ) {
$wpseo_titles = get_option( 'wpseo_titles', [] );
$yoast_post_description = isset( $wpseo_titles[ 'metadesc-' . $post->post_type ] ) ? $wpseo_titles[ 'metadesc-' . $post->post_type ] : '';
}
return wpseo_replace_vars( $yoast_post_description, $post );
}
我在插件中使用的解決方法是使用 class-frontend.php(yoast 的類)中的函數。 它在循環之外工作,只需給它一個帖子的 id:
function convert_yoast_title ($post_id) {
$string = WPSEO_Meta::get_value( 'title', $post_id );
if ($string !== '') {
$replacer = new WPSEO_Replace_Vars();
return $replacer->replace( $string, get_post($post_id) );
}
return ''; // if not found - returns empty string
}
只需您可以:
$title = wp_title( '-', false, '' );
從 Yoast 14.0 開始,這變得容易多了。 您可以使用以下代碼獲取當前頁面的標題:
YoastSEO()->meta->for_current_page()->title;
來源: https : //developer.yoast.com/blog/yoast-seo-14-0-using-yoast-seo-surfaces/
我最終得到了 Raunak Gupta 的腳本,我必須修改它才能在主頁標題中正確顯示博客描述。 這是修改后的變體:
function yoastVariableToTitle($post_id) {
$yoast_title = get_post_meta($post_id, '_yoast_wpseo_title', true);
$title = strstr($yoast_title, '%%', true);
if ( !is_front_page() ) {
$title = strstr( $yoast_title, '%%', true );
} else {
$title = get_bloginfo( 'description' );
}
$wpseo_titles = get_option('wpseo_titles');
$sep_options = WPSEO_Option_Titles::get_instance()->get_separator_options();
if (isset($wpseo_titles['separator']) && isset($sep_options[$wpseo_titles['separator']])) {
$sep = $sep_options[$wpseo_titles['separator']];
} else {
$sep = '-'; //setting default separator if Admin didn't set it from backed
}
$site_title = get_bloginfo('name');
$meta_title = $title . ' ' . $sep . ' ' . $site_title;
return $meta_title;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.