Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get post title in WordPress?

Tags:

php

seo

wordpress

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.

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.

I want get the title of All in one SEO pack to convert it.

Here is code for convert title in 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?

like image 739
ẪĦmẻḓ MøĦẫmẻḓ Avatar asked Nov 29 '22 00:11

ẪĦmẻḓ MøĦẫmẻḓ


2 Answers

It's easy <?php echo get_the_title( $post_id ); ?>

Hope that helps

like image 80
Ryan S Avatar answered Dec 01 '22 14:12

Ryan S


This is old question but I am answering it because it is appearing in Google search results.

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).

So to get value of title field of seo plugin use this line of code in your function in functions.php:

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

Explanation…

$post_id:

To get post id you can use:

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

:: I have not tested it so in case of any problem leave a comment I will be happy to help.

like image 40
Code Poet Avatar answered Dec 01 '22 12:12

Code Poet