So with my blog i have a photo attachment page but it only shows to photo's at a time, and those two photo's are used as the navigation and i hate that.
I want the attachment page to show all the photo's that goes along with the rest of that set.
Here is the current code
<div id="nav-images" class="navigation clearfix">
<div class="nav-next"><?php next_image_link() ?></div>
<div class="nav-previous"><?php previous_image_link() ?></div>
How do i change that to show all the post attachments?
To clarify, this doesn't work anymore - at least with version 3.5.2. I used this instead;
$attachments = get_children(
array(
'post_type' => 'attachment',
'post_parent' => get_the_ID()
)
);
foreach ($attachments as $attachment) {
// ...
}
Only resurrecting an old thread because this one ranks quite highly for this search term.
When you're on a page or post, you can get all of its attachments with the following:
global $post; // refers to the post or parent being displayed
$attachements = query_posts(
array(
'post_type' => 'attachment', // only get "attachment" type posts
'post_parent' => $post->ID, // only get attachments for current post/page
'posts_per_page' => -1 // get all attachments
)
);
foreach($attachements as $attachment){
// Do something exceedingly fancy
}
Since you're currently on an attachment page, you can get all the other attachments using the $post->post_parent value:
global $post; // refers to the attachement object
$attachements = query_posts(
array (
'post_type' => 'attachment', // only get "attachment" type posts
'post_parent' => $post->post_parent, // attachments on the same page or post
'posts_per_page' => -1 // get all attachments
)
);
To then display the attachment images, you can use the wp_get_attachment_image_src function. The attachment's ID will be available in each iteration of your foreach loop as $attachement->ID (if you use the same naming convention as my first example).
Since WordPress 3.6.0 you can also use get_attached_media.
$media = get_attached_media( 'image', $post->ID );
if(! empty($media)){
foreach($media as $media_id => $media_file){
$thumbnail = wp_get_attachment_image_src ( $media_id, 'thumbnail' );
$full = wp_get_attachment_url( $media_id );
echo '<img src="'.$thumbnail[0].'" alt="'.$media_file->post_title.'" />';
}
}
Related
I have a custom post type - "data-result" of which there are 5 published posts.
I have another post type "data-collection-tool" that creates a "data-result" post from the front end when a user visits/edits a 'data-collection-tool' post and this works well.
I want to display a notice in the dashboard when the admin tries to edit a "data-collection-tool" post but where there a one or more "data-result" posts.
Code
function ws48356743_warn_questionnaire_editor() {
if(get_post_type() == 'data-collection-tool' ){
?>
<div class="notice notice-warning is-dismissible">
<p><?php _e( 'Done! '.wp_98435409_checkResultsExist(get_the_ID()), 'sample-text-domain' ); ?></p>
</div>
<?php
}
}
add_action( 'admin_notices', 'ws48356743_warn_questionnaire_editor',1,0 );
To do this I am querying "data-result" in a separate function:
function wp_98435409_checkResultsExist(){
$args_n = array(
'post_type ' => 'data-result',
'post_status' => 'publish',
'posts_per_page' => 9999
);
$p = get_posts($args_n);
print_r($p);
wp_reset_postdata();
}
get_posts returns an empty array and I can't figure out why.
All of the above code runs in functions.php
Does anyone have any pointers?
Thanks
A rogue space stopped this working
$args_n = array(
'post_type' => 'data-result', // <- was here in 'post_type '
'post_status' => 'publish',
'posts_per_page' => 9999
);
I want to display my post text and images separately, like in two different columns.
I followed dew tricks found online. Unfortunatly, they work but not the way I want them to. It's displaying all the images present in media instead of the images which I inserted in the current post. I found the following snippet in almost every solution related to this topic.
Code:
<?php
$args = array( 'post_type' => 'attachment', 'post_status' => 'any', 'post_parent' => $post->ID );
$attachments = get_post($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
echo '<div class="col-md-4 col-sm-4 col-xs-6">';
the_attachment_link( $attachment->ID , flase );
echo '</div>';
}
}
wp_reset_postdata();
?>
Try the following:
$images = get_attached_media('image');
You can then use your existing foreach loop
I have created a page called "Gallery" and created new gallery with 10 images. My page id is 129. I have 10 images in that page id(129).
Now my question is, i need a code to fetch those 10 images in wordpress. please anyone help me with a code. Thanks in advance.
Get all images from post.
function get_all_images() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
return $first_img;
}
Here you get frist image such like you get all other images
Use get_children
I used this code to extract all the images from a page gallery in the chosen order. you can include this code in the loop or use it stand alone. just choose the appropriate post_parent code (see bellow the code example).
This example show all images associated to the page id 129, have a look:
$images = get_children( array( 'post_parent' => 129, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
$images is now a object that contains all images (related to post id 1) and their information ordered like the gallery interface.
if ( $images ) {
//looping through the images
foreach ( $images as $attachment_id => $attachment ) {
?>
<?php /* Outputs the image like this: <img src="" alt="" title="" width="" height="" /> */ ?>
<?php echo wp_get_attachment_image( $attachment_id, 'full' ); ?>
This is the Caption:<br/>
<?php echo $attachment->post_excerpt; ?>
This is the Description:<br/>
<?php echo $attachment->post_content; ?>
<?php
}
}
Find the post id you wan to extract images from and insert it into this argument: 'post_parent' => 129
you can also use:
'post_parent' => $post->ID
If you want to use get_children in a loop, and get the post id from the returned post id.
If you want to exclude the image selected as a featured image i would have a if statement check if the image URL is equal to the featured image URL.
I’d like to create a page for filtering posts based on a number of criteria.
I can work with wp_query and deliver posts quite easily, my problem is that I can’t figure out (nor can I find any answers online about this, believe me I looked) how to let users do this.
Take this for example, returns the posts in order of price (custom field meta value) from highest to lowest with 33 posts.
<?php
$featuredPosts = new WP_Query( array(
'posts_per_page' => 33,
'meta_key'=>'Price',
'orderby' => 'meta_value_num',
'order' => DESC
) );
?>
<?php if ( $featuredPosts->have_posts() ) : ?>
<?php while ( $featuredPosts->have_posts() ) : $featuredPosts->the_post(); ?>
<article <?php post_class('item-post block'); ?> id="post-<?php the_ID(); ?>">
<h2 class="price-title"><?php the_title(); ?> </h2>
</article> <!-- end div post -->
<?php endwhile; wp_reset_query(); ?>
<?php endif; ?>
Now, even after reading and googling, I’ll be damned if I can figure out how I’d implement this on the front end for users to filter posts.
I mean, I know you can append to the URLs in Wordpress to alter the order of posts, but in this context I’m totally lost.
I tried this, but it doesn't work.
<?php
$by_price = esc_url(add_query_arg(array(
'meta_key' => 'price',
'orderby' => 'meta_value_num',
'order' => ASC
)));
$by_date = esc_url(add_query_arg(array(
'orderby' => 'date',
'order' => DESC
)));
?>
<ul>
<li>Order by price</li>
<li>Order by date</li>
</ul>
What I’m trying to achieve is actually quite simple as well, let the user choose the category, choose the price range (guessing I’d write something in JQuery to deliver a value into an field), set the number of results they’d like to be returned.
I’ve tried googling everything under the sun I can think of for this, no dice.
Try Simple Custom Post Order plugin.
It is using AJAX and JavaScript, you don’t have to load anything else. You have to just drag and drop the posts.
OK, I update the code to make it clear:
---I do not think meta_key would be auto-pickup---
functions.php
...
$whitList = array(
'price' => array(
'posts_per_page' => 33,
'meta_key'=>'price',
'orderby'=>'meta_value_num',
'order' => ASC
),
'date' => array(
'posts_per_page' => 33,
'orderby'=>'date',
'order' => DESC
)
);
...
Your first loop php:
<?php
gloabl $whitList; //to use the $whitList in your functions.php.
$aryQuery = $whitList[$_REQUEST['orderby']] ? $whitList[$_REQUEST['orderby']] : $whitList['price'];
$featuredPosts = new WP_Query( $aryQuery );
....
....
?>
For your list page:
<ul>
<?php
gloabl $whitList; //to use the $whitList in your functions.php.
foreach( $whitList as $orderby => $aryOrderBySettings){
?>
<li> Order by <?php echo $orderby;?></li>
<?php
}
?>
</ul>
Using $_GET parameters is the way to go here. First of all you'll want to allow your visitors access to these add these variables. The link approach is fine, overall, so we can generate augmented links by using the add_query_arg to tack on extra parameters to the current URL.
<?php
$urla = add_query_arg( 'sort' => 'price', 'asc' => '1' );
$urld = add_query_arg( 'sort' => 'price', 'asc' => '0' );
?>
Sort by price (asc)
Sort by price (desc)
When clicked, the tacked on variables can thus be detected:
<?php
// Get an allowed sort variable and the order
$sort = isset( $_GET['sort'] ) && in_array( $_GET['sort'], array( 'price' ) ) )
? $_GET['sort'] : null;
$order = isset( $_GET['asc'] ) && $_GET['asc'] == '0' ? 'DESC' : 'ASC';
?>
Now you would augment your main query with the data you just retrieved. If you're using the default way of querying posts on a page you should be able to get away with query_posts, although it is not recomended. And, if you're using a custom loop, simply inject the new arguments into it:
<?php
$args = array();
switch ( $sort ):
case 'price':
$args['order'] = $order;
$args['orderby'] = 'meta_value_num';
$args['meta_key'] = 'price';
break;
default:
break;
endswitch;
$defaults = array( 'posts_per_page' => 33 );
$query = new WP_Query( wp_parse_args( $args, $defaults ) );
?>
You can add more variables, by creating more URLs and buttons to press, and more cases in the switch statement to extend the basic example above.
The first piece of code would go wherever you want your buttons to appear. The second piece of code goes before the third one, which goes before outputting the results.
Hi I´m trying to know if there are someway to remove the relation of the uploaded files with a post WITHOUT deleted the files, when you upload a file and inserted into the post. For example a pdf file. you will get a link inserted into the post. what I want to do is if I remove this link from the post. Delete the reference of this file from the post in the database.
The problem I'm facing is that I'm using a function wich return all the pdf files uploaded into a custom type of post.
function getPdfList(){
global $post, $posts;
$list = array();
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$ext = pathinfo($attachment->guid, PATHINFO_EXTENSION);
if("pdf" == strtolower($ext)){
$list[] = $attachment;
}
}
}
return $list;
}
So then I'm doing this in my php file
$args = array( 'post_type' => 'fuerzabasica', 'posts_per_page' => 40 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$pdfs = getPdfList();
echo '<div class="entry-content" style="">';
//the_content();
foreach ($pdfs as $pdf) {
echo $pdf->guid."<br />";
}
echo '</div>';
endwhile;
?>
The problem is that I'm still getting files wich I remove the link from the post, so if my USER upload new files he will get the old files(which the link was removed) and the new one. Is a way to delete the reference of this files of my post??
WordPress will maintain a parent/child relationship between posts and uploaded files. To prevent this you will have to make sure the post_parent value of every attachment is 0 instead of the parent posts ID.
You could accomplish this with a save_post filter, in which you'll loop through any attachments returned by get_posts(array('post_type' => 'attachment', 'post_parent' => $post_id, 'posts_per_page' => -1)); setting post_parent to 0. $post_id is the first parameter of your filter function.