Support » Fixing WordPress » Hide post images but keep thumbnail image in Category archive

  • Resolved wellhall12

    (@wellhall12)


    Hi basically within my home page i show my latest posts which have there own thumbnail, and within the posts there are images. however i did add the following code into “functions.php” which enabled me to hide the images but keep the thumbnail being shown:

    function wpi_image_content_filter($content){
    if (is_home() || is_front_page()){
    $content = preg_replace(“/<img[^>]+\>/i”, “”, $content);
    }
    return $content;
    }
    add_filter(‘the_content’,’wpi_image_content_filter’,11);

    Basically when the post is shown in the category list the thumbnail and the images of the post are shown, which is not what i want. I want the thumbnail image to show and the post images to be hiden like at the home page.

    Please help someone

Viewing 5 replies - 1 through 5 (of 5 total)
  • You can use the following conditional tag and include this in your check:

    is_category()
    reference

    okay so what would the following code look like? sorry im not very good with this.

    something like

    function wpi_image_content_filter($content){
    if (is_category() {
    $content = preg_replace("/<img[^>]+\>/i", "", $content);
    }
    return $content;
    }
    add_filter('the_content','wpi_image_content_filter',11);

    [Moderator Note: Please post code or markup snippets between backticks or use the code button.>]

    well, if your original code worked, then yes, that’s the idea. Also include your s_home() || is_front_page() conditional check if you want to maintain the old behaviors. But yeah that’s the idea, hopefully that’ll give you the desired effect.

    Thanks for your help

    The following code worked

    function wpi_image_content_filter($content){
    
        if (is_home() || is_front_page() || is_category()){
          $content = preg_replace("/<img[^>]+\>/i", "", $content);
        }
    
        return $content;
    }
    add_filter('the_content','wpi_image_content_filter',11);

    [Moderator Note: Please post code or markup snippets between backticks or use the code button.>]

    Thank you so much “wellhall12” I’ve been looking for a solution to hide post images on category post previews. Your code worked perfectly.

    function wpi_image_content_filter($content){
    
        if (is_home() || is_front_page() || is_category()){
          $content = preg_replace("/<img[^>]+\>/i", "", $content);
        }
    
        return $content;
    }
    add_filter('the_content','wpi_image_content_filter',11);
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Hide post images but keep thumbnail image in Category archive’ is closed to new replies.