Support » Plugin: Default featured image » Category Featured Image

  • Resolved SEOWebDesignLLC

    (@seowebdesignllc)


    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/

Viewing 15 replies - 1 through 15 (of 19 total)
  • Plugin Author janw.oostendorp

    (@janwoostendorp)

    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

    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

    Plugin Author janw.oostendorp

    (@janwoostendorp)

    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.

    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…

    Plugin Author janw.oostendorp

    (@janwoostendorp)

    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.

    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-->
    Plugin Author janw.oostendorp

    (@janwoostendorp)

    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.

    Plugin Author janw.oostendorp

    (@janwoostendorp)

    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…

    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!

    Plugin Author janw.oostendorp

    (@janwoostendorp)

    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.

    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.

    Plugin Author janw.oostendorp

    (@janwoostendorp)

    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();

    I give up.

    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?!

    Plugin Author janw.oostendorp

    (@janwoostendorp)

    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….

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Category Featured Image’ is closed to new replies.