default thumbnail for all posts in same category - wordpress

My Wordpress site use first image as post thumbnail, code:
add_filter('the_content','replace_content');
function get_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[1][0];
if(empty($first_img)) {
$first_img = "/path/to/default.png";
}
return $first_img;
}
Some posts have no image in its content, so i want to use different default thumbnails for them: posts in category A use picture1, posts in cagory B use category2...
How can I do this?
thank you

You need to get the current post category first, then, make a if statement in your code.
Check this link : https://developer.wordpress.org/reference/functions/get_the_category/#comment-305

How about this, here we are using has_post_thumbnail to check for image attachments, if none exist we are setting up the image and image source. From there we check for category assignment, if there are no category matches we use a default thumbnail.
<?php
if ( ! has_post_thumbnail() ) {
$themefolder = get_bloginfo('template_directory');
echo '<img src="' . $themefolder . '/images/';
if ( is_category( 'Category A' ) ) {
echo 'no-image-a.png';
} elseif ( is_category( 'Category B' ) ) {
echo 'no-image-b.png';
} else {
echo 'no-image.png';
}
echo '" alt="' . get_the_title() . '">' . PHP_EOL;
}
?>

Finally, It's my code:
add_filter('the_content','replace_content');
function get_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[1][0];
if(empty($first_img)) {
$categories = get_the_category();
if ( ! empty( $categories ) ) {
foreach ( $categories as $category ) {
if( $category->name == 'Category A' ){ $first_img = "/path/to/default1.png";}
elseif ( $category->name == 'Category B' ){ $first_img = "/path/to/default2.png";}
else {$first_img = "/path/to/default3.png";}
}
}
}
return $first_img;
}

Related

Hide certain acf fields wordpress

Help
Code in template:
<?php
$groupID = '';
$fields = get_fields($groupID);
$fields = get_field_objects();
if( $fields )
{
foreach( $fields as $field_name => $field )
{
if( $field['value'] )
{
echo '<ul>';
echo '<li>' . $field['label'] . ': <strong>' . $field['value'] . '</strong></li>';
echo '</ul>';
}
}
}
?>
I need to hide the fields:
  field_5c0a8d44cf56e
  field_5c0a8d4ecf56f
How can i do this?
Your question is not much clear for me, and from my knowledge what I have understood,
for an acf group you don't want to loop through, the control is yours,
so you can print it straightaway
but if you really want to do it in a loop then,
if( $fields )
{
foreach( $fields as $field_name => $value )
{
if( $value && !in_array($field_name, ["field_5c0a8d44cf56e", "field_5c0a8d4ecf56f"])
{
echo '<ul>';
echo '<li>' . $field_name . ': <strong>' . $value . '</strong></li>';
echo '</ul>';
}
}
}

How to show image and description in Categories widget in wordpress

Hello friends i want so image and description in categories list widget for showing description i use below code:
in function.php
function wpb_catlist_desc() {
$string = '<ul>';
$catlist = get_terms( 'category' );
if ( ! empty( $catlist ) ) {
foreach ( $catlist as $key => $item ) {
$string .= '<li>'. $item->name . '<br />';
$string .= '<em>'. $item->description . '</em> </li>';
}
}
$string .= '</ul>';
return $string;
}
add_shortcode('wpb_categories', 'wpb_catlist_desc');
and in class-wp-widget-categories.php
echo do_shortcode('[wpb_categories]');
it is showing categories name and his description now i want to show categories image also. any one please help me how can i get that. i had try Categories Images plugin for show images but can't able to show
Image get z_taxonomy_image_url($id) and z_taxonomy_image($id)
if(z_taxonomy_image_url($item->term_id))
{
$string .= '<img src ="'.z_taxonomy_image_url($item->term_id).'" style="width: 20%;">';
}
You can get the image using following code and append it to $string.
$catlist = get_terms( 'category' ); // get category list
$string ="<ul>";
foreach ( $catlist as $key => $item )
{
$thumbnail_id = get_woocommerce_term_meta( $item->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
if ( $image ) {
$image_dis='<img src="' . $image . '" alt="' . $cat->name . '" />';
}
$string .= '<li>'. $item->name . '<br />';
$string .= $image_dis . '<br />';
$string .= '<em>'. $item->description . '</em> </li>';
}
$string .="</ul>";
echo $string;

Use different size thumbnails for subcategories and products in Woocommerce

I need to set a different size of thumb for subcategory and product, is this possible?
I need to show a list of subcategories with thumb 500 x 100 and a list of products with thumbnail 300 x 390.
I have already setting in woocommerce> setting > product > display > Product images but i cannot set a different size for subcategory or product.
lol i develop a clean solution:
function register_size_image() {
add_image_size( 'category_thumb', 1170, 585,true );
add_image_size( 'product_thumb', 750, 940,true );
}
add_action( 'after_setup_theme', 'register_size_image' );
function size_of_category_thumb($u)
{
return array(1170, 585,true);
}
add_filter('subcategory_archive_thumbnail_size', 'size_of_category_thumb');
function size_of_product_thumb($u)
{
return array(750, 940,true);
}
add_filter('single_product_archive_thumbnail_size', 'size_of_product_thumb');
i resolve overriding woocommerce's function, i put this on my functions.php
add_image_size( 'category_thumb', 500, 100,1 );
add_image_size( 'product_thumb', 300, 390,1 );
function woocommerce_subcategory_thumbnail( $category ) {
$small_thumbnail_size = 'category_thumb';
$thumbnail_id = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );
if ( $thumbnail_id ) {
$image = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
$image = $image[0];
$image_srcset = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $thumbnail_id, $small_thumbnail_size ) : false;
$image_sizes = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $thumbnail_id, $small_thumbnail_size ) : false;
} else {
$image = wc_placeholder_img_src();
$image_srcset = $image_sizes = false;
}
if ( $image ) {
// Prevent esc_url from breaking spaces in urls for image embeds
// Ref: https://core.trac.wordpress.org/ticket/23605
$image = str_replace( ' ', '%20', $image );
// Add responsive image markup if available
if ( $image_srcset && $image_sizes ) {
echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $category->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" srcset="' . esc_attr( $image_srcset ) . '" sizes="' . esc_attr( $image_sizes ) . '" />';
} else {
echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $category->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" />';
}
}
}
function woocommerce_get_product_thumbnail( $size = 'shop_catalog', $deprecated1 = 0, $deprecated2 = 0 )
{
global $product;
// $image_size = apply_filters( 'single_product_archive_thumbnail_size', $size );
return $product ? $product->get_image( 'product_thumb' ) : '';
}
it works!
Yes, That is quite possible. Follow these steps -
Add new image size. To do that add following code to your theme's functions.php
add_image_size( 'your-custom-size', 500, 100 );
// You can add multiple image sizes by repeating this line with different values (as per you need)
After this step upload images.
If it is overhead for you to upload images again, you can use this plugin
This will create thumbnails with all your specified sizes.
Now in your theme, whereever you are showing images with function like -
the_post_thumbnail( 'your-custom-size' ) or wp_get_attachment_image( 42, 'your-custom-size' ), pass your image size name as param.
function yourFunctionName()
{
return array(1140, 300,true);
}
add_filter('subcategory_archive_thumbnail_size', 'yourFunctionName');
Works for me

