Site logo

Archived topic

Stop showing the date only in some categories

11 replies · Started by Oliver on March 23, 2021

Viewing posts 1–12 of 12

Hey guys, is it possible to only show the date in some categories? My website uses normal blog posts (not woocommerce) to showcase products. Buyers are then sent to Amazon to make the purchase. Can I stop showing the date inside the product categories posts, but let it be shown in the blog post category?

(The website doesn´t show the date in the posts archive, only inside the posts)

Hi there,

you can use the option_generate_blog_settings filter:

https://docs.generatepress.com/article/option_generate_blog_settings/

For example this will disable the date on the product archive or single post

add_filter( 'option_generate_blog_settings', 'db_exclude_date_from_product_category' );
function db_exclude_date_from_product_category( $options ) {
  if ( is_category('product') || in_category('product')  ) {
    $options['date'] = false;
    $options['single_date'] = false;
  }
  return $options;
}

Just make sure that the category term product matches yours

David, I replaced four times the word product with the slug of one of the categories on my site to test it, but received this error:

Don't Panic
The code snippet you are trying to save produced a fatal error on line 875:
mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()
The previous version of the snippet is unchanged, and the rest of this site should be functioning normally as before.
Please use the back button in your browser to return to the previous page and try to fix the code error. If you prefer, you can close this page and discard the changes you just made. No changes will be made to this site.

The mail server error part is understandable because I´m creating the site using bitnami and still don´t have a domain for the site.

Hey Leo thanks for replying. The same error message appeared but i might have not done it right.

I copied the code David wrote because after clicking your link i was taken back there and i figured out you edited the same code, am i right?
If so, this is the code I added into a snippet (the category being Historical Fiction and adding the category slug historical-fiction):


add_filter( 'option_generate_blog_settings', 'db_exclude_date_from_historical-fiction_category' );
function db_exclude_date_from_historical-fiction_category( $options ) {
  if ( is_category('historical-fiction') || in_category('historical-fiction')  ) {
    $options['date'] = false;
    $options['single_date'] = false;
  }
  return $options;
}

Hi there,

You should avoid using - in your declaration of function name and/or variable.

You have db_exclude_date_from_historical-fiction_category as a function name which has - in it.

Try changing it to underscore (_).

Example:

add_filter( 'option_generate_blog_settings', 'db_exclude_date_from_historical_fiction_category' );
function db_exclude_date_from_historical_fiction_category( $options ) {
  if ( is_category('historical-fiction') || in_category('historical-fiction')  ) {
    $options['date'] = false;
    $options['single_date'] = false;
  }
  return $options;
}

Thanks for your help Elvin... amazing!
Pasting code and running it is the closest we´ll come to a magic wand :D

Can I paste this code five times (for all five categories) into a single snippet? or do i need to create separate snippets for each category?

Elvin i tried it like this:


add_filter( 'option_generate_blog_settings', 'db_exclude_date_from_historical_fiction_category' );
function db_exclude_date_from_historical_fiction_category( $options ) {
  if ( is_category( array( 'mystery', 'thrillers', 'science_fiction', 'fantasy', 'historical_fiction' ) ) || in_category( array( 'mystery', 'thrillers', 'science_fiction', 'fantasy', 'historical_fiction' ) ) {
    $options['date'] = false;
    $options['single_date'] = false;
  }
  return $options;
}

But it gave me the don´t panic error again. Maybe I should have changed the histocial_fiction part of this line 'db_exclude_date_from_historical_fiction_category' for something general?

You can only add a function once... so these two lines:

add_filter( 'option_generate_blog_settings', 'db_exclude_date_from_historical_fiction_category' );
function db_exclude_date_from_historical_fiction_category( $options ) {

you see the function name:

db_exclude_date_from_historical_fiction_category

This has to be unique ie. there cannot be another function with the same name.

Guys, the code works when i use only one category. It does not work when i add the ( array( '1', '35', '36', 37) ) part to try to exclude the date from more than just one category.

At the end, and after giving it some thought, what i ended up doing was turning the date off for all posts in the customizer and then creating a function to force the blog category to show the date inside the posts. So I kind of just reversed it.

Thanks for your time guys, have as good one

At the end, and after giving it some thought, what i ended up doing was turning the date off for all posts in the customizer and then creating a function to force the blog category to show the date inside the posts. So I kind of just reversed it.

Ah yes that works as well.

Glad you got it sorted. Thank you for letting us know. No problem. :)

This archived topic is closed to new replies.