Make Target _Blank With This Attacment PHP - wordpress

this php below will print like this http://example.com/wp-content/uploads/2013/01/imagename.jpg with anchor text 'DOWNLOAD'
<?php
if ( $attachments = get_children( array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
)));
foreach ($attachments as $attachment) {
echo wp_get_attachment_link( $attachment->ID, '' , false, true, 'Download');
}
?>
1.when user clicking this link, how to target in _blank or open in new tab.
2.is possible this short code combine with Javascript to make Force download link? look like bellow.
if ( $attachments = get_posts( array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => -1,
'post_status' => 'any',
'post_parent' => $post->ID,
) ) );
foreach ( $attachments as $attachment ) {
echo '<a href="javascript:void(0);"
onclick="document.execCommand(\'SaveAs\', true, \'' . get_permalink( $attachment->ID ) . '\');">
Download This Wallpaper</a>';
}

This is what I meant below in my response.
array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID ,
'target' => 'target="_blank"';
)
See if it works this way.

amek teh chages and try this
foreach ( $attachments as $attachment ) {
echo '<a href="javascript:void(0);"
onclick="document.execCommand(\'SaveAs\', true, \'' . get_permalink( $attachment->ID ) . '\');" target="blank">
Download This Wallpaper</a>';
}
hope this will helpt you

Related

Need example code for wp_playlist_shortcode

I trying to use wp_playlist_shortcode for creating playlist with audio-files from all blog posts.
In official documentation i saw this parameter:
'ids'
(array) Create a playlist out of these explicit attachment IDs. If empty, a playlist will be created from all $type attachments of $id. Default empty.
I trying this code, and its doesn't working:
$attch_id = array('76', '73', '70', '67');
wp_playlist_shortcode( array( 'ids' => '$attch_id' );
How to create playlist with audio-files from all blog posts? Now i use this code, but it is a not playlist.
$audios = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => null, 'post_parent' => null, 'post_mime_type' => 'audio/mpeg' );
$attachments = get_posts( $audios );
if ($attachments) {
foreach ( $attachments as $post ) {
setup_postdata($post);
$media_url = $post->guid;
$media_title = $post->post_title;
echo wp_audio_shortcode( array( 'src' => $media_url) );
echo '<p>' . $media_title . '</p>';
// print_r($media_url);
}
}
wp_reset_postdata();

Getting Thumbnail id in wordpress

I want to get Thumbnail and full size images from custume post type.
Can anyone help me with how I can get the the title of image and thumbnail id in wordpress?
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => 'any',
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_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 );
}
}
?>
Try this hope it's help for you

In Wordpress, wp_query with special attribute

When I use the WP_Query, I want to filter them by titles' initial letter, Like I only want the post when the initial is between 'F-J', what should I do with it.
$query_arguments = array(
'post_type' => $atts['_type'],
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => $atts['postsPerPage'],
'ignore_sticky_posts'=> 1,
'paged' => $paged
);
$trombinoscope_query = new WP_Query($query_arguments);
You can try the mysql solution described here.
Something as :
<?php
$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE (post_title like 'F%' OR post_title like 'G%' OR post_title like 'I%' OR post_title like 'F%') AND post_status='publish'");
if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild );
//Your code comes here.
endforeach; endif;
?>
I add a meta_key, it's fine for me now.
like this
function set_meta_for_employe_post() {
$post_title = get_the_title();
$post_id = get_the_ID();
if ('employe' == get_post_type()) {
if($post_title) {
add_post_meta($post_id, 'initial_letter', $post_title[0], true);
}
}
}
add_action( 'save_post', 'set_meta_for_employe_post');
and after:
$query_arguments = array(
'post_type' => $atts['_type'],
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => $atts['postsPerPage'],
'ignore_sticky_posts'=> 1,
'paged' => $paged,
'meta_key' => 'initial_letter',
'meta_value' => $letters,
);

Display the image attachment of URL in wordpress

I want to display the actual image of an attachment, not the url of the attachment. I am using this code but it is displaying the url:
<?php
$argsThumb = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_status' => null
);
$attachments = get_posts($argsThumb);
if ($attachments) {
foreach ($attachments as $attachment) {
apply_filters('the_title', $attachment->post_title);
echo wp_get_attachment_url($attachment->ID, false, false);
}
}
?>
Perhaps try the following:
<?php
$argsThumb = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_status' => null
);
$attachments = get_posts($argsThumb);
if ($attachments) {
foreach ($attachments as $attachment) {
apply_filters('the_title', $attachment->post_title);
echo "<img src='" . wp_get_attachment_url($attachment->ID, false, false) . "' />";
}
}
?>

Display all images of a post with a specific thumbnail size

I'm using this snippet to display all images of a post:
<?php
$argsThumb = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null
);
$attachments = get_posts($argsThumb);
if ($attachments) {
foreach ($attachments as $attachment) {
//echo apply_filters('the_title', $attachment->post_title);
echo '<img src="'.wp_get_attachment_url($attachment->ID, 'testsize', false, false).'" />';
}
}
?>
And this code to create a custom thumbnail size
add_image_size( 'testsize', 400, 400, true );
Unfortunately it doesn't output the images at 400px X 400px, the size is just the original size.
(Note: I re-generated the thumbnails and also added new images to a post, but it still didn't work).
Here's the answer:
wp_get_attachment_url() does not accept parameters.
Using wp_get_attachment_image() instead works.
<?php
$argsThumb = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null
);
$attachments = get_posts($argsThumb);
if ($attachments) {
foreach ($attachments as $attachment) {
//echo apply_filters('the_title', $attachment->post_title);
echo '<img src="'.wp_get_attachment_image($attachment->ID, 'testsize').'" />';
}
}
?>

Resources