- This topic has 13 replies, 2 voices, and was last updated 4 years, 6 months ago by Leo.
-
AuthorPosts
-
October 22, 2019 at 2:19 pm #1041769shpr
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.
October 22, 2019 at 5:25 pm #1041822LeoStaffCustomer SupportHi there,
You will need this filter:
https://docs.generatepress.com/article/option_generate_blog_settings/#options-%E2%80%98post_image_alignment%E2%80%99Let me know if this helps π
October 23, 2019 at 11:50 am #1042719shprHi 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
October 23, 2019 at 1:14 pm #1042765LeoStaffCustomer SupportYou 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_PageOctober 23, 2019 at 1:43 pm #1042787shprYes, 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
October 23, 2019 at 1:45 pm #1042789LeoStaffCustomer SupportWhat 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; } ;
October 23, 2019 at 1:53 pm #1042796shprWhen 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.
October 23, 2019 at 4:41 pm #1042874LeoStaffCustomer SupportTry 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
October 23, 2019 at 11:40 pm #1042978shprWhen 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.
October 24, 2019 at 9:18 am #1043573LeoStaffCustomer SupportThe 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 π
October 24, 2019 at 11:07 am #1043657shprYes, 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-competitionsI could message you admin login if you like?
October 24, 2019 at 11:18 am #1043662LeoStaffCustomer SupportSorry just noticed an error in the code.
Can you give it another shot?
https://generatepress.com/forums/topic/change-featured-image-settings-in-specific-category-center/#post-1042874October 24, 2019 at 12:02 pm #1043682shprAwesome – 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?
October 24, 2019 at 12:06 pm #1043688LeoStaffCustomer SupportGlad 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!
-
AuthorPosts
- You must be logged in to reply to this topic.