I recently migrated a Wordpress network of two sites from a local MAMP server to an online one. Everything migrated perfectly, except the functionality of the Post-Thumbnail. The meta box is simply not appearing in any site, in any theme across the network.
I've tested a few different themes with post-thumbnail enabled, and the result is the same. No metabox is visible! I'm rather stumped, does anyone have suggestions?
The functions.php file for one of the sites is:
if(function_exists('add_theme_support')) {
add_theme_support('post-thumbnails', array( 'page', 'post', 'portfolio' ));
/** Register custom size */
add_image_size('page-image', 620, 620, false);
add_image_size('portfolio-image', 795, 575, true);
add_image_size('portfolio-thumbnail', 135, 94, true);
}
//------enable post thumbnail preview for custom columns
if ( !function_exists('fb_AddThumbColumn') && function_exists('add_theme_support') ) {
// for post and investments
function fb_AddThumbColumn($cols) {
$cols['thumbnail'] = __('Thumbnail');
return $cols;
}
function fb_AddThumbValue($column_name, $post_id) {
$width = (int) 200;
$height = (int) 125;
if ( 'thumbnail' == $column_name ) {
// thumbnail of WP 2.9
$thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
// image from gallery
$attachments = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') );
if ($thumbnail_id)
$thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true );
elseif ($attachments) {
foreach ( $attachments as $attachment_id => $attachment ) {
$thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true );
}
}
if ( isset($thumb) && $thumb ) {
echo $thumb;
} else {
echo __('None');
}
}
}
// for posts
add_filter( 'manage_posts_columns', 'fb_AddThumbColumn' );
add_action( 'manage_posts_custom_column', 'fb_AddThumbValue', 10, 2 );
// for portfolio
add_filter( 'manage_portfolio_columns', 'fb_AddThumbColumn' );
add_action( 'manage_portfolio_custom_column', 'fb_AddThumbValue', 10, 2 );
}
*/
function the_post_thumbnail_url($post_id) {
echo wp_get_attachment_url( get_post_thumbnail_id($post_id) );
}
You must switch to Network Admin mode
then go to
Settings > Network settings
near the end you'll find
Upload Settings > Media upload buttons
I checked Images to see the Featured Image box appears in my New Post admin page.
I had a similar problem with thumbnails when I migrated a Wordpress Network.
I've fixed it using this code, maybe it can help you. You need to add this code to functions.php
<?php
function get_timthumb_thumbnail($post_id = null) {
global $blog_id;
//we can access it, so why not use it?
$is_mu = WP_ALLOW_MULTISITE;
//if is true it means it's a wordpress MU site and we'll have to do some work
if($is_mu == true):
$thumbnail_id=get_the_post_thumbnail($post_id);
preg_match ('/src="(.*)" class/',$thumbnail_id,$link);
$imageParts = explode('files/', $link[1]);
if (!empty($imageParts[1])):
//check if the image is in the blog directory
if(#getimagesize('./wp-content/blogs.dir/' . $blog_id . '/files/' . $imageParts[1])):
$thumbnail = 'http://'.$_SERVER['HTTP_HOST'].'/wp-content/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
endif;
//check if the image is in the main uploads directory (you never know)
if(#getimagesize('./wp-content/uploads/' . $imageParts[1])):
$thumbnail = 'http://'.$_SERVER['HTTP_HOST'].'/wp-content/uploads/' . $imageParts[1];
endif;
else:
$imageParts = explode('uploads/', $link[1]);
if(#getimagesize('./wp-content/uploads/' . $imageParts[1])):
$thumbnail = 'http://'.$_SERVER['HTTP_HOST'].'/wp-content/uploads/' . $imageParts[1];
endif;
endif;
else:
$thumbnail_id=get_the_post_thumbnail($post_id);
preg_match ('/src="(.*)" class/',$thumbnail_id,$link);
$thumbnail = $link[1];
endif;
return $thumbnail;
}
?>
To use this function on the loop, I had added something like this:
<?php $thumb = get_timthumb_thumbnail($post->ID); ?>
<?php if(has_post_thumbnail()){ ?>
<img src="<?php echo $thumb;?>" class="thumb" />
<?php }else{
echo '<img src="'.get_bloginfo("template_url").'/img/boyds.png" class="thumbdflt" />';
}?>
Regards.
Related
I am using a custom archive-product.php file in my WooCommerce theme. I haven't changed much about the file, just added a few HTML elements for sidebars and such, but the main componants remain untouched.
In my template, I'm trying to get a list of categories with this code, in a sidebar (outside the loop):
$prodCats = get_categories(array('taxonomy'=>'product_cat','exclude'=>'26,482,474','parent'=>0));
if (!empty($prodCats)) {
//echo '<center><strong>Jump to Category</strong></center>';
echo '<ul>';
foreach ($prodCats as $cat) {
// get the thumbnail id using the queried category term_id
$thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
// get the image URL
$image = wp_get_attachment_url( $thumbnail_id );
echo '<li>';
echo '';
echo '<div class="catName">'.$cat->name.'</div>';
echo '<img src="'.$image.'"/>';
echo '</li>';
}
echo '</ul>';
}
But, this outputs only the product categories that are part of the current WooCommerce loop. It will not get all the product categories. It seems to somehow be tied to the WC loop on that page instead of operating independantly.
Is this intended behavior on a custom post type archive or specifically on WooCommerce?
If not, what might I look for to solve this?
This code, which does a similar thing on a woocommerce Product Attribute (pa_brand) is also affected. Note that I'm using new WP_Term_Query here to see if that solves it, but it still only pulls the terms that are in the current WC loop.
//Get brand list and display it
function ppp_get_brand_list() {
?>
<div id="brandList">
<center><h4>Brands</h4></center>
<?php
$brandArgs = array(
'taxonomy' => 'pa_brand',
'hide_empty' => true,
);
$brands = new WP_Term_Query( $brandArgs );
echo '<ul>';
foreach ( $brands->terms as $brand ) {
$brand_image_id = get_term_meta( $brand->term_id, 'product_search_image_id', true );
$brand_image = wp_get_attachment_url( $brand_image_id );
$brand_link = get_term_link($brand->term_id,'pa_brand');
echo '<li><img src="'.$brand_image.'" alt="" /></li>';
}
echo '</ul>';
?>
</div>
<?php
}
I need help creating a wp shortcode that shows a list of users (by role) with a check if they have commented on a certain post. Post ID and user role are shortocode parameters.
In order to do this I thought of merging this handmade shortcode ( it shows for a specified user if whether or not commented on a post, [fancyshortcode user_id="" post_id=""] ):
function fancycheck( $attributes ) {
// Things that you want to do.
$args = array(
'user_id' => get_the_ID(),
'post_id' => get_the_ID()
);
if( isset( $attributes['post_id'] ) ){
$args['post_id'] = absint($attributes['post_id']);
}
if( isset( $attributes['user_id'] ) ){
$args['user_id'] = absint($attributes['user_id']);
}
$comment = get_comments($args);
if(!empty($comment)){
return '<div class="ccount">YES</div>';
}
else {
return '<div class="ccountniet">NO</div>';
}
return "";
}
// register shortcode
add_shortcode('fancyshortcode', 'fancycheck');
with a shortcode to show users list... something like this:
function wcr_list_users($atts) {
// I don't use shortcode_atts because I want to have a dynamic number of attributes
$query = new WP_User_Query($atts);
$results = $query->get_results();
// no results
if (empty($results)) {
return;
}
// when we have results
ob_start();
echo '<ul>';
foreach ($results as $item) {
?>
<li>
<?php echo esc_html($item->display_name); ?></li>
<?php
}
echo '</ul>';
return ob_get_clean();
}
add_shortcode('list_users', 'wcr_list_users');
but it is a too complicated task for my skills. So I tried to simplify the shortcode just indicating for each user the number of comments left for a particular post.. something like this:
function listone($atts) {
$query = new WP_User_Query($atts); $results = $query->get_results();
$args = array( 'post_id' => get_the_ID() );
$comments = get_comments('post_id');
// no results
if (empty($results)) {
return;
}
// when we have results
ob_start();
echo '<ul>';
foreach ($results as $item) {
?>
<li>
<?php echo esc_html($item->display_name); ?><br />
Comments: <?php echo ($comment->comment_author .'--comment--'.$comment->comment_content);?>
</li>
<?php
}
echo '</ul>';
return ob_get_clean();
}
add_shortcode('listonesc', 'listone');
but obviously this <?php echo ($comment->comment_author .'--comment--'.$comment->comment_content);?> doesn't work.
Can anyone help me or give me directions on which way to go? Thank you
I'm using
if ( has_post_thumbnail() ){}
to check if the post have thumbnail image,
but this
echo get_attached_media('image', $post->ID);
display the word
Array
I need to show the attached image
If you are trying to get just one image in the post and use it as a thumbail, you might want to try this one:
Add this to you're functions.php :
// Get URL of first image in a post
function catch_that_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];
// no image found display default image instead
if(empty($first_img)){
$first_img = "/images/default.jpg";
}
return $first_img;
}
To call it just put
<?php echo catch_that_image() ?>
in your template file within the loop.
I found this brilliant code from this forum thread. Saved me a lot of times.
I found this, and it WORKS
<?php
if( has_post_thumbnail() )
{
// check if the post has a Post Thumbnail
echo ' <a href="';
the_permalink();
echo '" title="';
the_title_attribute();
echo '">';
the_post_thumbnail( 'medium' );
echo '</a>';
}
else
{
$imgs = get_attached_media( 'image' );
if( count( $imgs ) > 0 )
{
$img = array_shift( $imgs );
echo wp_get_attachment_image( $img->ID, 'thumbnail' );
}
}
?>
And all thanks and credit for #birgire on WPSE
You need to use get_attached_media( 'image', $post->ID );
Using
echo get_attached_media( 'image' );
should work.
I am using the following short code to list the product categories in woocommerce Wordpress plugin
<?php echo do_shortcode('[product_categories number=""]'); ?>
The problem is the category thumbnail that I need to get rid of. I want to hide it and doing it with CSS seems to be a big hassle. Is there anyway I can list the categories without the thumbnails appearing?
You could use the following:
$args = array( 'taxonomy' => 'product_cat' );
$terms = get_terms('product_cat', $args);
if (count($terms) > 0) {
echo '<p class="my_term-archive">';
foreach ($terms as $term) {
echo '' . $term->name . '';
}
echo '</p>';
}
Heres what I used for making a list of all categories by letter excluding empty letters and with category featured images!
All you need to do is style it the way you want ;)
<?php
/* Define which category // to list child categories array('parent'=>42) 42 is category ID */
$designers = get_terms('product_cat', array('parent'=>42));
/* If categ not empty for each designer take first letter */
if ( !empty( $designers ) && !is_wp_error( $designers ) ){
$term_list = [];
foreach ( $designers as $designer ){
$letter = strtoupper($designer->name[0]);
$designer_list[$letter][] = $designer;
}
unset($designer);
foreach ( $designer_list as $key=>$value ) {
/* Wrap around each designer */
echo '<div><span>' . $key . '</span><ul>';
foreach ( $value as $designer ) {
// Set thumbnail
$thumbnail_id = get_woocommerce_term_meta( $designer->term_id, 'thumbnail_id', true );
// get the image URL
$image = wp_get_attachment_url( $thumbnail_id );
/* List designers */
echo '<li>' . $designer->name . '<img src=" ' .$image.' "" alt="" /></li>';
}
/* end Wrap around designer */
echo '</ul></div>';
}?>
}
I'm using a custom post type plugin that returns an uploaded file's attachment ID instead of its url.
I've been able to get the image to display using wp_get_attachment_image_src as outlined in the codex here http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src but my problem is getting it to play nicely with the code on the template page used to call the information from the custom post type.
Stripping it down to the basics, this is what calls the custom post type info from the template page:
<?php
$slideshowplatform = get_post_meta($post->ID, 'slideshowplatform', true);
foreach($slideshowplatform as $slide) {
echo '<img src="' . $slide['slide'] . '" />';
}
?>
I'm having difficulty reconciling this with what the codex provides:
<?php
$attachment_id = 8; // attachment ID
$image_attributes = wp_get_attachment_image_src( $attachment_id ); // returns an array
?>
<img src="<?php echo $image_attributes[0]; ?>">
It seems like something like the following should work, but I'm obviously missing something with the php syntax
<?php
$slideshowplatform = get_post_meta($post->ID, 'slideshowplatform', true);
foreach($slideshowplatform as $slide) {
$image_attributes = wp_get_attachment_image_src( $slide['slide'] );
echo '<img src="<?php echo $image_attributes[0]; ?>" />';
}
?>
Any thoughts would be appreciated, thanks.
I think this is what you want
if ( $post->post_type == 'slideshowplatform' && $post->post_status == 'publish' )
{
$attachments = get_posts(array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
));
if ($attachments) {
foreach ($attachments as $attachment) {
$thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
echo $thumbimg;
//$image_attributes = wp_get_attachment_image_src( $slide['slide'] );
//echo '<img src="' . $image_attributes[0] . '" />';
}
}
}