php 如何在WordPress中获得帖子标题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19792123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get post title in WordPress?
提问by ??m?? M???m??
I have WordPress site. So I make code to convert title of post to english from arabic but the code get the title of post from WordPress.
我有 WordPress 网站。所以我编写代码将帖子标题从阿拉伯语转换为英语,但代码从 WordPress 获取帖子标题。
I use plugin All in one SEO pack. So I add title to plugin on every page, not title of post but title in input All in one SEO pack.
我在一个 SEO 包中使用插件。所以我在每个页面上为插件添加标题,不是帖子标题,而是输入中的标题全部在一个 SEO 包中。
I want get the title of All in one SEO pack to convert it.
我想获得 All in one SEO pack 的标题来转换它。
Here is code for convert title in functions.php
:
这是转换标题的代码functions.php
:
function arb2en_title($post=0)
{
$text = get_the_title($_aioseop_title);
/*
function arb2en_title($post=0)
{
$text = get_the_title($post);
*/
$arb_en_map=array(
'?'=>']',
'?'=>'[',
'?'=>'p',
'?'=>'o',
'?'=>'i',
'?'=>'u',
'?'=>'y',
'?'=>'t',
'?'=>'r',
'?'=>'e',
'?'=>'w',
'?'=>'q',
'?'=>'a',
'?'=>'s',
'?'=>'d',
'?'=>'f',
'?'=>'g',
'?'=>'h',
'?'=>'j',
'?'=>'k',
'?'=>'l',
'?'=>';',
'?'=>'\'',
'?'=>'/',
'?'=>'.',
'?'=>',',
'?'=>'m',
'?'=>'n',
'??'=>'b',
'?'=>'v',
'?'=>'c',
'?'=>'x',
'?'=>'z',
'?'=>'Y',
'??'=>'T',
'??'=>'G',
'?'=>'H',
'??'=>'B',
'?'=>'N'
);
foreach($arb_en_map as $key=>$value)
{
$text=preg_replace("/$key/",$value,$text);
}
return htmlentities($text);
}
This code get title of post but I need get title in input All in one SEO pack. How can I do that?
此代码获取帖子标题,但我需要在一个 SEO 包中获取所有输入中的标题。我怎样才能做到这一点?
回答by Ryan S
It's easy <?php echo get_the_title( $post_id ); ?>
这很简单 <?php echo get_the_title( $post_id ); ?>
Hope that helps
希望有帮助
回答by Code Poet
This is old question but I am answering it because it is appearing in Google search results.
这是一个老问题,但我正在回答它,因为它出现在 Google 搜索结果中。
All in one SEO pack use custom fields to store date in database. Assuming that the custom field name for title field of All in one SEO plugin is “_aioseop_title”. (please confirm first if plugin is using custom filed name other than _aioseop_title then replace it with that name).
多合一 SEO 包使用自定义字段在数据库中存储日期。假设All in one SEO插件的title字段的自定义字段名称为“_aioseop_title”。(请先确认插件是否使用 _aioseop_title 以外的自定义文件名,然后将其替换为该名称)。
So to get value of title field of seo plugin use this line of code in your function in functions.php:
因此,要获取 seo 插件的 title 字段的值,请在函数中的functions.php 中使用这行代码:
get_post_meta( $post_id, $key, $single );
Explanation…
解释…
$post_id:
$post_id:
To get post id you can use:
要获取帖子 ID,您可以使用:
global $post;
$post_id = $post->ID;
$key:
$键:
$key = “_aioseop_title”; //check if plugin is using same name for title field
$single:
$单:
$single = true; // if true then it will return single value otherwise all values of _aioseop_title in an array.
Ref: https://codex.wordpress.org/Function_Reference/get_post_meta
参考:https: //codex.wordpress.org/Function_Reference/get_post_meta
:: I have not tested it so in case of any problem leave a comment I will be happy to help.
:: 我还没有测试过,所以如果有任何问题,请发表评论,我很乐意提供帮助。