[Resolved] Change featured image settings in specific category (center)

Home Forums Support Change featured image settings in specific category (center)

  • This topic has 13 replies, 2 voices, and was last updated 11 months ago by Leo.
Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #1041769
    shpr

    Hello,

    I would like to change the featured image settings in 1 specific category to be as follows;

    – category name: News (slug: news-feed)
    – center position
    – no wrap (text content starts after featured image on new line)

    I had a play with this https://docs.generatepress.com/article/adjusting-the-featured-images/ but couldn’t manage to make it work.

    #1041822
    Leo
    Staff
    Customer Support
    #1042719
    shpr

    Hi Leo,

    I’ve tried this but something wrong…can you assist?

    add_filter( 'option_generate_blog_settings', 'cm_post_img_center' );
    function( $options ) {
        if ( is_category('2101') ) 
        {
    		if ( is_single()  ) {
    		  // DO STUFF
    		  $atts['post_image_alignment'] = true;
    			$atts['post_image'] = true;
    			$atts['post_image_alignment'] = 'post-image-aligned-center';
    			$atts['single_post_image_position'] = 'below-title'; 
    		}	  
        }  
        return $options;
    } ;

    Also, will “is_category” include child/ancestor of that category? (I would like it to).

    Thanks

    #1042765
    Leo
    Staff
    Customer Support

    You are trying to target the single post under a certain category right?

    I believe the in_category conditional tag is what you are looking for:
    https://codex.wordpress.org/Conditional_Tags#A_Category_Page

    #1042787
    shpr

    Yes, that is correct:
    I want to target ONLY single posts that exist within the Category ‘2101’ (and single post of child/ancestors of news-feed category).

    Category 2101 is /news-feed

    I have updated the code to use in_category:

    add_filter( 'option_generate_blog_settings', 'cm_post_img_center' );
    function( $options ) {
        if ( in_category('2101') ) 
        {
    		if ( is_single()  ) {
    		  // DO STUFF
    		  $atts['post_image_alignment'] = true;
    			$atts['post_image'] = true;
    			$atts['post_image_alignment'] = 'post-image-aligned-center';
    			$atts['single_post_image_position'] = 'below-title'; 
    		}	  
        }  
        return $options;
    } ;

    However it still seems to effect ALL featured images in ALL categories and in ALL locations…rather than ONLY single posts in category 2101 (and child/ancestors).

    I have left the filter on so you can see.

    eg. these should not be effected:
    * /news-feed (archive)
    * home page (archive)
    * the first ‘single-post’ on the homepage (not in category 2101)

    Also (but less important) the post image is not appearing below the title.

    Thanks

    #1042789
    Leo
    Staff
    Customer Support

    What if you just do this?

    add_filter( 'option_generate_blog_settings', 'cm_post_img_center' );
    function cm_post_img_center ( $options ) {
        if ( in_category('2101') ) {
            $options ['post_image_alignment'] = true;
    	$options ['post_image'] = true;
    	$options ['post_image_alignment'] = 'post-image-aligned-center';
    	$options ['single_post_image_position'] = 'below-title';   
        }  
        return $options;
    } ;
    #1042796
    shpr

    When I use that code, the result is the same as previous code (forum post #1042787).

    ie. it effects ALL featured images in ALL categories and in ALL locations (and post image is not appearing below the title).

    I have left the code in previous post (forum post #1042787) live on the website.

    The cache and autoptimize have been cleared. Screenshot attached so you can see it is the correct category I’m trying to target.

    Thanks again for looking at this.

    #1042874
    Leo
    Staff
    Customer Support

    Try this:

    add_filter( 'option_generate_blog_settings', function( $options) {
        if ( in_category( 'News' ) ) {
            $options['single_post_image_position'] = 'below-title';
    	$options['single_post_image_alignment'] = 'center';
        }
        return $options;
    } );

    Tested the code so it should work

    #1042978
    shpr

    When I use the above code, the featured image that are within a single post in the News category disappear (the rest of the site returns to normal).

    I also tried replacing News with ‘2101’ and ‘news-feed’ – the result was the same as when I use ‘News’.

    I’ve commented out the code for now.

    #1043573
    Leo
    Staff
    Customer Support

    The code definitely shouldn’t do that.

    Did you remove the previous code from before?

    Can you link me to a normal single post and a single post that’s in the News category?

    Make sure to disable all caching plugins please.

    Let me know 🙂

    #1043657
    shpr

    Yes, only one version of code is there (however I have commented it out for now).
    CDN is now of and cache plugin disabled.

    * All posts listed here are ‘News’ category: /news
    * All posts listed here are NOT ‘News’ category: /free-competitions

    I could message you admin login if you like?

    #1043662
    Leo
    Staff
    Customer Support
    #1043682
    shpr

    Awesome – that worked!

    Just one other thing you might be able to help with…

    I had someone write this script so the image caption would display below the image.

    jQuery(document).ready(function($) {
        if( $('.single.single-post.post-image-aligned-left').length && $('.inside-article .featured_caption').length && $('.inside-article .featured-image .ccfic').length ) {
    
            console.log('single');
            let imageCaption = $('.inside-article .featured_caption').text();
            $('.inside-article .featured-image .ccfic').text(imageCaption);
        }
    });

    Do you know is there a simple way to make this work with the new filter in place?

    #1043688
    Leo
    Staff
    Customer Support

    Glad it’s working 🙂

    The image caption should be below the image by default no?

    Anyways please open a new topic for the separate question.

    Thanks!

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.