如何在WordPress中获取文章标题?

6

我有一个WordPress网站。所以我编写了代码,将文章标题从阿拉伯语转换为英语,但是代码从WordPress获取文章的标题。

我使用插件“All in one SEO pack”。因此,我在每个页面上添加插件的标题,而不是文章标题,而是“All in one SEO pack”输入框中的标题。

我想获取“All in one SEO pack”的标题并将其转换。

以下是在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);
}

这段代码获取文章的标题,但我需要获取All in One SEO Pack中输入的标题。我该怎么做?

1
这是一个适用于波斯语网站的好的SEO想法。 - saber tabatabaee yazdi
2个回答

29

这很简单 <?php echo get_the_title( $post_id ); ?>

希望这有所帮助


1

虽然这是一个旧问题,但我回答它是因为它出现在Google搜索结果中。

All in one SEO包使用自定义字段来存储数据库中的数据。 假设All in one SEO插件标题字段的自定义字段名称为“_aioseop_title”。(请先确认插件是否使用其他名称的自定义文件名,如果是,请将其替换为该名称)。

因此,要获取SEO插件标题字段的值,请在functions.php中的函数中使用以下代码行:

get_post_meta( $post_id, $key, $single );

说明...

$post_id:

获取文章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.

参考: https://codex.wordpress.org/Function_Reference/get_post_meta

:: 我没有测试过,如果有任何问题,请留言,我很乐意帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接