WP_Query returns only one post - wordpress

My loop returns only one post instead all. I going through it 4th time and i do not see any reason why it act like that. Thank you for all your help.
localStorage has been cleaned, cache also - and yeah i have more then one post published in this category :)
function rtf_custom_grid( ) {
$args = array(
'post_type' => 'apartamenty',
'post_per_page' => -1,
'nopaging' => true,
'order' => 'date',
'orderby' => 'DESC'
);
$rtf_query = new WP_Query ( $args );
while($rtf_query->have_posts() ) : $rtf_query->the_post();
$cena_1 = get_field('cena_1');
$cena_2 = get_field('cena_2');
$short_dec = get_field('short_desc');
$output = '<div class="single-apartament">';
if ( has_post_thumbnail() ) :
$output .= '<div class="rtf-apartament-thumbnail">';
$output .= '' . get_the_post_thumbnail(get_the_id(), 'large') . '</div>';
endif;
$output .= '<div class="rtf-apartament-content">';
$output .= '' . get_the_title() . '';
$output .= '<div class="rtf-apartament-excerpt">' . $short_dec . '</div>';
$output .= '<div class="rtf-apartament-prize">';
$output .= '<span>' . $cena_1 . ' / ' . $cena_2 . '</span></div>';
$output .= 'Zobacz</div></div></div>';
endwhile;
wp_reset_query();
return $output;
}
add_shortcode('apartamenty', 'rtf_custom_grid');

$output = ' <div class="single-apartament"> ';
the above line is overwriting the previous content in the variable. So its showing only the last post.
Solution :
$output.= '<div class="single-apartament">';
This will append the html code into the variable instead of overwriting it.
Hope it works for you

Related

create a pagination with shortcodes

on my Wordpress site, I created CPT shortcodes, I want there to be 6 posts per page. But when I have more posts, I want the most anician to be visible and to set up a pagination.
how can I add that?
Here is my current code:
function diwp_create_shortcode_newprojects_post_type(){
$args = array(
'post_type' => 'projets',
'posts_per_page' => '6',
'category_name' => 'neufs',
'publish_status' => 'published',
);
$query = new WP_Query($args);
if($query->have_posts()) :
while($query->have_posts()) :
$query->the_post() ;
$content = wpautop( get_the_content() );
/* $result .= '<div class="post-item">';
$result .= '<div class="post-poster">' . get_the_post_thumbnail() . '</div>';
$result .= '<div class="post-name">' . get_the_title() . '</div>';
$result .= '<div class="post-desc">' . get_the_content() . '</div>';
$result .= '</div>'; */
$result .= '<div class="block-actu-list"> <div class="img-actu">' . get_the_post_thumbnail( $post->ID, 'large') . '</div> <div class="legend">' . get_the_title() . '</div></div>';
endwhile;
wp_reset_postdata();
endif;
return $result;
}
add_shortcode( 'newprojects-listed', 'diwp_create_shortcode_newprojects_post_type' );
----------------- Edit -----------------
With #Saqib Amin 's answer I'm trying this, is this correct ?
// CPT shortcode HomePage
function diwp_create_shortcode_project_post_type(){
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'projets',
'posts_per_page' => '10',
'paged' => $paged,
'category_name' => '',
'publish_status' => 'published',
);
$query = new WP_Query($args);
if($query->have_posts()) :
while($query->have_posts()) :
$query->the_post() ;
$content = wpautop( get_the_content() );
/* $result .= '<div class="post-item">';
$result .= '<div class="post-poster">' . get_the_post_thumbnail() . '</div>';
$result .= '<div class="post-name">' . get_the_title() . '</div>';
$result .= '<div class="post-desc">' . get_the_content() . '</div>';
$result .= '</div>'; */
$result .= '<div class="block-actu-list"> <div class="img-actu">' . get_the_post_thumbnail( $post->ID, 'large') . '</div> <div class="legend">' . get_the_title() . '</div></div>';
endwhile;
wp_reset_postdata();
endif;
return $result;
}
add_shortcode( 'project-listed', 'diwp_create_shortcode_project_post_type' );

Post loops with Wordpress + Advanced Custom Fields + WooCommerce

I'm trying to display an ACF field within a Wordpress loop using functions.php to create a shortcode. Unfortunately, the_field('conference_location') does not display.
function show_conferences_func() {
global $post;
$html = "";
$my_query = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => 3,
'product_cat' => 'conferences'
));
if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();
$html .= "<div class=\"conference-block\">";
$html .= "<h2>" . get_the_title() . " </h2>";
$hmtl .= "<p>" . the_field('conference_location') . "</p>";
$html .= "Learn more";
$html .= "</div>";
endwhile; endif;
return $html;
}
add_shortcode( 'show_conferences', 'show_conferences_func' );
You should use
get_field('conference_location')
instead of
the_field('conference_location')
the_field() echo your data. So you're not storing your value.

Translatable output in shortcode

