0

There is the following code that works correctly, that is, the title of the post is displayed and after the comma its category name on the page in the header:

 <?php foreach (HTML_SOBI::getMyCategories($mySobi) as $category) { echo $category['name'];}?></h1> 

How can I display a post with the same title, category ['name'] format in OpenGraph?

The following working code but no category name:

<meta property="og:title" content="'.$mySobi->title.'" />

How can I correctly add the category name after the title, separated by commas? This part of code: <?php foreach (HTML_SOBI::getMyCategories($mySobi) as $category) { echo $category['name'];}?>

1
  • If you are doing Joomla development, please join Joomla Stack Exchange and ask your Joomla-related questions there. Commented Jun 19, 2021 at 23:30

2 Answers 2

0

Just try this

 echo $mySobi->title.' , '.$category;

Apply it like this

$YouTitle = "MY TITLE";
$category = "MY CATEGORY";
$Meta = $YouTitle." , ".$category;

echo'<meta property="og:title" content="'.$Meta.'" />';

Or from an array

$source = array(
    "Title" => "My Title",
    "category" => "My Category"
    );

$meta = $source['Title']." , ".$source['category'];

echo'<meta property="og:title" content="'.$meta.'" />';
Sign up to request clarification or add additional context in comments.

3 Comments

The following working code but no category name: <meta property="og:title" content="'.$mySobi->title.'" /> where to insert your solution?
How can I understand if I don't know where your category is from and what source your title is from?, you have to enter a script of where the category comes from
Apply it like this works super! however, how to call the category name? $category = "MY CATEGORY"; $category = "<?php foreach (HTML_SOBI::getMyCategories($mySobi) as $category) { echo $category['name'];}?>"; ??
0

If I understood the question properly, this might be what you want:

$categories = array_map(function($category) {
    return $category['name'];
}, HTML_SOBI::getMyCategories($mySobi));
$opengraph_meta=' <meta property="og:title" content="'.$mySobi->title.' '.implode(', ', $categories).'" />';

14 Comments

syntax error, unexpected <
Oh, sorry. I wrapped it in an echo
My complete code : $opengraph_meta=' <meta property="og:title" content="'.$mySobi->title.'" />';
Okay, I added your code to the answer
Why, when using this when further on the page, <?php echo $categories; ?> there is no output of the category name, but the value is shown instead ARRAY>?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.