Exclude the_post_thumbnail from gallery shortcode - wordpress

I am using this code to have a simple gallery on the page:
<?php echo do_shortcode('[gallery itemtag="ul" icontag="li" size="full" columns="0" link="file" ]'); ?>
The problem now is that the end-user has to upload an image via the Media page before selecting this image as featured image.
I know this could be solved by adding the featured image's ID to the shortcode's exclude list, but how to get this ID automatically?

function exclude_thumbnail_from_gallery($null, $attr)
{
if (!$thumbnail_ID = get_post_thumbnail_id())
return $null; // no point carrying on if no thumbnail ID
// temporarily remove the filter, otherwise endless loop!
remove_filter('post_gallery', 'exclude_thumbnail_from_gallery');
// pop in our excluded thumbnail
if (!isset($attr['exclude']) || empty($attr['exclude']))
$attr['exclude'] = array($thumbnail_ID);
elseif (is_array($attr['exclude']))
$attr['exclude'][] = $thumbnail_ID;
// now manually invoke the shortcode handler
$gallery = gallery_shortcode($attr);
// add the filter back
add_filter('post_gallery', 'exclude_thumbnail_from_gallery', 10, 2);
// return output to the calling instance of gallery_shortcode()
return $gallery;
}
add_filter('post_gallery', 'exclude_thumbnail_from_gallery', 10, 2);

<?php $id = get_post_thumbnail_id(get_the_ID()); // gets the post thumbnail ID ?>
<?php echo do_shortcode('[gallery exclude='.$id.' link="file" itemtag="div" icontag="span" captiontag="p" size="thumbnail" columns="4" ]'); ?>

How about?
echo do_shortcode('[gallery exclude="' . get_post_thumbnail_id( $post->ID ) . '"]');

Related

How do I return only image src from "woocommerce_get_product_thumbnail()"?

echo woocommerce_get_product_thumbnail();
That output the whole image tag.
How can I get only the image source?
Same problems solution with just WordPress:
How do I get image url only on the_post_thumbnail
Is that possible with WooCommerce?
Try to write your own code based on the function woocommerce_get_product_thumbnail() from wp-content/plugins/woocommerce/includes/wc-template-functions.php. For example:
function my_get_the_product_thumbnail_url( $size = 'shop_catalog' ) {
global $post;
$image_size = apply_filters( 'single_product_archive_thumbnail_size', $size );
return get_the_post_thumbnail_url( $post->ID, $image_size );
}
You can get the thumbnail of any post as such:
$thumbnail = get_the_post_thumbnail_url($post->ID);
it also provides the image URL through normal WordPress get post URL method
try this:
<?php echo the_post_thumbnail_url(); ?>
I thought woocommerce post thumbnail is different than normal post thumbnail( self teaching limits) . But they are the same thing.
So to get just the "src" attribute aka image url use the following line-
<?php the_post_thumbnail_url( $size ); ?>

Inserting variable into header

I have setup a custom template and functions.php such that which users can insert an image url. The is for this image to be displayed in the header.
In the custom template, I use an add_action to place the image into the header. My problem is that whilst I can supply a url directly in the custom template with this image showing up, the same url inserted as a variable does not work.
So, the following extract from my custom template works (where $featimage is set to the url of the image)
$page_id = $wp_query->get_queried_object_id();
$memoptions = get_post_meta($post->ID,'_my_meta',TRUE);
function img(){
$featimage="http://etc.jpg";
return $featimage;
}
function memberland_featured_image() {
$catchkathmandu_featured_image = '<div id="header-featured-image">';
$catchkathmandu_featured_image .= '<img id="main-feat-img" class="wp-post-image" alt="" src="'.img().'" />';
$catchkathmandu_featured_image .= '</div><!-- #header-featured-image -->';
echo $catchkathmandu_featured_image;
}
add_action( 'catchkathmandu_after_hgroup_wrap', 'memberland_featured_image', 10 );
get_header();
but here where $featimgage is set to the variable from the metabox it does not work.
$page_id = $wp_query->get_queried_object_id();
$memoptions = get_post_meta($post->ID,'_my_meta',TRUE);
function img(){
$featimage=$memoptions['memberland_ftd_hder_image'];
return $featimage;
}
function memberland_featured_image() {
$catchkathmandu_featured_image = '<div id="header-featured-image">';
$catchkathmandu_featured_image .= '<img id="main-feat-img" class="wp-post-image" alt="" src="'.img().'" />';
$catchkathmandu_featured_image .= '</div><!-- #header-featured-image -->';
echo $catchkathmandu_featured_image;
}
add_action( 'catchkathmandu_after_hgroup_wrap', 'memberland_featured_image', 10 );
get_header();
p.s. I am using the function img() as in this post as without it, inserting $featimage directly into the src, even if pre-defined as the url, did not work.
I know that the variable options from them the metaboxes are working as images etc which I want displaying under the header all function.
Is this something to do with the order of wordpress functions? Thanks!
the img() function does not know about $memoptions variable as the variable is defined outside the scope of the img() function.
you can define $memoptions in img() function as below:
function img(){
$memoptions = get_post_meta($post->ID,'_my_meta',TRUE);
$featimage=$memoptions['memberland_ftd_hder_image'];
return $featimage;
}

