Wordpress Shortcode inside of plugin - php

Here is a snippet from the shortcodes.php file in one of the plugins I'm writing.
if($home_loop->have_posts()) {
switch($display) {
case "static":
while ($home_loop->have_posts()): $home_loop->the_post();
if(has_post_thumbnail() ) {
$content .= '<div class="parallax">';
$content .= get_the_post_thumbnail($post->ID, 'wrapper-wide', array('class' => "img-responsive"));
$content .= '</div>';
$content .= '<div class="container">';
$content .= get_the_content();
$content .= '</div>';
$content .= '<style>';
$content .= '.parallax{//css style here}';
$content .= '</style>;
} else {
//Something
}
endwhile;
break;
This works perfect. If I put a featured image thumbnail in my home post it will be displayed in my parallax div. The question I have is instead of placing the thumbnail inside of the div, I want to have it background-image: url(). I found this code below
<?php
$background = get_field( 'background_image' );
if($background):
?>
<style>
.main-wrapper {
background-image: url(<?php echo $background ?>);
}
</style>
<?php endif; ?>
How would I incorporate and run something like this inside of the $content? The reason I'm trying to get the image as a background-image:url() is so I can add specific css styling like background-position, size, repeat, attachment.

Why not just add the image as a background-image right inside your code? [wp_get_attachment_image_src][1] will return an array of the url, width and height of any attachment. Pass it the ID of the featured image, and gain the flexibility to use ANY size you want, not just the actual featured image size. Then place it as the background using inline CSS. It can be overwritten in your CSS sheet if needed.
($home_loop->have_posts()) {
switch($display) {
case "static":
while ($home_loop->have_posts()): $home_loop->the_post();
if(has_post_thumbnail() ) {
$post_thumnail_info = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'wrapper-wide' );
$content .= '<div class="parallax" style="background-image: url( \'' . $post_thumbnail_info[0] . '\' );">';

Related

Trying To Call Alt & Title Tag Properly

I can't figure this out. I've tried everything. What I'm trying to do is extend the WooCommerce Category file with a header image and text on the right side. However, the image needs to have an ALT tag and a Title tag for good SEO purposes.
This is my existing code just to show the image.
function show_category_header() {
$category_header_src = woocommerce_get_header_image_url();
if( $category_header_src != "" ) {
echo '<div class="woocommerce_category_header_image"><img src="'.$category_header_src.'" /></div>';
}
}
I've tried this, which gets me close but doesn't pull the correct Alt for the image that's uploaded. Gets me some random Alt
function show_category_header() {
$category_header_src = woocommerce_get_header_image_url();
$image_id = get_post_thumbnail_id();
$image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', TRUE);
$image_title = get_the_title($image_id);
if( $category_header_src != "" ) {
echo '<div class="woocommerce_category_header_image"><img src="'.$category_header_src.'" alt="'.$image_alt.'" /></div>';
}
}
add_action('woocommerce_archive_description', 'show_category_header', 1);
What am I missing? I need to add the Title tag as well. Thanks in advance.

Where to add code to custom gallery shortcode

I'm using the information here http://ieg.wnet.org/2016/02/replacing-default-wordpress-gallery-shortcode/ as the basis for a custom gallery shortcode, and trying to create this carousel slider https://www.jssor.com/demos/image-gallery.slider. Problem is I don't know how exactly to implement it.
The images and everything up until then shows in the source code, but then nothing after it.
<div data-u="slides" style="cursor:default;position:relative;top:0px;left:0px;width:800px;height:356px;overflow:hidden;">
<?php
foreach ( $images as $image ) {
$thumbnail = wp_get_attachment_image_src($image->ID, 'homepage-thumb');
$thumbnail = $thumbnail[0];
$fullsize = wp_get_attachment_image_src($image->ID, 'service-page');
$fullsize = $fullsize[0];
$gallery .= "<div><img data-u='image' src='".$thumbnail."'><img data-u='thumb' src='".$fullsize."'></div>";
}
return $gallery;
?>
</div>
It's probably something simple, this is just far past my base of knowledge when it comes to this.
You are returning the value instead of printing it to the web page. Use echo instead of return. Also make sure you echo it in each iteration of the loop so you don't just print one image, but all of them.
<div data-u="slides" style="cursor:default;position:relative;top:0px;left:0px;width:800px;height:356px;overflow:hidden;">
<?php
foreach ( $images as $image ) {
$thumbnail = wp_get_attachment_image_src($image->ID, 'homepage-thumb');
$thumbnail = $thumbnail[0];
$fullsize = wp_get_attachment_image_src($image->ID, 'service-page');
$fullsize = $fullsize[0];
$gallery .= "<div><img data-u='image' src='".$thumbnail."'><img data-u='thumb' src='".$fullsize."'></div>";
echo $gallery;
}
?>
</div>

Wordpress: Change Post Featured Image alt tags to the given alt tag while uploading

I am giving alt tags for an image while uploading, but it is not taking the given image alt tags , it shows blank
i tried to add this filter but still not working
/* Register callback function for post_thumbnail_html filter hook */
add_filter( 'post_thumbnail_html', 'meks_post_thumbnail_alt_change', 10, 5 );
/* Function which will replace alt atribute to post title */
function meks_post_thumbnail_alt_change( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
$post_title = get_the_title();
$html = preg_replace( '/(alt=")(.*?)(")/i', '$1'.esc_attr( $post_title ).'$3', $html );
return $html;
}
The post content is displayed from loop-single.php file
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
How to use those alt tags for my post featured image
Try this it worked for me :)
function change_post_thumbnail_html($html, $post_id, $post_thumbnail_id, $size, $attr) {
$id = get_post_thumbnail_id();
$src = wp_get_attachment_image_src($id, $size);
$alt = get_the_title($id); // gets the post thumbnail title
$class = $attr['class'];
$html = '<img src="' . $src[0] . '" alt="' . $alt . '" class="' . $class . '" />';
return $html;
}
add_filter('post_thumbnail_html', 'change_post_thumbnail_html', 99, 5);
Well i found out the error , it seems previous developer added the image manually to the post and featured image was also set . so i got confused.
i simply put the alt text in edit mode of the post directly.

Need Proper Syntax for Condition (wrap Image with link only when there is one)

we had a developer code something for a Custom Post Type in WordPress that we need to tweak just a touch. They're not available right now, but I think it's a pretty simple PHP issue if knowing the proper syntax. Unfortunately, I'm not completely PHP fluent, so hoping to get some help.
We have a posts landing page where we are just displaying a post thumbnail if there is one, and a link to the full post page (single) ONLY if an email address has been entered into the custom post type. If no email address was entered for the post, no link to the final post. Here's the main chunk of code that is currently working properly for this:
$team_query = new WP_Query($args);
while ($team_query->have_posts()) {
$team_query->the_post();
$member_email = get_post_meta(
get_the_ID(), '_base_team_email', true
);
$html .= '<div class="span-6 team-member">';
if (has_post_thumbnail()) {
$html .= '<div class="member-photo-wrap">'
. get_the_post_thumbnail(get_the_ID(), 'medium')
. '</div>';
}
if (!empty($member_email)) {
$html .= '<p class="member-email">'
. '<a class="linkIcon" href="' . get_permalink() . '#member-top">'
. 'Email & Bio »'
. '</a></p>';
}
$html .= '</div>';
}
All we need to do is tweak so that IF an email address has been entered, the same hyperlink is added around the thumbnail image so it can link to the final post along with the 'Email & Bio' text link. But if NO email address is found, the thumbnail image displays as-is (e.g, no link added).
THANKS!
This would add a link to the thumbnail as well, if the $member_email is not empty.
$team_query = new WP_Query($args);
while ($team_query->have_posts()) {
$team_query->the_post();
$member_email = get_post_meta(
get_the_ID(), '_base_team_email', true
);
// Create anchor tag to use when member email is not empty
$memberEmailAnchorStart = '';
$memberEmailAnchorEnd = '';
if (!empty($member_email)) {
$memberEmailAnchorStart =
'<a class="linkIcon" href="' . get_permalink() . '#member-top">';
$memberEmailAnchorEnd = '</a>';
}
$html .= '<div class="span-6 team-member">';
if (has_post_thumbnail()) {
$html .= '<div class="member-photo-wrap">'
. $memberEmailAnchorStart
. get_the_post_thumbnail(get_the_ID(), 'medium')
. $memberEmailAnchorEnd
. '</div>';
}
if (!empty($member_email)) {
$html .= '<p class="member-email">'
. $memberEmailAnchorStart
. 'Email & Bio »'
. $memberEmailAnchorEnd
. '</p>';
}
$html .= '</div>';
}
Depending on what the linkIcon class does, you might want to remove it from the thumbnail anchor tag.

outputting a featured image rather than a link to a featured image

In the past, I used this custom function to create an image carousel on my WordPress blog with Thesis theme
function top_carousel() {
echo '<div id="topcarousel">';
$the_query = new WP_Query(array(
'category_name'=>'featured',
'orderby'=>'date',
'order'=>'DESC',
'showposts'=>'4'
));
while ($the_query->have_posts()) : $the_query->the_post();
$do_not_duplicate = $post->ID;
$post_image = thesis_post_image_info('thumb');
echo '<div class="carouselu">';
echo '' . $post_image['output'] . '<br />' . get_the_title() . '';
echo '</div>';
echo '<div style="clear:both"></div>';
echo '</div>';
}
add_action('thesis_hook_before_content_box', 'top_carousel');
It has been a while since I used the code, and, having recently setup a new WordPress blog, I discovered it doesn't work anymore, in that the image isn't getting displayed with the link to the post.
Someone suggested to me that this might be because I'm using featured images in my new WordPress blog. Following that tip and looking at the code on the WordPress code, I changed part of the custom function above to look like this
$post_image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'small');
echo '<div class="carouselu">';
echo '' . $post_image[0] . '<br />' . get_the_title() . '';
echo '</div>';
This results in a little progress. Now a link to the image appears above the headline, rather than the actual image. For example,
http://localhost:8888/wp-content/uploads/2013/12/Screen-Shot-2013-12-27-at-9.37.49-AM.png
How do I get the image to show, rather than just a link to where the image is uploaded?
Update
Using the first answer to this post, I tried this code unsuccessfully
$post_image = get_the_post_thumbnail($post_id, 'small');
echo '<div class="carouselu">';
echo '' . $post_image['output'] . '<br />' . get_the_title() . '';
echo '</div>';
I also tried this unsuccessfully
$post_image = wp_get_attachment_image_src( get_the_post_thumbnail($post_id, 'small'));
echo '<div class="carouselu">';
echo '' . $post_image['output'] . '<br />' . get_the_title() . '';
echo '</div>';
Neither of those solutions output anything, whereas my attempt at least output a link to the uploaded image. Am I using the function incorrectly?
You should use the function get_the_post_thumbnail, refering to the doc : http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
Usage:
<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>
To enable Post Thumbnails, the current theme must include
add_theme_support( 'post-thumbnails' ); in its functions.php file

Categories