WordPress not logged in users (visitors) only show a picture in the post Used to guide the user to sign up

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.

图片[1]-WordPress 未登录用户(游客)只显示文章中一张图片 用于引导用户注册-光子波动网 | WordPress教程、Elementor教程与故障修复

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;
}
图片[2]-WordPress 未登录用户(游客)只显示文章中一张图片 用于引导用户注册-光子波动网 | WordPress教程、Elementor教程与故障修复

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(); } ?>
图片[3]-WordPress 未登录用户(游客)只显示文章中一张图片 用于引导用户注册-光子波动网 | WordPress教程、Elementor教程与故障修复

Contact Us
Can't read the tutorial? Contact us for a free answer! Free help for personal, small business sites!
客服微信
Customer Service
Tel: 020-2206-9892
QQ咨询:1025174874
(iii) E-mail: [email protected]
Working hours: Monday to Friday, 9:30-18:30, holidays off
© Reprint statement
This article was written by Jack
THE END
If you like it, support it.
kudos0 share (joys, benefits, privileges etc) with others
commentaries sofa-buying

Please log in to post a comment

    No comments