WordPress.org

Ready to get started?Download WordPress

Forums

Code Snippets
[resolved] Code formatting (6 posts)

  1. sorto
    Member
    Posted 1 year ago #

    I have a code snippet I like to use to place a default image for a category when there is not one already on the post. I am having trouble getting it to work in this plugin. Thoughts?

    // category-specific post thumbnails
    function category_specific_post_thumbnails() {
    	if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) {
    		the_post_thumbnail('thumbnail');
    	} else {
    		$category = get_the_category();
    		//echo '<img src="'. get_bloginfo('template_directory') .'images'. $category[0]->cat_name .'.png">'; 
    
    	}
    }

    http://wordpress.org/extend/plugins/code-snippets/

  2. Shea Bunge
    Member
    Plugin Author

    Posted 1 year ago #

    Hello,
    It looks like you're missing some code. Try re-copying it from where you found it.
    You're looking for a line something like
    add_action( <em>hook_name</em>, 'category_specific_post_thumbnails' );
    or
    add_filter( <em>hook_name</em>, 'category_specific_post_thumbnails' );
    where hook_name could be almost anything.

  3. Shea Bunge
    Member
    Plugin Author

    Posted 1 year ago #

    Did you have any luck getting your snippet to work?

  4. sorto
    Member
    Posted 1 year ago #

    I did not. I added the add_action with tag of transition_post_status and now I see and error message:

    Warning: Cannot modify header information - headers already sent by (output started at /wp-content/plugins/code-snippets/code-snippets.php(398) : eval()'d code:8) in /wp-includes/pluggable.php on line 881

    The add_action code:

    add_action( 'transition_post_status', 'category_specific_post_thumbnails' );

    I received this error on all of the post modification hooks(post_publish, post_save, etc). It is probably not the correct hook for the code.

    I have a series of posts that get published via RSS feeds and they come in without featured images, so no thumbnail on the main pages but they all go to the same category. I had hoped to set the featured image for all new published posts in this category to a single image for that category. I can accomplish this by changing the functions.php, but prefer your tool for theme upgrades/changes, visibility, etc.

    I am very skilled with PHP, though, so this may be a bit out of reach.

    Also, a feature request - is it possible to create a central repository for all code snippets? It would be pretty great to create a library of snippets that can be used on multiple sites.

  5. Shea Bunge
    Member
    Plugin Author

    Posted 1 year ago #

    Such libraries already exist:

    Also see:

    There are plenty more out there.

  6. Shea Bunge
    Member
    Plugin Author

    Posted 1 year ago #

    Try hooking it to these filters:

    add_filter('the_excerpt_rss', 'category_specific_post_thumbnails');
    add_filter('the_content_feed', 'category_specific_post_thumbnails');

    Let me know if it works.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.