Display sidebar after x paragraphs

I am looking for a way to display a sidebar dynamically after x number of paragraphs in my content.
Problem : dynamic_sidebar (' name ') doesn't display text : var_dump($ad_code) = bool(true).
Result : My sidebar is displayed twice in the header, once before the content and at the right paragraphs it displays the number "1".
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = dynamic_sidebar( 'sidebar-6' );
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 1, $content );
}
return $content;
}
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
I just add this :
ob_start();
dynamic_sidebar('sidebar-id');
$sidebar = ob_get_contents();
ob_end_clean();
It's working !

Feeding Wordpress blog entries to own website

I have some code which will feed through the ten most recent Wordpress blog posts through to my own non-Wordpress site. Is there any way to display the featured image assigned to a blog post? I have successfully echoed the blog title, date and body.
<?php
global $text, $maxchar, $end;
function substrwords($text, $maxchar, $end='...') {
if (strlen($text) > $maxchar || $text == '') {
$words = preg_split('/\s/', $text);
$output = '';
$i = 0;
while (1) {
$length = strlen($output)+strlen($words[$i]);
if ($length > $maxchar) {
break;
} else {
$output .= " " . $words[$i];
++$i;
}
}
$output .= $end;
} else {
$output = $text;
}
return $output;
}
$rss = new DOMDocument();
$rss->load('http://myblog.wordpress.com/rss/'); // <-- Change feed to your site
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 10; // <-- Change the number of posts shown
for ($x=0; $x<$limit; $x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$description = substrwords($description, 400);
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<div style="margin-bottom:25px;">';
echo '<h3><strong><a style="color: #139035;" href="'.$link.'" title="'.$title.'" target="_blank">'.$title.'</a></strong></h3>';
echo '<p><small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
echo '<span> <strong><a target="_blank" style="color: #139035;" href="'.$link.'" title="'.$title.'">Read blog ></span></strong>';
echo '</div>';
}
?>
You can use following code to add featured image into your RSS
function img_in_rss($content) {
global $post;
if ( has_post_thumbnail( $post->ID ) ){
$content = get_the_post_thumbnail( $post->ID, 'medium') . $content;
}
return $content;
}
add_filter('the_excerpt_rss', 'img_in_rss');
add_filter('the_content_feed', 'img_in_rss');

Resources