I have this function connected to a short code that is supposed to return all stored social profiles. For now it only outputs the first social profile, even though it got two more. I think there is something wrong with the way this code returns the output, I just don't know how to solve it.
function ichi_social_profiles() {
if( $social_profiles = Youxi()->option->get( 'social_profiles' ) ):
?><div class="site-social-profiles">
<ul class="plain-list"><?php
foreach( $social_profiles as $profile ):
$profile = wp_parse_args( $profile, array(
'title' => '',
'url' => '#',
'icon' => 'socicon socicon-500px',
'new_tab' => 0
) );
$output = '<li>';
$output .= '<a href="' . esc_url( $profile['url'] ) . '" title="' . esc_attr( $profile['title'] ) . '"' . ( wp_validate_boolean( $profile['new_tab'] ) ? ' target="_blank"' : '' ) . '>';
$output .= '<span class="' . esc_attr( $profile['icon'] ) . '"></span>';
$output .= '</a>';
$output .= '</li>';
return $output;
endforeach;
?></ul>
</div><?php endif;
}
add_shortcode('some-ikon', 'ichi_social_profiles');
The return statement is too early, inside of the foreach loop, and it is only returning the contents of $output. Also, the div tag, the ul tag and rest of the HTML are being echoed on the page, instead of being returned by the function.
I would set $output = '' at the beginning of the function before the if statement, and append HTML to it as you go, then just return $output at the end, after the endif.
The WordPress Codex has some examples that may also help, though some are much more complex:
https://codex.wordpress.org/Shortcode_API
SOLUTION
I added ob_start() to the beginning of the function before any echo call.
at the end of the function I added:
$response = ob_get_contents();
ob_end_clean();
return $response;
Related
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
I've written a function in my functions.php file that runs a new WP_Query class to fetch some child pages on my custom page template according to their meta key/value. It's kind of working but it's only returning one result - I know there are more as I had the query running properly on the specific page before I turned it into a function. It returned all the correct results, but as I may need this functionality in several pages I decided to turn it into a function.
Here is my function code…
function contact_profiles($args) {
global $post;
$output = "";
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
$output = '<div class="staff-member">'
.'' . get_the_post_thumbnail() . ''
.'<h2 class="name">' . get_the_title() . '</h2>'
.'<h3 class="job-role">' . get_post_meta( $post->ID, 'job_role', true ) . '</h3>'
.'</div>';
endwhile;
wp_reset_postdata();
return $output;
}
and here is how I am calling it in my custom page template…
$myarray = array('meta_key' => 'job_area', 'meta_value' => 'Online', 'post_type' => 'page',);
echo contact_profiles($myarray);
Am I doing something obvious that I shouldn't be? Is the global $post bit causing the issue as I'm not sure I should call it from the functions file.
Most probably you have your posts per page setting to 1 in the back end reading section. If no custom value is passed to a custom query, the default option get_option( 'posts_per_page' ) is used as value to the posts_per_page parameter.
You solution would be to explicitely set posts_per_page to a desired amount or to -1 to get all posts
EDIT
I have missed this before, your concatenation is wrong.
$output = '<div class="staff-member">'
should be
$output .= '<div class="staff-member">'
Here is an updated version of your code
function contact_profiles($args)
{
$output = "";
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
$output .= '<div class="staff-member">'
.'' . get_the_post_thumbnail() . ''
.'<h2 class="name">' . get_the_title() . '</h2>'
.'<h3 class="job-role">' . get_post_meta( $post->ID, 'job_role', true ) . '</h3>'
.'</div>';
endwhile;
wp_reset_postdata();
return $output;
}
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.
I have a related posts by author function using this code:
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' => 10 ) );
$output = '<div class="block">';
foreach ( $authors_posts as $authors_post ) {
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
$output .= '</div>';
return $output; }
To output I use the following code inside the loop of my single.php file:
<?php echo get_related_author_posts(); ?>
Currently the function it displays only post titles as links.
How should look this wp code in order to display thumbnails for this related posts by author function?
Try:
foreach ( $authors_posts as $authors_post ) {
$output .= get_the_post_thumbnail($authors_post->ID);
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
$output .= '</div>';
check get_the_post_thumbnail for more information like image size and attributes like extra classes.
Let me know if you need more help.
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.