Are you using Yoast SEO plugin? Yoast SEO for WordPress is one of the best WordPress plugin out there. Plugin by default adds Image and Description for your Archive and Category pages.
In this tutorial we will go over how to update Twitter and Facebook social Image and Description for WordPress Archive and Category.
Here is a real example once you make below changes.
How to modify Yoast SEO Facebook Image for Archive / Category page?
Just put below code to your site’s functions.php and save file.
add_filter( 'wpseo_opengraph_image', 'crunchify_wpseo_add_custom_facebook_image', 10, 1 ); function crunchify_wpseo_add_custom_facebook_image( $crunchify_image ) { if( is_post_type_archive( 'archivename') ) { $crunchify_image = get_stylesheet_directory_uri().'/crunchify/crunchify.png'; } if( is_category( 'categoryname') ) { $crunchify_image = get_stylesheet_directory_uri().'/crunchify/crunchify.png'; } return $crunchify_image; }
Here are some functions / WordPress hooks which are using here.
get_stylesheet_directory_uri()
- Retrieves stylesheet directory URI for current theme.
- The returned URI does not contain a trailing slash.
is_post_type_archive( string|string[] $post_types = ” )
- Determines whether the query is for an existing post type archive page.
is_category( int|string|int[]|string[] $category = ” )
- Determines whether the query is for an existing category archive page.
How to modify Yoast SEO Facebook Description for Archive / Category page?
add_filter( 'wpseo_opengraph_desc', 'crunchify_wpseo_add_custom_facebook_description' ); function crunchify_wpseo_add_custom_facebook_description( $crunchify_description ) { if( is_post_type_archive( 'posttypename' ) ) { $crunchify_description = 'Howdy folks. Crunchify is the largest free, premium, technical & blogging resource site for beginners.'; } if( is_category( 'categoryname' ) ) { $crunchify_description = 'Howdy folks. Crunchify is the largest free, premium, technical & blogging resource site for beginners.'; } return $crunchify_description; }
How to modify Yoast SEO Twitter Image for Archive / Category page?
add_filter( 'wpseo_twitter_image', 'crunchify_wpseo_add_custom_twitter_image', 10, 1 ); function crunchify_wpseo_add_custom_twitter_image( $crunchify_image ) { if( is_post_type_archive( 'archivename') ) { $crunchify_image = get_stylesheet_directory_uri().'/crunchify/crunchify.png'; } if( is_category( 'categoryname') ) { $crunchify_image = get_stylesheet_directory_uri().'/crunchify/crunchify.png'; } return $crunchify_image; }
Make sure you update folder and image name. In this case:
- crunchify
- crunchify.png
How to modify Yoast SEO Twitter Description for Archive / Category page?
add_filter( 'wpseo_metadesc', 'crunchify_wpseo_add_custom_twitter_description', 10, 1 ); function crunchify_wpseo_add_custom_twitter_description( $crunchify_description ) { if( is_post_type_archive( 'portfolio' ) ) { $crunchify_description = 'Howdy folks. Crunchify is the largest free, premium, technical & blogging resource site for beginners.'; } if( is_category( 'categoryname' ) ) { $crunchify_description = 'Howdy folks. Crunchify is the largest free, premium, technical & blogging resource site for beginners.'; } return $crunchify_description; }
Just clear your site cache and you will see your Twitter and Facebook Image and Description updated for specific category or archive page.