Wordpress grab page attributes

Is there a Wordpress function for grabbing the page attributes? I need to be able to check which templates are being used on which pages.
I have tried the get_post and get_pages but neither one outputs the page attributes.
Thanks in advance!
solution:
$ids= get_all_page_ids();
foreach ($ids as $id){
$meta = get_metadata('post', $id);
//var_dump($meta);
$template = $meta['_wp_page_template'][0];
echo $template;
echo "<br>";
}
Try using get_all_metadata. That will fetch all the meta records for a given object.
<?php
$post_id = 123;
$meta = get_metadata('post', $post_id);
echo $meta['my_custom_field_key'];
The docs are a good place to look: Function Reference « WordPress Codex
i.e.: Function Reference/get page template which
Displays the filename of the page template used to render a Page
(printed within an HTML comment, in this example) :
<?php echo '<!-- ' . basename( get_page_template() ) . ' -->'; ?>
And,
global $wp_query;
$template_name = get_post_meta( $wp_query->post->ID, '_wp_page_template', true );
will give you the template file name. Use str_replace() to strip the .php from the end.
`

Wordpress - Can't output image (from custom field) for previous and next posts

I am working on a portfolio website. Whenever a portfolio piece is clicked, the user is taken to a page that shows details about that piece (i.e. more photos and information). There will also be previous and next navigation links to get to additional pieces. However, I want the previous and next navigation links to be a thumbnail photo of the next piece (custom field for that is thumbnail_photo). This is what I have so far:
<?php
$previous_post = get_previous_post();
$next_post = get_next_post();
$prev_value = get_post_meta( $previous_post->ID, 'materials', $single = true);
$next_value = get_post_meta( $next_post->ID, 'thumbnail_photo', $single = true);
?>
<p><?php echo $prev_value; ?></p>
<p><?php echo $next_value; ?></p>
I used 'materials' in the call for $prev_value since 'materials' is another custom field. I just wanted to see if it was actually working. It outputs the materials just fine, but it only outputs the ID number of the thumbnail_photo. I can't get it to reference the file name so that I can output the actual image.
I am using the Advanced Custom Fields plugin, so each image is stored as an image object. So this is how I would typically output a thumbnail image:
<?php
$image = get_field('thumbnail_photo);
echo $image[sizes]["thumbnail"]; // thumbnail is a reference to wordpress' thumbnail media size
?>
When you configure the ACF input field, be sure you set the return value to be an image object instead of the image ID after change this, or if you already changed, you have to update the post or posts you're trying to get. Because, if you set the field return value as image ID, created the posts and now you wanna change their values you have to modify all the posts because every post_meta is containing the ID of the image.
In the ACF website has a tutorial how you could get the values an turn into images:
With the ID
<?php
wp_get_attachment_image( $next_value, 'thumbnail' );
?>
Another Example With the ID, but this time you have to create the image HTML, the function returns an array with the url, width and height
<?php
$image = wp_get_attachment_image_src( $next_value, 'thumbnail' );
// url = $image[0];
// width = $image[1];
// height = $image[2];
?>
<img src="<?php echo $image[0]; ?>" />
You could do a check for the returned value and then write the image, example:
<?php
if( is_int( $next_value ) ) {
wp_get_attachment_image( $next_value, 'thumbnail');
} elseif ( is_object( $next_value ) ) {
echo $next_value['sizes']['thumbnail'];
} else { ?>
<img src="<?php the_field('field_name'); ?>" alt="" />
<?php } ?>
I didn't test it, but I think it works fine.
I hope this help and sorry for the bad english

Wordpress exit the define Post?

I create category image thumbnails, here i want to which post have no images the post is exit(); .Here is my code for found post image, but how can exit the particular post that have dont image. plz specify me.
<?php //found the images from the post content
global $post, $posts;
$postContent = $post->post_content;
$searchimages = '~<img [^>]* />~';
preg_match_all( $searchimages, $postContent, $post_imgs );
// Check to see if we have at least 1 image
$findImages = count( $post_imgs[0] );
if ( $findImages > 0 ) {
echo "found"."<br />";
} else {
echo "not found"."<br />";
}
?>
Here i define the not found, but i want to this not found post is exit;
If by exit, you mean exiting out the braces (of an if/else statement), use a simple break; function.

Resources