Home>

We have created a site with WordPress and on the top page
The latest 3 external WordPress articles are obtained via RSS.

Currently password protected articles will be displayed, so
I want to display 3 other articles without displaying protected articles.

The RSS acquisition was written referring to the following page.
https://innervisions.info/wordpress/rss-read/

<? php
include_once (ABSPATH. WPINC. '/feed.php');
$rss = fetch_feed ('http: // ●●/feed /');
if (! is_wp_error ($rss)) {
$maxitems = $rss->get_item_quantity (3);
$rss_items = $rss->get_items (0, $maxitems);
}
?>
<? php if (! empty ($maxitems)):?>
<? php if ($maxitems == 0) echo '<ul><li>There was no RSS data</li></ul>';
else
foreach ($rss_items as $item):?>
<div><span>
<a href = "<? php echo $item->get_permalink ();?>">
<? php
$first_img = '';
if (preg_match ('/<img.+?src=[\'"]([^\'"]+?)[\'"].*?>/msi',
$item->get_content (), $matches)
) {$first_img = $matches [1];}?>
<? php if (! empty ($first_img)):?>
<img src = "<? php echo esc_attr ($first_img);?>" alt = "" />
<? php endif;?>
</a>
</span>
<p><? php echo $item->get_date ('Y/m/d');// Date?></p><p>
<a href = "<? php echo $item->get_permalink ();?>">
<? php echo $item->get_title ();// Title?>
</a></p></div>
<? php endforeach;?>
<? php endif;?>

I understand that it's better to exclude only those whose titles are "Protected" as a method.
I did not know how to write for PHP beginners, so I asked a question here.

  • Answer # 1

    Did you check it after logging out?


    Addition:

    Increase the number of acquisitions

    Check if the specific title is included in the article title and continue if it is included

    Make a count variable and break when 3 foreach outputs

  • Answer # 2

    I checked a little, but the word "protected" is displayed only on the page of the site, and since there is no "protected" character in RSS, it is not possible to exclude it. Is it possible?
    I don't think there is any data in $rss that distinguishes between password protection and non-password protection.

  • Answer # 3

    If excluded, there may be fewer than 3 cases, so
    I got about 10 items and moved to stop when 3 items were displayed.

Related articles