Hello I would like to make part of the output translatable in my shortcode but I don't know how to do it.
I tried several times but even if I managed to add the code, it was displayed outside of the div that outputs the variables so won't work..
My code without translation string is:
add_shortcode('cv', 'vp_cv');
function vp_cv($atts, $content=null) {
extract(shortcode_atts(array(
'number' => 6
), $atts));
global $post;
$output .= '<div class="container">';
$query = new WP_Query('post_type=resume&posts_per_page=' . $number . '&cat=' . $categories);
while($query->have_posts() ) : $query->the_post();
$year = get_post_meta($post->ID, 'resume_year', true);
$title = get_the_title();
$client = get_post_meta($post->ID, 'resume_client', true);
$address = get_post_meta($post->ID, 'resume_address', true);
$output .= '<p class="year">' . $year . '</p>';
$output .= '<p class="cv-title">' . $title . '</p>';
$output .= '<p class="cv-client"> <strong> Client:</strong> ' . $client . '</p>';
$output .= '<p class="cv-address"> <strong> Address:</strong> ' . $address. '</p>';
endwhile;
$output .= '</div>
<div class="clearboth"></div>';
return $output;
}
I'd like to add to the client and address a translatable string like:
<?php _e('Client:','ikos');?>
And it must result inside the tags
Thanks!
Assuming that you're loading text domain correctly, try this:
<?php
// ....
$output .= '<p class="cv-client"> <strong> ' . __( 'Client: ', 'ikos' ) . ' </strong> ' . $client . '</p>';
$output .= '<p class="cv-address"> <strong> ' . __( 'Address: ', 'ikos' ) . ' </strong> ' . $address. '</p>';
// ....
?>
Using __( 'Translatable string', 'your-text-domain' ); return the string translated without echo.
Using _e( 'Translatable string', 'your-text-domain' ); echoes the translated string.
Try it, hope it helps! If something is not clear feel free to ask.

Get the_category outside of loop

I'm working on a site that has custom author pages and on the author pages it has a recent author posts widget which displays other posts by that author. This site has multiple authors so it is different for every post and I haven't found a plugin that does this. I'm trying to display the post thumbnail, title, and category name.
I'm using a function that is displaying the title and the thumbnail, but it doesn't have the category . I tried to add the category in with: the_category(' ','multiple',$authors_post->ID) unfortunately it displays all of the categories in the first li. Instead of for each post.
Here is what I'm working with:
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID,
'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
$output = '<ul>';
foreach ( $authors_posts as $authors_post ) {
$output .= '<li>' . get_the_post_thumbnail( $authors_post->ID, 'xsmall-thumb' )
. '<p>' . the_category(' ','multiple',$authors_post->ID)
. '</p> <a href="' . get_permalink( $authors_post->ID )
. '">' . apply_filters( 'the_title', $authors_post->post_title,
$authors_post->ID ) . '</a></li>';
}
$output .= '</ul>';
return $output;
}
Any help would be greatly appreciated,
Thanks!
Please try this code,
<?php
$cat=1;
$yourcat = get_category($cat);
if ($yourcat)
{
echo '<h2>' . $yourcat->name . '</h2>';
}
?>
To get category name,
try some thing like
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
$output = '<ul>';
foreach ( $authors_posts as $authors_post ) {
$category = get_the_category($authors_post->ID);
foreach($category as $c){
$cat=$c->cat_name.' '.$cat;
}
$output .= '<li>' . get_the_post_thumbnail( $authors_post->ID, 'xsmall-thumb' ) . '<p>' . $cat . '</p> ' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</li>';
}
$output .= '</ul>';
return $output;
}
hope this work for you..

How to modify a theme-bundled widget in child theme?

I want to remove the nofollow code from the latest posts displayed in the sidebar. I found that the code which adds rel=nofollow tag to latest post is located here
theme folder/example theme/lib/activity/plugin.php.
Here is the exact code:
private function get_latest_posts( $post_count ) {
// Get the latest posts
$latest_posts = get_posts(
array(
'numberposts' => $post_count,
'order' => 'desc',
'orderby' => 'date'
)
);
// Create the markup for the listing
$html = '<div class="tab-pane" id="recent">';
$html .= '<ul class="latest-posts">';
if( count( $latest_posts ) > 0 ) {
foreach( $latest_posts as $post ) {
$html .= '<li class="clearfix">';
// Add the small featured image
if( has_post_thumbnail( $post->ID ) ) {
$html .= '<a class="latest-post-tn fademe" href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
$html .= get_the_post_thumbnail( $post->ID, array( 50, 50 ) );
$html .= '</a>';
} // end if
$html .='<div class="latest-meta">';
// Add the title
$html .= '<a href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
$html .= get_the_title( $post->ID );
$html .= '</a>';
// Add date posted
// If there's no title, then we need to turn the date into the link
if( strlen( get_the_title( $post->ID ) ) == 0 ) {
$html .= '<a href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
} // end if
$html .= '<span class="latest-date">';
$html .= get_the_time( get_option( 'date_format' ), $post->ID );
$html .= '</span>';
// Close the anchor
if(strlen( get_the_title( $post->ID ) ) == 0 ) {
$html .= '</a>';
} // end if
$html .='</div>';
$html .= '</li>';
} // end foreach
} else {
$html .= '<li>';
$html .= '<p class="no-posts">' . __( "You have no recent posts.", 'standard' ) . '</p>';
$html .= '</li>';
} // end if/else
$html .= '</ul>';
$html .= '</div>';
return $html;
} // end get_latest_posts
Now please tell me how to remove the nofollow tag from this code using the child theme?
Since you have control of the child theme, you can wrap the call to display the widget zone for that widget with something that grabs the output, performs a regex search/replace on it, and outputs the result. I wrote a blog post about that recently:
Filtering the output of WordPress widgets
The basics are that you have a function that replaces dynamic_sidebar() with your own function, like this:
function theme_dynamic_sidebar($index = 1) {
// capture output from the widgets
ob_start();
$result = dynamic_sidebar($index);
$out = ob_get_clean();
// do regex search/replace on $out
echo $out;
return $result;
}
Seems that you are out of luck.
That's a private function and no filter hooks are offered by the theme author.
You may try to override the include('path/to/plugin.php'); and include your own modified version.

Resources