How can I display the featured icon on frontend? - php

Hello I applied the code I found here.
But I can't figure out how can I call this image on frontend.
I tried this so far:
$args = array('post_type' => 'katastima');
$the_query = new WP_Query($args);
while ( $the_query->have_posts() ) : $the_query->next_post();
$id= $the_query->post->ID;
$location = get_post_meta($id, 'listingimagediv', true);
echo $location;
endwhile;
but I guess this is not the correct way to get the image link from the custom metabox.
Any suggestions?
Thanks in advance.

You get the photo ID from get_post_meta
So the next step is to get the url of the image, see here
$size = 'full';
$icon = null;
$attr = array( "class" => "img-responsive" );
echo wp_get_attachment_image( $location, $size, $icon, $attr );

Related

WordPress replace all instances of [vc_single_image] shortcode with img src

im updating my WordPress website and removing the https://wpbakery.com/ editor and I'm wanting to remove the markup generated by the plugin.
This plugin https://codecanyon.net/item/shortcode-cleaner-clean-wordpress-content-from-broken-shortcodes/21253243 I'm using will remove all shortcodes with no problem except one. It's removing images.
On closer inspection images are posted on the backend using the below shortcode
[vc_single_image image="10879" img_size="large" add_caption="yes" alignment="center"]
And I want to update all references to use HTML instead
<img src="IMGURL">
However, I'm not sure about the right way to do it, any advice, please?
There is a regular expression replace in MySQL 8, but if you don't have that, you could do it with PHP.
$query = new WP_Query( [-- whatever posts you want--] );
while ( $query->have_posts() ) {
$query->the_post();
$content = get_the_content();
preg_match('/\[vc_single_image image="(\d+)" img_size="(\w+)"[^\]]*\]/', $content, $matches);
if( isset($matches[1]) ) {
$url = wp_get_attachment_image_url( (int) $matches[1], $matches[2] );
$img = sprintf( '<img src="%s" />', $url );
$new_content = str_replace( $matches[0], $img, $content );
wp_update_post( [ 'ID' => get_the_ID(), 'post_content' => $new_content] );
}
}
Here Is the updated answer to update all images in the post/page content. The above code finds the first image of the content and update that.
// Args for the WP_Query
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'post__in' => array(5824),
'orderby' => 'post__in'
);
// Execute WP_Query with args
$query = new WP_Query( $args );
// Start the Loop
while ( $query->have_posts() ) {
$query->the_post();
// Get Page/Post content
$content = apply_filters('the_content', $content);
// Get the vc_single_image count from the post
$found_keyword = substr_count( $content,"vc_single_image" );
// Start loop to replace all elements from the content
for ($i=0; $i < $found_keyword; $i++) {
// Get the position of vc_single_image shortcode with Image ID and Image Size
preg_match( '/\[vc_single_image image="(\d+)" img_size="(\w+)"[^\]]*\]/', $content, $matches );
// Check shotcode are exist on loop
if( isset( $matches[1]) ){
// Get the Image ur by Image id and Size
$url = wp_get_attachment_image_url( (int) $matches[1], $matches[2] );
$img = sprintf( '<img src="%s" />', $url );
// Replce shortcode with <img> tag
$content = str_replace( $matches[0], $img, $content );
}
}
// Update post content with updated content with <img> tag
wp_update_post( [ 'ID' => get_the_ID(), 'post_content' => $content] );
}
// END the Loop

How to get WordPress post featured image from post ID in url

I'm trying to get the featured image from the postid passed through the url.
http://www.example.com/schedule-appointment/?postid=589
I've managed to get the postid from the url, but everything goes down hill from there. I must be missing something simple. I'm not a programmer...would love some help.
add_shortcode('CF7_ADD_POST_ID', 'cf7_add_post_id');
function cf7_add_post_id(){
$Path=$_SERVER['REQUEST_URI'];
$control = array();
$control = explode('?', $Path);
$get = $control[1];
$get = explode('=', $get);
$get2 = $get[1];
$args = array(
'post_type' => 'page',
'post__in' => $get2,
);
// Fire up the Query
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ): $the_query->the_post();
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->$get2) );
echo '$feat_image';
};
Try this
<?php
add_shortcode('CF7_ADD_POST_ID', 'cf7_add_post_id');
function cf7_add_post_id(){
$ID = isset( $_GET["postid"] ) ? $_GET["postid"] : false;
if( $ID ){
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $ID ), 'full' );
$url = $thumb['0'];
echo "<img src ='".$url."' alt = 'Image'>";
}
}
?>
There is no need for the WP_Query , You have one id and you can easily get this done by using following code,
add_shortcode('CF7_ADD_POST_ID', 'cf7_add_post_id');
function cf7_add_post_id(){
$postid = $_GET['postid'];
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($postid) );
echo '$feat_image';
};

Get attachment ID

