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"

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

1 Answer 1

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

7
  • Thanks, is true "title" => __('hi', 'theme-textdomain') . ' ' . get_the_title(get_the_ID()), only : from the end // get_the_title // I want to censor / delete part of the text and not display it That is, if this is my title : "how are you ...." I want it to be like this: "hi how are"
    – sa eu
    Commented Dec 21, 2020 at 12:36
  • @saeu you mean shorten the text? by what rule..?
    – honk31
    Commented Dec 21, 2020 at 13:53
  • wp_trim_words @honk31
    – sa eu
    Commented Dec 21, 2020 at 13:58
  • @saeu edited the answer
    – honk31
    Commented Dec 21, 2020 at 17:09
  • Thanks dear friend
    – sa eu
    Commented Dec 21, 2020 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.