I have function that retrieves the images uploaded by each user. The limit per user is 5 images and I want to display a default image to fill the blanks upto 5 if they have less than 5 uploads. How can I achieve this?
function display_images() {
$imgs = get_images();
$html = '<div class="myImages">';
foreach($imgs as $img) {
$html .= '<div class="myImageContainer"><img src="'. $img .'"/></div>';
}
$html .= '</div>';
return $html;
}
function get_images() {
global $current_user;
get_currentuserinfo();
$args = array(
'author' => $current_user->ID,
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'posts_per_page' => 5,
'orderby' => 'date'
);
$query_images = new WP_Query( $args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= $image->guid;
}
return $images;
}
You have to check for current length of total images:
function display_images() {
$imgs = get_images();
$html = '<div class="myImages">';
foreach($imgs as $img) {
$html .= '<div class="myImageContainer"><img src="'. $img .'"/></div>';
}
if (count($imgs)<5) {
for($i=0; $i<(5-count($imgs)); $i++) {
$html .= '<div class="myImageContainer"><img src="MY_BLANK_IMAGE_LINK"/></div>';
}
}
$html .= '</div>';
return $html;
}
Related
i have created shortcode to show all testimonial on one page.
i have created custom post type to add each testimonial.
can anybody tell me how to show all testimonial on single page .
this code is used to create sshortcode
below i m mentioning code :-
function fn_testimonials_block()
{
$args = array(
'post_type'=> 'testimonials',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
$pages = $the_query->posts;
//echo "<pre>";
//print_r($pages);
//echo "</pre>";
//exit;
$output = '';
$count = 1;
foreach($pages as $page) {
//-----------------
$author = $page->post_title;
$testimonial = $page->post_content;
$page_url = get_page_link( $page->ID );
$author_image = get_the_post_thumbnail_url( $page->ID, 'thumbnail' );
if ($count%2 == 1)
{
$output .= '<div>';
}
$output .= '<div>
<p>'.$testimonial.'<br>
<img src="'.$author_image .'"> <span>'.$author.', </span></p>
</div>';
if ($count%2 == 0)
{
$output .= '</div>';
}
$count++;
}
if ($count%4 != 1){
$output .= '</div>';
}
return $output;
}
I think you will have to pass 'posts_per_page' key in $args array. So your $args array will look like this
$args = array(
'post_type'=> 'testimonials',
'order' => 'ASC',
'posts_per_page' => '-1'
);
I am not sure about the $count variable you are using. I suppose you are trying to implement logic for pagination there but since your question is about showing all testimonials on single page, I won't get into that part.
Making changes in $args will return all testimonials objects.
This how my shortcode function is:
Here I use a global variable $displayed_posts_ids which I want to access and edit in another function; should I redeclare it in the other file?
function posts_mobile($atts)
{
global $displayed_posts_ids;
$html = "";
if (rw_is_mobile())
{
$args = array(
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
'exclude' => array(get_the_id()),
);
$posts_array = get_posts($args);
// var_dump($posts_array);
$ids = get_the_id() . ",";
if (count($posts_array) > 0)
{
$html .= "<div class='battle-block'>";
$html .= "<div id='posts' class='row items-posts'>";
foreach ($posts_array as $post)
{
$html .= show_post_selected($post->ID);
$ids .= $post->ID . ",";
}
$html .= "</div></div>";
}
$ids = rtrim($ids, ",");
$displayed_posts_ids .= $ids;
$html .="<script>
jQuery(document).ready(function(){
$(window).scroll(function(){
if ($(window).scrollTop() == $(document).height()-$(window).height()){
alert('" . $displayed_posts_ids . "');
check_page_end();
}
});
});
</script>";
return $html;
}
}
And here the function where I want to edit and access my global variable in :
function ajax_get_other_posts()
{
$ids_posts = explode(',', $displayed_posts_ids);
if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'ajax_get_other_posts'))
{
$args = array(
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
'exclude' => $ids_posts,
);
$posts_array = get_posts($args);
if (!empty($posts_array))
{
$ids = "";
foreach ($posts_array as $post)
{
$html .= show_post_selected($post->ID);
$ids .= $post->ID . ",";
}
$displayed_posts_ids .= ',' . rtrim($ids, ",");
$params_encoded = json_encode($html);
echo $params_encoded;
}
exit;
}
}
Any help will be appreciated.
Thanks.
If you want to access $displayed_posts_ids in your ajax_get_other_posts() method than you have to again call global variable, like this:
function ajax_get_other_posts(){
global $displayed_posts_ids;
$ids_posts = explode(',', $displayed_posts_ids);
//..
//..
}
Hope this helps!
Can someone show me how to add 2 posts separately using a single query? For example when calling the code: { $content_block[2] .= (fphp_get_related_posts() ); show a post, then { $content_block[4] .= (fphp_get_related_posts() ); show the next post (offset by 1). How can I do this?
At the moment I know I have set 'max' => '1' I'm no php warrior, I was thinking you could 'max' => '2' and call each post like this: (fphp_get_related_posts(1) or (fphp_get_related_posts(2) but that is not right..
//Insert Related Posts Into Content Function
add_filter('the_content', 'mte_add_incontent_post');
function mte_add_incontent_post($content)
{ if(is_single()){
$content_block = explode('<p>',$content);
if(!empty($content_block[2]))
{ $content_block[2] .= (fphp_get_related_posts() );
}
if(!empty($content_block[4]))
{ $content_block[4] .= (fphp_get_related_posts() );
}
for($i=1;$i<count($content_block);$i++)
{ $content_block[$i] = '<p>'.$content_block[$i];
}
$content = implode('',$content_block);
}
return $content;
}
function fphp_get_related_posts($atts) {
$atts = shortcode_atts( array(
'max' => '1',
), $atts, 'relatedposts' );
$reset_post = $post;
global $post;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
$post_tag_ids = array();
foreach($post_tags as $post_tag) $post_tag_ids[] = $post_tag->term_id;
$args=array(
'tag__in' => $post_tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page' => $atts['max'],
'orderby' => 'DESC'
);
$related_query = new wp_query( $args );
if (intval($related_query->post_count) > 0) {
$html = '<div class="related_post"><b>SEE ALSO:</b>';
while( $related_query->have_posts() ) {
$related_query->the_post();
$html .= '<a rel="external" href="'. get_the_permalink(). '">';
$html .= get_the_title() . '</a>';
}
}
$post = $reset_post;
wp_reset_query();
$html .= '</div>';
return $html;
}
}
Would appreciate your help so I can understand how to do this.
I need to display the latest post from 3 categories and two posts from this last one with another HTML formatting. The problem is the last category prints only one post and stops with a var_dump() on the object I can see the two posts.
Check the functions here:
function destaques( $atts ) {
extract( shortcode_atts( array(
'id' => 0,
), $atts ) );
$id = array(6,16,10,4);
$posts = array();
$nomesCat = array();
foreach ($id as $key => $value) {
if ($value == 4){
//this is the categorie with two posts
$posts[] = new WP_Query( array('posts_per_page' => 2, 'category__in' => array($value)));
} else {
$posts[] = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($value)));
}
$nomesCat[] = get_category($value);
}
$html = '<ul class="destaques">';
foreach ($posts as $key => $value) {
if ($value->have_posts()){
while($value->have_posts()){
$value->the_post();
if ($nomesCat[$key]->cat_name == 'Colunistas') {
// check for the categorie name, then call another
// function, passing the wp_query object
$html .= auxiliarColunistas($value);
break;
} else {
//lots of html formatting code
$html .= '</li>';
}
}
}
}
$html .= '</ul>';
return $html;
This is the helper function:
function auxiliarColunistas ($posts) {
$html = '<li class="last">';
/*var_dump($posts); this returns two posts!
die;*/
$html .= '<h2>Colunistas</h2>';
if ($posts->have_posts()){
while ($posts->have_posts()) {
$posts->the_post();
//more html formatting code
}
}
$html .= '</li>';
return $html; }
Why does the loop print just one post and stops?
Try doing it like this
foreach ($id as $key => $value) {
if ($value == 4){
//this is the categorie with two posts
$posts_query = new WP_Query( array('posts_per_page' => 2, 'category__in' => array($value)));
} else {
$posts_query = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($value)));
}
$nomesCat[] = get_category($value);
}
$html = '<ul class="destaques">';
while($posts_query->have_posts()){
if ($nomesCat[$key]->cat_name == 'Colunistas') {
// check for the categorie name, then call another
// function, passing the wp_query object
$html .= auxiliarColunistas($posts_query->the_post());
break;
} else {
//lots of html formatting code
$html .= '</li>';
}
I could solve it changing this line:
if ($value == 4){
//this is the categorie with two posts
$posts_query = new WP_Query( array('posts_per_page' => 2, 'category__in' => array($value)));
to
if ($value == 4){
//this is the categorie with two posts
$posts_query = new WP_Query( array('posts_per_page' => 3, 'category__in' => array($value)));
but I still don't understand why the while loop is not getting the last post.
I have a gallery which should display 3 columns, the first two columns have a certain class and work absolutely fine but I am trying to add a "last class" to my third column - for some reason it's not working. Find my code below:
<?php
$mod =1;
if ( $images = get_posts(array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',)))
{
foreach( $images as $image ) {
$attachmenturl=wp_get_attachment_url($image->ID);
$attachmentimage=wp_get_attachment_image_src( $image->ID, full );
$imageDescription = apply_filters( 'the_description' , $image->post_content );
$imageTitle = apply_filters( 'the_title' , $image->post_title );
if ($mod % 3 == 0) {
$class = "gallery-entry-img-last";
}else{
$class = "gallery-entry-img";
}
echo '<div class="'.$class.'"><div class="gallery-entry-img-l"><a rel="next" href="' . $attachmentimage[0] . '"><span class="rollover" ></span><img src="library/tools/timthumb.php?src=' . $attachmentimage[0] . '&w=270&h=198" alt="" /></div></div>';
}
$mod++;
}
else {
echo "No Image";
}
?>
Some expert advise would be greatly appreciated.
increment your $mod variable inside foreach loop, like:
foreach(.....) {
......
$mod++;
}