hi i have this code i created in content-gallery.php
but when i use printf to show the number of photos within the post does not show it
it must be print
[This Post Gallery Contains 9 Photos.]
but does not work. why?
when my errors?
<?php
$the_images = get_children( array(
'post_parent' => $post->ID, // Pass the ID of a post or Page to get its children. Pass 0 to get attachments without parent. Pass null to get any child regardless of parent.
'post_mime_type'=> 'image', // A full or partial mime-type, e.g. image, video, video/mp4, which is matched against a post's post_mime_type field.
'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => 'ASC',
'numberposts' => 999
));
if ( !empty($the_images) ) {
$the_total_images = count($the_images);
$the_images = array_slice($the_images, 0, 10); // prin_r form 0 to 10 images
?>
<p><strong>
<?php printf (_n('This Post Gallery Contains %s Photo.', 'This Post Gallery Contains %s Photos.', $the_total_images, 'the-bootstrap'), $the_total_images); ?>
</strong></p>
<?php } ?>
Try using sprintf() instead of printf().
Also, make sure to not have any spaces when using functions. spaces are alright once your within the brackets, but have the space in printf () is bad practice and most of the time errors or doesn't work.
You're checking if the_images is not empty. If it is empty then $the_total_images is never set.
I don't understand your usage of array_slice either. Surely it would be more efficient to only get 10 posts.
Here's how I'd do it:
<?php
global $post;
$the_images = get_posts( array(
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_type' => 'attachment',
'post_status' => 'any',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1,
) );
$the_total_images = ( $the_images ) ? count( $the_images ) : 0;
$image_count_text = sprintf( _n( 'This Post Gallery Contains %s Photo.', 'This Post Gallery Contains %s Photos.', $the_total_images, 'the-bootstrap' ), $the_total_images); ?>
<p><strong><?php echo $image_count_text; ?></strong></p>
$the_total_images is no longer dependent on the outcome of get_posts(). I've left it loading all image attachments but I think a better solution could be found if you're only going to use 10.
Related
I have a WordPress query like this :
<?php $recent = new WP_Query( array( 'tag' => $tags, 'posts_per_page' => '4' ) ); while( $recent->have_posts() ) : $recent->the_post(); ?>
Which gives me the last 4 posts using the $tags.
But any idea of how may I edit this code to do not get the four first posts but from the 4th?
Thanks!
Take a look at the WP_Query Documentation. It goes into detail on how to properly interact with the class.
If you want to query your posts starting from the 4th post, you'll want to take a look at the offset parameter. In your case, take a look at the code below (Note: I moved the arguments array to a variable for clarity)
$recent_args = array(
'tag' => $tags,
'posts_per_page' => 4, // Don't need quotes around integers
'offset' => 3, // Add this param to "Skip" this many posts
);
$recent = new WP_Query( $recent_args );
// Loop through your posts here
If you need to get all posts beyond the first (newest) posts, you can use the offset argument:
<?php $next_posts = new WP_Query( array(
'tag' => $tags,
'offset' => 4,
)
);
while($next_posts->have_posts()) : $next_posts->the_post(); ?>
There is a caveat to this! If you need pagination or set posts_per_page to -1, this will not work. For more information on a more robust solution if you need pagination, checkout the WP documentation:
https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
So here is the code that worked for me :
<?php $recent = new WP_Query(array( 'tag' => $tags, 'posts_per_page' => '4', 'offset' => '3', )); while($recent->have_posts()) : $recent->the_post(); ?>
Thanks a lot to #disinfor & #Xhynk for leading me to the right direction !
I have a page for posts and a different static home page, as well as other common pages, eg About, Sponsors.
I am trying to have in the header of all pages the name of the site and after the title of the last post. I have tried different functions (the_title(), get_the_title()). It works well in the post page, but for all the remaining I get the title of the page ("About" or "Home" or "Sponsors").
I get that the functions I used so far might only work inside the loop, but was wondering if there's some global way to have the title of the last post available everywhere.
I got to: $posts_page = get_option( 'page_for_posts' ) to get the id of the post page and was trying to find a way from there to get the title of the last post, but no success so far.
Is there a simple way to do this?
Try it:
echo current( wp_get_recent_posts( array('numberposts' => 1, 'post_status' => 'publish') ) )['post_title'];
Do you mean the latest/most recent post?
Would this work for you?
<?php
$args = array(
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC'
);
$post = get_posts( $args );
$latest_post_title = $post[0]->post_title;
echo $latest_post_title;
?>
Or maybe this is a bit more light weight, not sure but they both do pretty much the same thing.
<?php
$args = array(
'numberposts' => 1,
);
$recent = wp_get_recent_posts($args);
$last_post_title = $recent[0]['post_title'];
echo $last_post_title;
?>
This is probably a simple one but I can't seem to find an answer anywhere.
I have 2 custom post types. I display them in sections on my home page.
In each section I'd like to have "Last Updated [DATE]" text near the title for each section.
I've found <?php get_lastpostdate( $timezone ) ?> but is there a way of specifying which post type you'd like to query?
[UPDATE]
Here's the final code I used based on Howdy_McGee's answer below. I wanted the date to read as "16th May" for example.
`Thanks, that's the route I started to go down as well. I guess I was hoping to not do another WP_Query, but it works. This is the final code I used:
<p class="right last-update"><?php
$latest = new WP_Query(array('post_type' => 'car', 'post_status' => 'publish', 'posts_per_page' => 1, 'orderby' => 'modified', 'order' => 'ASC'));
if($latest->have_posts()){
$modified_date = $latest->posts[0]->post_modified;
}
//Get the last post update and display the date as "10th March"
$lastpost = strtotime($modified_date);
$lastmonth = date('F', $lastpost);
$lastday = date('jS', $lastpost);
echo 'Last Updated '.$lastday.' '.$lastmonth;
?>
</p>
You can use the_post_modified() function if you're in The Loop. The modified date will change any time the post is changed in any way / updated in any way.
Update
Ok, lets run a small query, what this does is pulls the latest post, modified or new. Since it's just 1 post we can just check if it has posts, and get the first posts modified date.
<?php
$latest = new WP_Query(
array(
'post_type' => 'car',
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'modified',
'order' => 'DESC'
)
);
if($latest->have_posts()){
$modified_date = $latest->posts[0]->post_modified;
}
?>
For a full list of Date Format Options, View Codex. If you're using it outside The Loop, you can use get_the_modified_date() function. Hope it helps!
I needed something similar for a site I was working on and adapted your code slightly if this helps anyone else
<?php
$taxonomy = 'book'; // or post
$latest = new WP_Query(
array(
'post_type' => $taxonomy,
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'modified',
'order' => 'DESC'));
if($latest->have_posts()){
echo 'Last Updated ' . mysql2date('jS F Y', $latest->posts[0]->post_modified);
}
?>
This shows the last modified date of the post_type selected
Update: just realised that someone else posted this answer, I only looked at the 1st post "update", but, meh
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.'" />';
}
}
I've been searching for quite a while now to find a way to limit wordpress tags by date and order them by the amount of times they appeared in the selected timeframe. But I've been rather unsuccesful.
What I'm trying to achieve is something like the trending topics on Twitter. But in this case, 'trending tags'. By default the wordpress tagcloud displays the most popular tags of all time. Which makes no sense in my case, since I want to track current trends.
Ideally it would be something like:
Most popular tags of today
Obama (18 mentions)
New York (15 mentions)
Iron Man (11 mentions)
Robin Hood (7 mentions)
And then multiplied for 'most popular this week' and 'most popular this month'. Does anyone know of a way to achieve this?
Okay, so what I think you probably want is to do this for say, the last 50 posts.
Loop over the last n posts, extract the term_id of each tag for each post, then pass that string into the include argument of wp_tag_cloud();
$how_many_posts = 50;
$args = array(
'posts_per_page' => $how_many_posts,
'orderby' => 'date',
'order' => 'DESC',
);
// get the last $how_many_posts, which we will loop over
// and gather the tags of
query_posts($args);
//
$temp_ids = array();
while (have_posts()) : the_post();
// get tags for each post
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
// store each tag id value
$temp_ids[] = $tag->term_id;
}
}
endwhile;
// we're done with that loop, so we need to reset the query now
wp_reset_query();
$id_string = implode(',', array_unique($temp_ids));
// These are the params I use, you'll want to adjust the args
// to suit the look you want
$args = array(
'smallest' => 10,
'largest' => 30,
'unit' => 'px',
'number' => 150,
'format' => 'flat',
'separator' => "\n",
'orderby' => 'count',
'order' => 'DESC',
'include' => $id_string, // only include stored ids
'link' => 'view',
'echo' => true,
);
wp_tag_cloud( $args );
I'm pretty sure that Tags does not have timestamps - perhaps you could do a search for posts with specific tags for a certain timeperiod?
I think you can look at some of the plugins and see if your have a plugin like what you need
Yo can get the tag list with a query so you don't have to make a loop throw the last X post.
<ul id="footer-tags">
<?php $wpdb->show_errors(); ?>
<?php
global $wpdb;
$term_ids = $wpdb->get_col("
SELECT term_id FROM $wpdb->term_taxonomy
INNER JOIN $wpdb->term_relationships ON $wpdb->term_taxonomy.term_taxonomy_id=$wpdb->term_relationships.term_taxonomy_id
INNER JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->term_relationships.object_id
WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= $wpdb->posts.post_date");
if(count($term_ids) > 0){
$tags = get_tags(array(
'orderby' => 'count',
'order' => 'DESC',
'number' => 28,
'include' => $term_ids,
));
foreach ( (array) $tags as $tag ) {
echo '<li>' . $tag->name . '</li>';
}
}
?>
</ul>