Detach an image from a page using php

There are no core (4.8.1) functions like detach_post() or wp_update_attachment() but we can use wp_update_post() to detach attachments: $result = wp_update_post( [ ‘ID’ => $attachment_id, ‘post_parent’ => 0, // detach ] ); if( is_wp_error( $result ) ) { // error } else { // success } We can also create a helper function (untested): /** … Read more

Link to the parent post in attachment page

Here’s one way within the main loop on the attachment’s page: First we get the parent ID from the current attachment’s post: $parent_id = get_post_field( ‘post_parent’ ); Then we check if it’s non-zero and print the link to the parent: if ( $parent_id ) { printf( ‘<a href=”https://wordpress.stackexchange.com/questions/312468/%1$s”>%2$s</a>’, esc_url( get_permalink( $parent_id ) ), esc_html__( ‘Go … Read more

Randomize attachment IDs

For attachment posts, the following should do it, i.e. use the wp_insert_attachment_data hook along with get_post(): function my_random_post_id( $data, $postarr ) { // Runs if the post is being created and not updated. if ( empty( $postarr[‘ID’] ) ) { // Locate a yet-unused ID in the (wp_)posts table. do { // Based on the … Read more

Display “large” image size and show caption in attachment page

I’ve tested the following and it works for me. This uses the image’s Title for the ‘alt’ text and displays the image’s caption below the image itself. Use this in content-attachment.php, replacing the current the_content function: $attachment_id = get_the_ID(); // If this attachment is an Image, show the large size if ( wp_attachment_is_image( $attachment_id ) … Read more

Displaying Post Attachment(s) at Top of single.php

Attachments are considered children of the post they’re attached to, so this should work: $images=get_children( array(‘post_parent’=>$post->ID, ‘post_mime_type’=>’image’, ‘numberposts’=>1)); echo wp_get_attachment_image($images[0]->ID, ‘large’); for a large image… replace “large” with the size definition you want or a width,height array.

List Post Attachments – Remove Image Thumbnails

Instead of the_attachment_link use the following: echo wp_get_attachment_link($attachment->ID, ‘full’, false, false, true); wp_get_attachment_link is what’s being called internally by the_attachment_link, the last parameter set to true makes it output a text link instead of an image.

Sorting By Custom Posts With Attachments

Surprisingly, WordPress does not make it very straightforward. Even though lots of people have had similar questions on the wordpress.org forums, I believe yours is unique in that you want to keep the order and sort. This is by no means quality code, but does exactly what you’re looking for. class WP_Query_Custom extends WP_Query { … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)