Displaying all images from a WordPress post (including media library) - wordpress

With this function, I'm getting only those images that I uploaded using the upload button in the post section.
But, if I add an image from the media library it's now showing. I want to show all the images from the post.
How can I show all my images from the post?
$attachments = get_children(array('post_parent' => $post->ID,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID'));
foreach($attachments as $att_id => $attachment) {
$full_img_url = wp_get_attachment_url($attachment->ID);
echo wp_get_attachment_image($attachment->ID, 'full');
}

Related

Set sidebar position disabled and hide page title through php wordpress plugin

I've created a plugin for wordpress that can auto post pages on demand
my problem is formatting this pages to a specific template
I need that when the post page is auto inserted in the DB, set the Sidebar position to disabled in order to have a full width page and hide the page title... this options appear in the dashboard and I can click on them one by one, but that has to be automatic, not manually.
$my_post = array(
'post_title' => wp_strip_all_tags( $tituloFichaP1 ),
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 ),
'post_type' => 'page',
'post_parent' => $parentPost,
'page_template' => 'microsite',
'comment_status' => 'open',
);
wp_insert_post( $my_post );
By the way, the page_template attribute ('page_template' => 'microsite') doesn't work either. Te post is inserted but the template is set to default.
Thanks in advance!!!
I just go through you code, you have done a simple mistake, in "page_template" that should be a file name (filename.php) of your template not a template name . for ex. if your template microsite's file name is microsite.php than your code will be.
$my_post = array(
'post_title' => wp_strip_all_tags( $tituloFichaP1 ),
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 ),
'post_type' => 'page',
'post_parent' => $parentPost,
'page_template' => 'microsite.php',
'comment_status' => 'open',
);
wp_insert_post( $my_post );
Hope this will help you.

wordpress - check if image is a post thumbnail of any post

How to check efficiently if a given image of a Wordpress blog is already a post thumbnail (featured image) of any of the posts in the Wordpress blog.
Found a solution which seems to do:
function isPostThumbnail($imageId) {
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => '=',
'value' => $imageId
),
)
);
return sizeof(get_posts( $args )) >0;
}

Random permalinks in Wordpress

I wrote a code to retrieve random wp posts,It works perfectly the problem is it shows recently added media and its permalink.What changes to be made in the following code to retrieve random permalinks instead of recently added.
<?
function get_match( $regex, $content ) {
preg_match($regex, $content, $matches);
return $matches[1];
}
$shortcode_args = shortcode_parse_atts(get_match('/\[gallery\s(.*)\]/isU', $post->post_content));
$ids = $shortcode_args["ids"];
$attachments = get_posts(
array(
'include' => $ids,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'menu_order ID',
'orderby' => 'post__in',
)
);
foreach ($attachments as &$pos) {
$perm = get_permalink($pos->ID);
$img = wp_get_attachment_link( $pos->ID, 'thumbnail',true );
echo(''.$img.'');
}
?>
'post_type' => 'attachment', //retrives all attachements so change that to
'post_type' => 'post',// or your custom_post_type if you want to get only your custom post type
and also remove
'post_mime_type' => 'image',

Need to get the 'GUID' value for WordPress plugin

I have developed a plugin for creating automatic page which containing the following code:
$new_page = array(
'slug' => $_REQUEST[sl_store],
'title' => $_REQUEST[sl_store],
'content' => "$_REQUEST[sl_store] <br> $_REQUEST[sl_address]"
);
$new_page_id = wp_insert_post( array(
'post_title' => $new_page['title'],
'post_type' => 'page',
'post_name' => $new_page['slug'],
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => $new_page['content'],
'post_status' => 'publish',
'post_author' => 1
));
The page is creating fine. However, I need to fetch the "GUID" value for the newly created page from the DB.
Can anyone help me?
There's a get_the_giud() function (see here). So
get_the_guid( $new_page_id );
should do what you need.

Link to first image attachment in a wordpress post

How would I create a text link to the first image attachment page in a wordpress post without trying to figure out what the slug will be after it is published. I realize that I can link the images to their attachment page but I wan't to be able to create a text link. Is this possible?
Figured it out:
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'offset' => 0,
'orderby' => 'menu_order',
'order' => 'asc',
'post_status' => null,
'post_parent' => $post->ID,
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
if(wp_attachment_is_image( $attachment->ID )) {
echo 'LINK TEXT HERE';
break;
}
}
}
?>

Resources