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;
}
}
}
?>
Related
How can I render a child page on the parent page in wordpress? I'am building a landing page website, and the idea is to use child pages to make landing page structure.
Now I'am using this code in my parent page template:
$args = array(
'post_type' => 'page',
'post_parent' => $post->ID,
'orderby' => 'rand',
'posts_per_page' => 1,
'no_found_rows' => true
);
$child = new WP_Query($args);
var_dump($child->posts);
But it just gives me an array, and I need fully rendered HTML of my child pages.
Thank you in advance)
Try this code here:
$args = array(
'hierarchical' => 0,
'child_of' => $post->ID,
'parent' => $post->ID,
'sort_column' => 'menu_order, ID',
);
$pages = get_pages( $args );
foreach ( $pages as $post ) : setup_postdata( $post );
// child page html content here
endforeach;
//reset to the main page
wp_reset_postdata();
Finally I've found the proper solution, inspired by Matt Browne's answer
functions.php
function eatwings_show_page($pageid)
{
global $post;
$post = get_page($pageid);
$tpl_slug = get_page_template_slug($post->ID);
$tpl_slug_exp = explode('.', $tpl_slug);
get_template_part($tpl_slug_exp[0]);
}
parent-page-template.php:
$args = array(
'post_type' => 'page',
'post_parent' => $post->ID,
'orderby' => 'menu_order, ID',
'posts_per_page' => 1,
'no_found_rows' => true
);
$child = new WP_Query($args);
foreach($child->posts as $childpage)
{
eatwings_show_page($childpage->ID);
}
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',
I am using wordpress default wp_mail.php to fetch email posts. Now I want to get the attachments from the email and publish them with the post. How can I do that without any plugin?
Thanks in advance.
do you mean like:
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo apply_filters('the_title', $attachment->post_title);
the_attachment_link($attachment->ID, false);
}
}
I have an old WordPress web site. Featured images was not common at that time. so I wrote custom code for thumbnails.
Now I changed my theme, updated WordPress and featured images is a standard for new themes.
I want to set fist (or any) image that uploaded for the post as a featured image for the post.
How can I do that?
Thanks.
I figured it out
function autoset_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', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');
this one is worked for me.
try this:
$args = array(
'post_type' => 'posts',
'numberposts' => -1,
);
$posts = get_posts($args);
foreach($posts as $post_temp){
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_parent' => $post_temp->id,
'orderby' => 'post_date',
'order' => 'DESC'
);
$attachment = get_posts($args);
set_post_thumbnail( $post_temp->id, $attachment->id );
}
I made an Wordpress theme, with pages and posts.
The loop of posts show me a short brief of post and a Continue reading link.
I like this, but how can I make the theme show in the post brief of the loop image(s) attached to post at beginning, if any.
Thank you!
You can get your attached images by using:
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => 1,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_parent' => $post->ID
);
$images = get_posts($args);
and display it like this:
echo wp_get_attachment_image($images[0]->ID, $size='attached-image');
This for getting all attachement images with your post.
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ($attachments) {
foreach ( $attachments as $post ) {
$img = wp_get_attachment_image_src($post->ID, 'medium');
$fullsize = wp_get_attachment_image_src($post->ID, 'full');
}
}
You should add in your loop:
<?php
if(has_post_thumbnail()) {
$theimage = wp_get_attachment_image_src( get_post_thumbnail_id ( $post->ID ), 'thumbnail' );
}
?>
<img class="img_class" src="<?php echo $theimage[0]; ?>" />
Where "thumbnail" correspond to the size you want to show.
Remember that there is also a WordPress specific site in StackExchange