How do i get the "attachement_id="
like here ;
I hope someone can give me an answer. I want to tie this id to a jquery selector for fullscreen images.
It is a little bit complicated, because all attachments are linked to the given posts.
Example: To display all of the images and titles attached to a certain page and display them as a list you can use the following:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo wp_get_attachment_image( $attachment->ID, 'full' );
echo '<br/>';
echo apply_filters( 'the_title', $attachment->post_title );
}
}
endwhile; endif; ?>
Fixed it this way;
foreach ($metas as $metakey) {
$image_id++;
echo "<div class='image' id='img_$image_id'>";
and for the jquery selector;
$('#img\\_<?php echo $image_id ?>').on('click', function () {
this way each image i click, actually is that image. So i got this
$full_image = wp_get_attachment_image_src($metakey['image'], 'full');
<script>
$('#img\\_<?php echo $image_id ?>').on('click', function () {
alert('<?php echo $full_image[0] ?>');
});
</script>
This results that each image i click i get the right url.
Thanks though.
What you looking for is :
$attachment->ID
Take a look at the function wp_get_attachment_image
Reference: Wordpress Attachment
You have to get the $post->ID in order for that to work.
Reference : https://core.trac.wordpress.org/browser/tags/4.0.1/src/wp-includes/link-template.php#L392

How do I get random excerpts to show in WordPress?

Good morning, I been trying to get an excerpt of a post to show random post excerpts on the homepage with no luck. My code looks like this:
<?php
$spotlight = new WP_Query( array( 'post_type' => 'success-spotlight', 'posts_per_page' => 1, 'orderby' => 'rand' ) );
while ( $spotlight->have_posts() ) : $spotlight->the_post();
$spotlight_title = get_the_title();
$spotlight_excerpt = types_render_field("story-headline", array("raw"=>"true"));
$spotlight_link = get_permalink();
if (has_post_thumbnail()) {
$thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'spiffy-success-spotlight-`home');
$spotlight_image = $thumb[0]; // thumbnail url
}
endwhile;
wp_reset_postdata();
?>
<?php
?>
Any suggestions would be greatly appreciated and thank you in advance
How about:
query_posts('orderby=rand&showposts=1&post_type=success-spotlight');

Wordpress: Moving inline images to Featured Galleries

Here's my situation:
I'm redesigning an existing Wordpress site.
The new design separates all images from the actual post content and puts it in a Featured Gallery.
Currently, each post has your typical inline images in the post content.
Is this even remotely possible to extract all inline images from the post content, and create Featured Galleries with those images for each post?
In the past I've done something like the below to grab the first image in the post content, and set it as the standard featured image, but nothing like what I have to do with this dilemma.
function wpforce_featured() {
global $post;
$already_has_thumb = has_post_thumbnail($post->ID);
if (!$already_has_thumb) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
}
}
} //end function
add_action('the_post', 'wpforce_featured');
add_action('save_post', 'wpforce_featured');
add_action('draft_to_publish', 'wpforce_featured');
add_action('new_to_publish', 'wpforce_featured');
add_action('pending_to_publish', 'wpforce_featured');
add_action('future_to_publish', 'wpforce_featured');
Basically the method I used was to create a one time script that I ran to move all inline images from the post to the Featured Gallery, like this:
// Loop over every post
while ( have_posts() ) : the_post();
// Get the images attached to the post.
$imageids = array();
$images = get_attached_media( 'image', $post->ID );
foreach ($images as $image) {
$imageids[] = $image->ID;
}
$comma_separated = implode(",", $imageids);
// Save them to the Featured Gallery (Got this info by digging into the Featured Gallery plugin's source code)
if ( $post->post_type == 'revision' ) {return;}
if ( get_post_meta( $post->ID, 'fg_perm_metadata', FALSE ) ) {
update_post_meta( $post->ID, 'fg_perm_metadata', $comma_separated );
} else {
add_post_meta( $post->ID, 'fg_perm_metadata', $comma_separated );
}
if ( !$comma_separated ) delete_post_meta( $post->ID, 'fg_perm_metadata' );
endwhile;
// Reset Query
wp_reset_query();
Then, I created a function that removes all images in the post content, which I placed in functions.php:
function remove_images( $content )
{
$content =
preg_replace(
array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}',
'{ wp-image-[0-9]*" /></a>}'),
array('<img','" />'),
$content
);
$content = preg_replace('/<img(.*)>/i','', $content, 1);
return $content;
}
add_filter( 'the_content', 'remove_images' );
First preg_replace removes the link around the images. Second removes the image.
the problem is if i read you correctly is that all the images are located as urls in the post content?
You can parse them:
$content = get_the_content();
$html = new DOMDocument;
$html->loadHTML($content);
//get the images
$images = $html->getElementsByTagName('img');
foreach($images as $image=>$key {
$imageurls[]=$key->attributes->getNamedItem("src")->value;//play with this cant remember how it returns object.
}
$content= preg_replace('/<img(.*)>/i','',$content,1);
echo $content;
// you also have a array of images to use.

Categories