WordPress.org

Forums

Default featured image
[resolved] Category Featured Image (20 posts)

  1. SEOWebDesignLLC
    Member
    Posted 1 year ago #

    I need help making default images for each of my categories. I tried your snippet of php code for the functions file, but it seemed to break the site. Here is the snippet I pasted at the end of my functions.php file:

    function dfi_category( $dfi_id ) {
      if ( has_category( 'commodities' ) ) {
        return 157531; // the image id
      } else if ( has_category( 'dividends-income' ) ) {
        return 157533; // the image id
      } else if ( has_category( 'etfs' ) ) {
        return 157540; // the image id
      } else if ( has_category( 'forex' ) ) {
        return 157543; // the image id
      } else if ( has_category( 'investing' ) ) {
        return 157549; // the image id
      } else if ( has_category( 'market' ) ) {
        return 157553; // the image id
      } else if ( has_category( 'personal-finance' ) ) {
        return 157560; // the image id
      } else if ( has_category( 'real-estate' ) ) {
        return 157563; // the image id
      } else if ( has_category( 'retirement' ) ) {
        return 157571; // the image id
      } else if ( has_category( 'stocks' ) ) {
        return 157575; // the image id
      } else if ( has_category( 'trading' ) ) {
        return 157578; // the image id
      } else if ( has_category( 'us-world-news' ) ) {
        return 157730; // the image id
      }
      return $dfi_id; // the original featured image id
    }
    add_filter('dfi_thumbnail_id', 'dfi_category' );

    Please help!

    http://wordpress.org/plugins/default-featured-image/

  2. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    Hi,

    too bad to hear this. The code is valid and you edited it correctly.
    Can you specify how it 'breaks the site'. Do you see errors of just white screens?

    Did you make sure it is before the ?> of the functions.php, if it has a ?> at all.

    What is your WordPress function? Do you use other plugins?

    I highly suspect that you pasted it wrong within the functions.php

    Let me know

  3. SEOWebDesignLLC
    Member
    Posted 1 year ago #

    So it was pasted incorrectly as you suspected. However, the default images are still showing the main overall default image that is set with your media plugin.

    Do the categories have to be stand-alone categories for it to work correctly? Currently my main categories are within a parent category "Resources". Or maybe it just takes awhile to refresh?

    Resources
    -Commodities
    -Dividends + Income
    -ETFs
    -FOREX
    -Investing
    -Market
    -Personal Finance
    -Real Estate
    -Retirement
    -Stocks
    -Trading

    ***but the US & World News category is a stand-alone and that didn't work with a category specific image either?***

    The domain is --> http://growthfinancial.net

  4. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    That's weird. Can you try:

    function dfi_category( $dfi_id ) {
    	global $post;
      if ( has_category( 'commodities', $post->ID  ) ) {
        return 157531; // the image id
      } else if ( has_category( 'dividends-income', $post->ID ) ) {
        return 157533; // the image id
      } else if ( has_category( 'etfs', $post->ID  ) ) {
        return 157540; // the image id
      } else if ( has_category( 'forex', $post->ID  ) ) {
        return 157543; // the image id
      } else if ( has_category( 'investing', $post->ID  ) ) {
        return 157549; // the image id
      } else if ( has_category( 'market', $post->ID  ) ) {
        return 157553; // the image id
      } else if ( has_category( 'personal-finance', $post->ID  ) ) {
        return 157560; // the image id
      } else if ( has_category( 'real-estate', $post->ID  ) ) {
        return 157563; // the image id
      } else if ( has_category( 'retirement', $post->ID  ) ) {
        return 157571; // the image id
      } else if ( has_category( 'stocks', $post->ID  ) ) {
        return 157575; // the image id
      } else if ( has_category( 'trading', $post->ID  ) ) {
        return 157578; // the image id
      } else if ( has_category( 'us-world-news', $post->ID  ) ) {
        return 157730; // the image id
      }
      return $dfi_id; // the original featured image id
    }
    add_filter('dfi_thumbnail_id', 'dfi_category' );

    If that doesn't help can you show me the lines that fetch a post and shows the featured image.

  5. SEOWebDesignLLC
    Member
    Posted 1 year ago #

    Thanks again for your speedy reply, but that didn't seem to work either.

    Where do I find the lines that fetch a post and shows the featured image? Not too familiar with WP quite yet...

  6. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    It should be one of these functions: http://codex.wordpress.org/Post_Thumbnails#Function_Reference

    As which file is hard to say. Depends a lot on the theme.

  7. SEOWebDesignLLC
    Member
    Posted 1 year ago #

    Hopefully this is what you were looking for:

    <div id="content-area">
    
    	<?php $mm_featured_img = get_option('mm_featured_img'); if ($mm_featured_img == "true") { ?>
    
    		<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { /* if post has a thumbnail */ ?>
    
    		<div class="post-image">
    
    			<?php the_post_thumbnail('post-thumb'); ?>
    
    		</div><!--post-image-->
    
    		<?php } else { ?>
    
    		<?php } ?>
    
    	<?php } ?>
    
    	<?php the_content(); ?>
    
    	<?php wp_link_pages(); ?>
    
    </div><!--content-area-->
  8. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    Yes that is what I'm looking for. It does meet all requirements.
    I don't know why it doesn't work :(

    One thing you could try is paste this above the the_post_thumbnail.

    <?php var_dump(has_category( 'commodities' )) ?>

    Does it return false or true on a commodities post? That text should appear just above the image.

    Let me know.

  9. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    Just to be sure I tested this on a clean install. This code works.

    You might want to know why it is not default part of the plugin. I don't think it is easy enough to manage. Since 1 post can have multiple categories and there are subs.

    adding the $post->ID did not matter...

  10. SEOWebDesignLLC
    Member
    Posted 1 year ago #

    It returned this --> "bool(true)"

    Yeah, I had read your previous threads about why you have yet to add this function to the plugin itself. I am sure writing out the hierarchy or even allowing for customization within the category/subs is a pain...but it would be nice to have!

  11. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    It did return TRUE ? Well that is weird.

    But it didn't show the correct featured image? Did that post have more categories?

    I have 2 idea's left:

    IDEA 1
    Could you try modding the filter in functions.php

    Add: var_dump(has_category( 'commodities' ))
    above if ( has_category( 'commodities', $post->ID ) ) {

    If that returns a true on the same post.....

    You may revert back to your original code without all the $post->ID. It can only slow it down.

    IDEA 2
    Remove the lines to get this.

    <div id="content-area">
    	<?php $mm_featured_img = get_option('mm_featured_img'); if ($mm_featured_img == "true") { ?>
    		<div class="post-image">
    			<?php the_post_thumbnail('post-thumb'); ?>
    		</div><!--post-image-->
    	<?php } ?>
    	<?php the_content(); ?>
    	<?php wp_link_pages(); ?>
    </div><!--content-area-->

    Don't mix these Idea's. IDEA1 might not appear on screen at all...
    Let me know.

  12. SEOWebDesignLLC
    Member
    Posted 1 year ago #

    Correct, it still showed the default featured image that was set with your media plugin. As I explained earlier, all of the categories I am trying to set these images for are inside of a parent category 'Resources' (see above).

    I am not sure how IDEA 1 is supposed to look because when adding that line above if there was a 'syntax error' notice in dreamweaver. Can you provide the full code?

    IDEA 2 didn't seem to do anything different. Still showing they generic featured image from your media plugin.

  13. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    Idea 1 typo, forgot a ;

    This is what I mean.

    function dfi_category( $dfi_id ) {
      var_dump(has_category( 'commodities' ));
      if ( has_category( 'commodities' ) ) {
        return 157531; // the image id
      } else if ( has_category( 'dividends-income' ) ) {

    If it doesn't show try a true of false anywhere add a die();
    var_dump(has_category( 'commodities' ));die();

  14. SEOWebDesignLLC
    Member
    Posted 1 year ago #

    I give up.

  15. SEOWebDesignLLC
    Member
    Posted 1 year ago #

    When doing all of this I noticed there was also a functions.php file within my theme's folder. I had been doing the previous edits to the functions.php that is in the wp-includes folder.

    I have reverted everything back to its original state as I am bit frustrated with this WP fiasco. Why is this so difficult?!

  16. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    Well this has to be put in the theme's functions.php.
    I don't know how you even found the one in wp-includes I develop WP daily and never even saw it before.

    WP rule #1: NEVER edit source files.

    If adding the code from your first post to the your-theme/functions.php doesn't work I have one idea left. Disable all (other) plugins.
    Something might be interfering.

    If that doesn't work something else is wrong this your installation and I can't give support on that....

  17. SEOWebDesignLLC
    Member
    Posted 1 year ago #

    Told you I was new to WP. I find all sorts of stuff when I have no idea what I am looking for exactly.

    ...and that's all it took so make sure to let it be known, use the THEME's functions.php file...and no other(s)!

    Thanks for all of your help and quick feedback.

    -The End-

  18. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    So it worked?

    Hint:
    the WordPress codex has a lot of information about all these things.

    Including building your own theme
    General tip: don't try to figure stuff out yourself, Google everything you are not certain about. WordPress has a lot of information all across the web. You will find your answers there.

  19. SEOWebDesignLLC
    Member
    Posted 1 year ago #

    Yes it worked.

    Hint:
    Just clarify exactly WHICH functions.php needs to be edited in your FAQs because I promise that Google nor the WP codex would tell me THAT!

    Thanks again for all of your help...and expert advice!

  20. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    I'll note that. It is so common knowledge for me easy to forget. Also because it is the first step in WP theming.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Default featured image
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags

No tags yet.