Wordpress Custom Gallery Function - php

I need to change some code from the WP-Bootstrap Wordpress Files.
In their code they remove the standard gallery code and replace it with their own which works fine accept it doesn't properly account for Rows.
function gallery_shortcode_tbs($attr) {
global $post, $wp_locale;
$output = "";
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
$output = '<div class="row-fluid"><ul class="thumbnails">';
foreach ( $attachments as $attachment ) {
$output .= '<li class="span3">';
$att_title = apply_filters( 'the_title' , $attachment->post_title );
$output .= wp_get_attachment_link( $attachment->ID , 'thumbnail', true );
$output .= '</li>';
}
$output .= '</ul></div>';
}
return $output;
}
could anyone advise on how to edit the above code to insert html code to close the row and start another after the 3rd item.. or any number i choose?
thank you in advance.
R

Take a look at the default WP gallery shortcode source, where a break is inserted after the specified number of columns, on lines 837-838:
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />';

Related

How to show categories and date on posts

I want to categorise posts and show them on my WordPress website. So in order to achieve that I've written a code. My code is below.
function wpb_postsbycategory() {
// the query
$the_query = new WP_Query( array(
'category_name' => 'travel',
'posts_per_page' => 5
) );
// The Loop
if ( $the_query->have_posts() ) {
$string .= '<ul class="postsbycategory widget_recent_entries">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
if ( has_post_thumbnail() ) {
$string .= '<li>';
$string .= '' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</li>';
} else {
// if no featured image is found
$string .= '<li>' . get_the_title() .'</li>';
}
}
} else {
// no posts found
$string .= '<li>No Posts Found</li>';
}
$string .= '</ul>';
return $string;
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('categoryposts', 'wpb_postsbycategory');
This code works perfectly fine. However, I want to show posts meta descriptions as well (i.e post created date and the category). How would I achieve that? Right now It only extracts the post heading and the image. Thanks so much for helping me out.
Try this:
function wpb_postsbycategory() {
// the query
$the_query = new WP_Query( array(
'category_name' => 'travel',
'posts_per_page' => 5
) );
// The Loop
if ( $the_query->have_posts() ) {
$string .= '<ul class="postsbycategory widget_recent_entries">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$string .= '<li>';
$string .= '<a href="' . get_the_permalink() .'" rel="bookmark">';
if ( has_post_thumbnail() ) {
$string .= get_the_post_thumbnail($post_id, array( 50, 50) );
}
// title
$string .= get_the_title();
// date
$string .= get_the_date();
// categories
$categories = get_the_category();
$category_list = join( ', ', wp_list_pluck( $categories, 'name' ) );
$string .= wp_kses_post( $category_list );
// close link
$string .= '</a></li>';
}
// no posts found
$string .= '<li>No Posts Found</li>';
}
$string .= '</ul>';
return $string;
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('categoryposts', 'wpb_postsbycategory');
I guess you need/want to add HTML tags around date and category.
For example:
$string .= '<span class="date">'.get_the_date().'</span>';
But that's up to you.
You can change the output of the categories based on the docs here.

Wordpress Loop inside Shortcode creates infinite page load (post query)

So I have looked at every possible solution and read through most similar questions, I just couldn't find the error inside my shortcode for a post query in wordpress.
This is the complete Code:
function common_cats(){
global $posts;
$parts = array();
$title = get_the_title();
$teile = explode("|", $title);
$args = array(
'posts_per_page' => -1,
'no_found_rows' => true,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true,
'tag_slug__in' => $teile
);
$posts = get_posts($args);
$output = "<ul>";
if( $posts->have_posts() ){
while($posts->have_posts()): $post->the_post();
$output .= "<a href=\"".get_permalink()."\">";
$output .= "<li>".get_the_title(). "<br>";
$output .= "<img class=\"sbf-background\" src=\"".get_the_post_thumbnail_url()."\"></img>";
$output .= get_the_excerpt()."</li>"."</a><!-- ends here -->";
endwhile;
}
$output .= "</ul>";
return $output;
}
add_shortcode('commoncats', 'common_cats');
Through testing I could boil the problem down to this part of the code:
if( $posts->have_posts() ){
while($posts->have_posts()): $post->the_post();
$output .= "<a href=\"".get_permalink()."\">";
$output .= "<li>".get_the_title(). "<br>";
$output .= "<img class=\"sbf-background\" src=\"".get_the_post_thumbnail_url()."\"></img>";
$output .= get_the_excerpt()."</li>"."</a><!-- ends here -->";
endwhile;
wp_reset_postdata();
}
But looking at it I just don't see whats causing my site to break additionally the code will work if I comment out these lines
#if( $posts->have_posts() ){
# while($posts->have_posts()): $post->the_post();
$output .= "<a href=\"".get_permalink()."\">";
$output .= "<li>".get_the_title(). "<br>";
$output .= "<img class=\"sbf-background\" src=\"".get_the_post_thumbnail_url()."\"></img>";
$output .= get_the_excerpt()."</li>"."</a><!-- ends here -->";
# endwhile;
wp_reset_postdata();
#}

how to show all testimonial on single page via shortcode in wordpress?

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.

How to query the first 2 posts in this function but call them separately?

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.

Wordpress output loop into html columns (two divs)?

I have a loop through my custom post types.
I want to select 8 posts only, and print out the output into 2 columns, 4 posts each. I'm not really sure how to approach this. So far I have:
<?php
$args = array( 'post_type' => 'hh_event', 'posts_per_page' => 8 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="entry-date">';
$event_date = get_post_meta($post->ID, 'event_date', true);
echo date('M j',strtotime($event_date));
echo '</div>';
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
?>
A simple solution would be to have your content stored in an array like so:
<?php
$args = array( 'post_type' => 'hh_event', 'posts_per_page' => 8 );
$loop = new WP_Query( $args );
$columns = array( '', '' );
$content = '';
$i = 0;
global $post;
while ( $loop->have_posts() ) : $loop->the_post();
$content = '<div class="entry-date">';
$event_date = get_post_meta($post->ID, 'event_date', true);
$content .= date('M j',strtotime($event_date));
$content .= '</div>';
$content .= get_the_title();
$content .= '<div class="entry-content">';
$content .= apply_filters( 'the_content', $post->post_content );
$content .= '</div>';
$columns[ ($i % 2) ] .= $content;
$i ++;
endwhile;
echo '<div class="column-left">' . $columns[0] . '</div><div class="column-right">' . $columns[1] . '</div>';
?>
So basically you have an array containing two empty strings. You then assign the content of each column to a variable named $content. You then append the value of that variable to the proper part of the $columns variable and increment the counter $i.
Then just echo the contents of each column to a proper wrapping <div> element, or however you are going to separate those into two columns.

Categories