2

I have added microdata into every article manually and it works fine. Now I want to insert them into the latest articles module.

To override the module in my template and I added:

<li itemscope itemtype="https://schema.org/Article">
    <span itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
        <span itemprop="name" content="...your organization name..."></span>
        <span itemprop="logo" itemscope itemtype="https://schema.org/ImageObject">
            <link itemprop="url" href="/images/KS.jpg">
            <meta itemprop="width" content="50">
            <meta itemprop="height" content="50">
        </span>
    </span>
    <span itemprop="author" content="...your name..."></span>` 

and it works for organization, organization image - logo and author.

Next I added:

<meta itemprop="datePublished" content="<?php echo JHtml::_('date', $item->publish_up, JText::_('Y-m-dTh:m:s')); ?> " >

and it works for published date.

Now I have a problem when I try to add articles image markup. I added:

<?php $images = json_decode($item->images); ?>
<span itemprop="image" itemscope itemtype="https://schema.org/ImageObject">
    <link itemprop="url" href="<?php echo htmlspecialchars($images->image_intro); ?>" alt="<?php echo htmlspecialchars($images->image_intro_alt); ?>">
    <meta itemprop="width" content="150">
    <meta itemprop="height" content="100">
</span>

and it works fine, but only for intro images through article image and links tab. I have my images in my html (not in image and links tab) and I want to extract image url from html. I have more than 400 articles and I prefer to extract the images from the html.

0

2 Answers 2

3

I would have preferred to just suggest some touch ups to your snippet, but there is too much for me to address in your answer as a comment and I'd rather explain fully. Instead, I'll post this new answer which will review and clean up your assumed-working coding attempt for the benefit of you and future researchers.

  1. You are declaring $images = json_decode($item->images);, then before ever using it, you are overwriting that same variable with $images = $dom->getElementsByTagName('img');. For this reason, you might as well, never declare it the first time. Perhaps this is a typo while posting. If this is correct: $images = json_decode($item->images);, then you probably mean to use $dom->loadHTML($images->image_intro);

  2. You should try to separate your "processing" from your "displaying" as much as possible for clarity. Rather than generating a dom document and extracting the desired data mid-way through writing your <link> tag, I recommend that you fully interrogate the introtext data, then -- if there is any data to feed into the link tag -- then write your span and link tags. If there is no usable data, I assume you don't need to print the span and link tags.

  3. You don't need the extra overhead of two dom documents. You can perform both getAttribute() calls on the same element in one fell swoop.

  4. If there is no <img> tag in the introtext, then $images[0] will generate a Notice because array element [0] does not exist. Instead of performing a "truthy" check, use isset().

  5. There is no benefit to concatenating '' (an empty string) before or after a variable. Simply omit it -- it is just code bloat. I don't know who is teaching this technique, but I see it every once in a while on Stackoverflow.

All together, transferring the first image's attributes from the introtext to your new tag is as simple as this:

<?php
$dom = new DOMDOcument;
$dom->loadHTML($item->introtext);  // or $images->image_intro  ...whatever you are actually calling it
$images = $dom->getElementsByTagName('img');
if (isset($images[0])) {
    ?>
    <span itemprop="image" itemscope itemtype="https://schema.org/ImageObject">
    <link itemprop="url"
          href="<?php echo $images[0]->getAttribute('src'); ?>"
          alt="<?php echo $images[0]->getAttribute('alt'); ?>">
    <?php
}
0
1

The solution to my question. I think is valid:

<?php $images = json_decode($item->images);
?>
<span itemprop="image" itemscope itemtype="https://schema.org/ImageObject">
<link itemprop="url" href="<?php // Create new DOMDocument object.
$dom = new DOMDOcument;

// Load article text into DOMDocument.
$dom->loadHTML($item->introtext);

// Get all images.
$images = $dom->getElementsByTagName('img');

// If article contains an image, get its URL.
$imageSrc = $images[0] ? $images[0]->getAttribute('src') : null;

// Output the image.
if ($imageSrc !== null)
{
 echo '' . $imageSrc . '>';
}
?>" alt="<?php // Create new DOMDocument object.
$dom = new DOMDOcument;

// Load article text into DOMDocument.
$dom->loadHTML($item->introtext);

// Get all images.
$images = $dom->getElementsByTagName('img');

// If article contains an image, get its URL.
$imageSrc = $images[0] ? $images[0]->getAttribute('alt') : null;

// Output the image.
if ($imageSrc !== null)
{
 echo '' . $imageSrc . '>';
}
?>">```

Your Answer

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

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