最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Add text to the first "title"=>get_the_title($post->ID), and shorten

programmeradmin0浏览0评论

I use this code to display the title :

 $imageAttr = array(
                "class"=>"alignnone size-medium",
                "alt"=>get_the_title($post->ID),
                **"title"=>get_the_title($post->ID),**
        );

Now I want to add a text the title, for example "hi" And shorten a few words from the end of the title

That is, if this is my title :

"how are you"

I want it to be like this: "hi how are"

I use this code to display the title :

 $imageAttr = array(
                "class"=>"alignnone size-medium",
                "alt"=>get_the_title($post->ID),
                **"title"=>get_the_title($post->ID),**
        );

Now I want to add a text the title, for example "hi" And shorten a few words from the end of the title

That is, if this is my title :

"how are you"

I want it to be like this: "hi how are"

Share Improve this question edited Dec 17, 2020 at 13:12 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Dec 17, 2020 at 12:39 sa eusa eu 33 bronze badges 3
  • i guess, that would be "title" => __('hi', 'theme-textdomain') . ' ' . get_the_title(get_the_ID()),..? the __()is a function for translation purposes, 'theme-textdomain' would be your theme text domain, followed by a single space as a separator for the title, that is returned by get_the_title(). and since you use these wordpress function, you also could use get_the_ID() as it also returns the $post->ID without accessing the $post variable directly.. – honk31 Commented Dec 17, 2020 at 14:07
  • I did not catch – sa eu Commented Dec 17, 2020 at 14:58
  • @honk31 Add this as an answer, not a comment. It seems to answer the question, and it'll be a lot easier to format the code for readability. – Pat J Commented Dec 18, 2020 at 3:49
Add a comment  | 

1 Answer 1

Reset to default 0

there are many ways to do this, but frankly, this is not a wordpress question it's a php question. here is your code doing what you asked for

$imageAttr = [
    "class" => "alignnone size-medium",
    "alt" => get_the_title(get_the_ID()),
    "title" => __('hi', 'theme-textdomain') . ' ' .  wp_trim_words(get_the_title(get_the_ID()), 2),
];

$variable = [] is just a shorthand for Array()

get_the_ID() is the same as $post->ID but without the use of global variable $post. well it is called inside that function, but you don't have to define it first. because calling $post without the prior global $post; might cause issues elsewhere. simply use the build in wordpress functions and you should be fine. reference

__('hi', 'theme-textdomain') is a function, that let's you translate your theme. 'hi' is simply the string, that is returned from that function, 'theme-textdomain' is the domain of the set, thr string is saved in. this quite old article is still a good read on how to do it properly.

last bit i added is the . ' ' . that is the space between your custom string and the title of the post, . is simply the php glue of multiple strings. you could have added that space to your custom string in the previous function, but when it comes to the translation part, this can easily be forgotten, so i added it separately.

and as a bonus, here is another method:

"title" => sprintf('%1$d %2$d', __('hi', 'theme-textdomain'), wp_trim_words(get_the_title(get_the_ID()), 2))

it's combining the 2 strings via php sprintf function. same same.. but different..

"title" => sprintf(__('hi %d', 'theme-textdomain'), wp_trim_words(get_the_title(get_the_ID()), 2))

yet another one. sprintf() documentation here

发布评论

评论列表(0)

  1. 暂无评论