There are times when we publish a file and add multiple images, but how do we make it so that unlogged in users (visitors) can only see one image? After the user logs in, he can see all the pictures in the article. Then this article introduces the following, modify the file code to achieve this effect, so that you can guide the user to register.
First step, in the theme folder funtions.php(path /wp-content/themes/your-theme-name/funtions.php) add the following code to the file (end of file)
function remove_extra_images_from_content() {
// Get the current post ID
$post_id = get_the_ID();
// Get the content of the post
$content = get_the_content(); // Get the content of the post.
// Match all image addresses
preg_match_all('//i', $content, $matches);
// Remove all image tags except the first one
if ( count($matches) > 0 && count($matches[0]) > 1 ) {
for ( $i = 1; $i < count($matches[0]); $i++ ) {
$content = str_replace($matches[0][$i], '', $content);
}
}
// Return the content of the processed article
return $content;
}
Step 2 Modify the singe.php file in the theme folder. (path /wp-content/themes/your-theme-name/singe.php)
File Editor Find
<?php the_content(); ?>
Replace it with the following code and you're good to go
<?php if ( ! is_user_logged_in() ) { echo remove_extra_images_from_content() ; } else { the_content(); } ?>