Not able to have more than 1 post per column - php

Hi I am working within wordpress and have so far been able to display different sizes for my post loop but I keep getting only 1 post in each column. I have tried adding the multiple post loop into our php and have also set the css for the size but still only 1 post per column.
/* returns carousel html and js embed code */
function get_carousel($id, $get_first_post)
{
$id = intval($id);
if ($id <= 0)
die ('tc-oops7');
global $wpdb;
$carousels_table = $wpdb->prefix . 'touchcarousels';
$slider_row = $wpdb->get_row("SELECT * FROM $carousels_table WHERE id = $id", ARRAY_A);
if(!$slider_row) {
return "<p>Oops, TouchCarousel with ID $id not found.</p>";
}
$carousel_html = '';
$skin_name = $slider_row['skin'];
$css_classes = $slider_row['css_classes'];
$carousel_html .= "<div id=\"touchcarousel-$id\" class=\"touchcarousel $skin_name $css_classes\" style=\"width:{$slider_row['width']}; height:{$slider_row['height']}; \">\n";
$carousel_html .= "\t<ul class=\"touchcarousel-container\">\n";
global $post;
$args = array(
'numberposts' => intval($slider_row['max_posts']),
'posts_per_page' => intval($slider_row['max_posts']),
'offset' => 0,
'cat' => $slider_row['post_categories'],
'orderby' => $slider_row['post_orderby'],
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => $slider_row['post_type'],
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish' );
$post_taxonomies_arr = (array)json_decode (stripslashes($slider_row['post_categories']));
$taxonomies_query_arr = array();
$taxonomies_query_arr['relation'] = $slider_row['post_relation'];
$count = 0;
foreach ($post_taxonomies_arr as $key => $taxonomy ) {
$taxonomies_query_arr[$count]['taxonomy'] = $key;
$taxonomies_query_arr[$count]['terms'] = $taxonomy;
$taxonomies_query_arr[$count]['field'] = 'slug';
$count++;
}
$args['tax_query'] = $taxonomies_query_arr;
$the_query = new WP_Query( $args );
if($get_first_post) {
$the_query->have_posts();
$the_query->the_post();
$this->current_loop_post = $post;
return $post;
}
if($the_query->have_posts()) {
$layout_code = stripslashes($slider_row['layout_code']);
while ($the_query->have_posts()) : $the_query->the_post();$this->current_loop_post = $post;
$do_not_duplicate[] = $post->ID;
$carousel_html .= "\t\t<li class=\"touchcarousel-item\">\n";
$carousel_html .= preg_replace_callback ("/\[tco.*?\](.*?)\[\/tco\]/", array($this, 'format_variables'), $layout_code);
$carousel_html .= "\t\t</li>\n";
endwhile;
$carousel_html .= "\t</ul>\n";
$carousel_html .= "</div>";
$slider_settings = stripslashes($slider_row['js_settings']);
$carousel_js = "";
$carousel_js .= "<script type=\"text/javascript\">\n";
$carousel_js .= "jQuery(document).ready(function($) {";
$carousel_js .= "$(\"#touchcarousel-$id\").touchCarousel(";
$carousel_js .= $slider_settings;
$carousel_js .= ");";
$carousel_js .= "});\n";
$carousel_js .= "</script>";
$carousel_html .= $carousel_js;
} else {
$carousel_html = "<p class=\"tc-posts-not-found\">". __('TouchCarousel Warning: No posts found with selected settings.', 'touchcarousel') ."</p>";
}
wp_reset_postdata();
wp_reset_query();
return stripslashes($carousel_html);
}
The example location can be viewed at http://demo.campaignready.com
So far I have tried calling the posts and divs by their ids and classes but it is still only showing 1 post per column after running the "the_query"

Related

Get Category name in Wordpress Walker Nav Menu

I Write a Custom Nav Walker in My Wordpress Custom Child Theme.
I Need To Detect Current Category in my end_lvl Function. Is there Any Solution ?
function end_lvl(&$output, $depth=0, $args=null) {
$indent = str_repeat( "\t", $depth );
$divder_div_start = "<div class=\"megamenu-divder\">";
$divder_div_end = "</div>";
// Get 4 most recent product IDs in date descending order.
$query = new WC_Product_Query( array(
'limit' => 4,
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
'category' => array( 'md-o-poshak' ),
) );
$topseller_img_tags = '';
$products = $query->get_products();
foreach($products as $p_id){
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $p_id ));
$topseller_img_tags .= "\n<div><img src='$image[0]' width='150' height='150' /></div>";
}
$topseller_div_start = "<div class=\"top-seller-megamenu\">";
$topseller_div_end = "</div>";
if($depth > 0)
{
$output .= "$indent</ul>\n$indent\n$divder_div_start\n$divder_div_end\n$topseller_div_start\n$topseller_img_tags\n$topseller_div_end\n</section>\n";
}
else
{
$output .= "</ul>";
}
}
i need category name.
try to add this:
$cat_name = '';
if (is_category()) {
$category = get_queried_object();
$cat_name = $category->name;
}

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 access and edit global variable from two functions in separated files in WordPress

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!

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.

Categories