I have the below function which gets wordpress posts and outputs them in a bootstrap grid (3 posts per row). Each array chunk has 3 posts in it. Basically what I want to do is if the chunk is not complete and only has say 2 posts in it then the first post in that chunk gets "col-sm-offset-2" class added. I believe I need some way off counting the posts in the chunk but I'm not sure how to implement.
function post_events($atts) {
global $post;
$args = array(
'post_type' => 'event',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'ASC',
);
$posts = get_posts($args);
$posts_chunks = array_chunk($posts, 3);
$output = '';
foreach ($posts_chunks as $row) {
$output .= '<div class="row">';
foreach ($row as $post) {
setup_postdata($post);
$output .= '<div class="col-md-6 col-sm-6 event-item">';
$output .= '' .get_the_post_thumbnail(). '';
$output .= '<div class="event-item-text">';
$output .= '<h3>' .get_the_title(). '</h3>';
$output .= '<span class="event-date">' .get_the_date("d-m-Y"). '</span>';
$output .= '<p>' .wp_trim_words( get_the_content(), 40, '...' ). '</p>';
$output .= '</div>';
$output .= '</div>';
}
$output .= '</div>';
}
return $output;
}
add_shortcode('post_events','post_events');
You can just use count() on $row, and then set the class based on whether it's the first iteration or not:
$class = 'col-md-6 col-sm-6 event-item';
$count = count($row);
foreach( $row as $k => $post )
{
$cls = ($k == 0 && $count < 2) ? $class.' col-sm-offset-2' : $class;
setup_postdata($post);
$output.= '<div class="'.$cls.'">';
//...
}
Since you have chunked, the subarrays will be 0 based and the first post will be 0. So just check that and if the count is less than 3:
foreach ($posts_chunks as $row) {
$output .= '<div class="row">';
foreach ($row as $key => $post) {
$extra = ''; //set extra class to empty
if($key == 0 && count($row) < 3) { //check first post and count less than 3
$extra = 'col-sm-offset-2 '; //set extra class
}
setup_postdata($post);
//use $extra somewhere
$output .= '<div class="'.$extra.'col-md-6 col-sm-6 event-item">';
$output .= '' .get_the_post_thumbnail(). '';
$output .= '<div class="event-item-text">';
$output .= '<h3>' .get_the_title(). '</h3>';
$output .= '<span class="event-date">' .get_the_date("d-m-Y"). '</span>';
$output .= '<p>' .wp_trim_words( get_the_content(), 40, '...' ). '</p>';
$output .= '</div>';
$output .= '</div>';
}
$output .= '</div>';
}
Related
It should be a slider, which shows all posts, but only 15 posts on each slide. I get all Posts.
(I use Wordpress functions.)
In the -div class = "slide"- there are 15 posts, after that a new -div class = "slide"- with 15 posts should be created.
Here is the code for all posts:
$myposts = get_posts($args);
$result = '<div id="fullpage">';
$result .= '<div class="section" id="section1">';
$result .= '<div class="slide">';
foreach ($myposts as $post) {
$result .= '' . $post->post_title . '';
// the_post_thumbnail('full');
}
$result .= '</div>';
$result .= ' </div>';
$result .= '</div>';
return $result;
After 15 posts, I would like a new slide. I do not know how to adjust the foreach loop. Should I do this with an if statement, or can I do this with a foreach loop?
Use a counter, and reset it after 15. (Not tested, I have no PHP at the moment)
$counter = 1;
foreach ($myposts as $post) {
if ($counter == 1) {
$result .= '<div class="slide">';
}
$result .= '' . $post->post_title . '';
$counter++;
if ($counter == 16) {
$counter = 1;
$result .= '</div>';
}
}
Have the following Array that functions perfectly on the site, but before the first output the word "Array" is printed.
I figure it has to be in the $json_object or $fbdata query but cannot isolate or eliminate it from showing.
<?php
$page_id = '{page_id_here}';
$access_token = '{access_token_here}';
//Get the JSON
$json_object = #file_get_contents('https://graph.facebook.com/' . $page_id .
'/posts?fields=full_picture,link,message&limit=3&access_token=' .
$access_token);
//Interpret data
$fbdata = json_decode($json_object);
foreach ($fbdata->data as $post )
{
$posts .= '<div class="col-sm-4">';
$posts .= '<div class="stay-connected-inner">';
$posts .= '<div class="stay-connected-info">';
$posts .= '<div class="stay-connected-left"><i class="fa fa-facebook"></i></div>';
$posts .= '<div class="stay-connected-right">';
$posts .= '<h5>Title</h5>';
$posts .= '<p>' . $post->message . '</p>';
$posts .= '</div>';
$posts .= '</div>';
$posts .= '<div class="stay-connected-fig">';
$posts .= '<p><img src="' . $post->full_picture . '"></p>';
$posts .= '</div>';
$posts .= '</div>';
$posts .= '</div>';
}
//Display the posts
print_r ($posts)
?>
Declare $posts as a String before your foreach loop.
<?php
$page_id = '{page_id_here}';
$access_token = '{access_token_here}';
//Get the JSON
$json_object = #file_get_contents('https://graph.facebook.com/' . $page_id .
'/posts?fields=full_picture,link,message&limit=3&access_token=' .
$access_token);
//Interpret data
$fbdata = json_decode($json_object);
$posts = ''; // Add this
foreach ($fbdata->data as $post )
{
$posts .= '<div class="col-sm-4">';
$posts .= '<div class="stay-connected-inner">';
$posts .= '<div class="stay-connected-info">';
$posts .= '<div class="stay-connected-left"><i class="fa fa-facebook"></i></div>';
$posts .= '<div class="stay-connected-right">';
$posts .= '<h5>Title</h5>';
$posts .= '<p>' . $post->message . '</p>';
$posts .= '</div>';
$posts .= '</div>';
$posts .= '<div class="stay-connected-fig">';
$posts .= '<p><img src="' . $post->full_picture . '"></p>';
$posts .= '</div>';
$posts .= '</div>';
$posts .= '</div>';
}
//Display the posts
print_r ($posts)
?>
Otherwise it is making $posts into an array, so when it is printed, it also prints that it's an array.
Figured it out. I changed my 'posts' variable to 'postst'. The word 'post' was being used in the URL and that seems to have confused the array in some way.
Updated code:
<?php
$page_id = '{page_id_here}';
$access_token = '{access_token_here}';
//Get the JSON
$json_object = #file_get_contents('https://graph.facebook.com/' . $page_id .
'/posts?fields=full_picture,link,message&limit=3&access_token=' .
$access_token);
//Interpret data
$fbdata = json_decode($json_object);
foreach ($fbdata->data as $post )
{
$postst .= '<div class="col-sm-4">';
$postst .= '<div class="stay-connected-inner">';
$postst .= '<div class="stay-connected-info">';
$postst .= '<div class="stay-connected-left"><i class="fa fa-facebook"></i></div>';
$postst .= '<div class="stay-connected-right">';
$postst .= '<h5>Title</h5>';
$postst .= '<p>' . $post->message . '</p>';
$postst .= '</div>';
$postst .= '</div>';
$postst .= '<div class="stay-connected-fig">';
$postst .= '<p><img src="' . $post->full_picture . '"></p>';
$postst .= '</div>';
$postst .= '</div>';
$postst .= '</div>';
}
//Display the posts
print_r ($postst);
?>
The theme that I have been using for my wp site has related posts shortcode which works, but it doesn't hide the currently viewed post. I had some idea to add a variable before the query to a post so the query sees that post which is currently viewed has that variable set to true and skip it. Here is the code:
<?php
global $post;
$categories = get_the_category();
$ceker=false;
foreach ($categories as $category[0]) {
if ($ceker == false){
$ceker=true;
?>
content etc...
I use this in my own themes and it works. How can I do something similar to these shortcode that this theme uses:
#BLOG LIST...
if(!function_exists('dt_blog_posts')) {
function dt_blog_posts( $atts, $content = null ) {
extract(shortcode_atts(array(
'show_feature_image' => 'true',
'excerpt_length' => 35,
'show_meta' => 'true',
'limit' => -1,
'categories' => '',
'posts_column' => 'one-column', // one-column, one-half-column, one-third-column
), $atts));
global $post;
$meta_set = get_post_meta($post->ID, '_tpl_default_settings', true);
$page_layout = !empty($meta_set['layout']) ? $meta_set['layout'] : 'content-full-width';
$post_layout = $posts_column;
$article_class = "";
$feature_image = "";
$column = ""; $out = "";
//POST LAYOUT CHECK...
if($post_layout == "one-column") {
$article_class = "column dt-sc-one-column blog-fullwidth";
$feature_image = "blog-full";
}
elseif($post_layout == "one-half-column") {
$article_class = "column dt-sc-one-half";
$feature_image = "blog-twocolumn";
$column = 2;
}
elseif($post_layout == "one-third-column") {
$article_class = "column dt-sc-one-third";
$feature_image = "blog-threecolumn";
$column = 3;
}
//PAGE LAYOUT CHECK...
if($page_layout != "content-full-width") {
$article_class = $article_class." with-sidebar";
$feature_image = $feature_image."-sidebar";
}
//POST VALUES....
if($categories == "") $categories = 0;
//PERFORMING QUERY...
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
$args = array('post_type' => 'post', 'paged' => $paged, 'posts_per_page' => $limit, 'cat' => $categories, 'ignore_sticky_posts' => 1);
$wp_query = new WP_Query($args);
$pholder = dt_theme_option('general', 'disable-placeholder-images');
if($wp_query->have_posts()): $i = 1;
while($wp_query->have_posts()): $wp_query->the_post();
$temp_class = $format = "";
if($i == 1) $temp_class = $article_class." first"; else $temp_class = $article_class;
if($i == $column) $i = 1; else $i = $i + 1;
$out .= '<div class="'.$temp_class.'"><!-- Post Starts -->';
$out .= '<article id="post-'.get_the_ID().'" class="'.implode(" ", get_post_class("blog-post", $post->ID)).'">';
if($show_meta != "false"):
$out .= '<div class="post-details"><!-- Post Details Starts -->';
$out .= '<div class="date"><span>'.get_the_date('d').'</span>'.get_the_date('M').'<br />'.get_the_date('Y').'</div>';
$out .= '<div class="post-comments">';
$commtext = "";
if((wp_count_comments($post->ID)->approved) == 0) $commtext = '0';
else $commtext = wp_count_comments($post->ID)->approved;
$out .= ''.$commtext.' <i class="fa fa-comment"></i>';
$out .= '</div>';
$format = get_post_format();
$out .= '<span class="post-icon-format"> </span>';
$out .= '</div><!-- Post Details ends -->';
endif;
$out .= '<div class="post-content"><!-- Post Content Starts -->';
$out .= '<div class="entry-thumb">';
if(is_sticky()):
$out .= '<div class="featured-post">'.__('Featured','iamd_text_domain').'</div>';
endif;
//POST FORMAT STARTS
if( $format === "image" || empty($format) ):
if( has_post_thumbnail() && $show_feature_image != 'false'):
$out .= '<a href="'.get_permalink().'" title="'.get_the_title().'">';
$attr = array('title' => get_the_title()); $out .= get_the_post_thumbnail($post->ID, $feature_image, $attr);
$out .= '</a>';
elseif($pholder != "true" && $show_feature_image != 'false'):
$out .= '<a href="'.get_permalink().'" title="'.get_the_title().'">';
$out .= '<img src="http://placehold.it/840x340&text='.get_the_title().'" alt="'.get_the_title().'" />';
$out .= '</a>';
endif;
elseif( $format === "gallery" ):
$post_meta = get_post_meta($post->ID ,'_dt_post_settings', true);
$post_meta = is_array($post_meta) ? $post_meta : array();
if( array_key_exists("items", $post_meta) ):
$out .= "<ul class='entry-gallery-post-slider'>";
foreach ( $post_meta['items'] as $item ) { $out .= "<li><img src='{$item}' alt='gal-img' /></li>"; }
$out .= "</ul>";
endif;
elseif( $format === "video" ):
$post_meta = get_post_meta($post->ID ,'_dt_post_settings', true);
$post_meta = is_array($post_meta) ? $post_meta : array();
if( array_key_exists('oembed-url', $post_meta) || array_key_exists('self-hosted-url', $post_meta) ):
if( array_key_exists('oembed-url', $post_meta) ):
$out .= "<div class='dt-video-wrap'>".wp_oembed_get($post_meta['oembed-url']).'</div>';
elseif( array_key_exists('self-hosted-url', $post_meta) ):
$out .= "<div class='dt-video-wrap'>".wp_video_shortcode( array('src' => $post_meta['self-hosted-url']) ).'</div>';
endif;
endif;
elseif( $format === "audio" ):
$post_meta = get_post_meta($post->ID ,'_dt_post_settings', true);
$post_meta = is_array($post_meta) ? $post_meta : array();
if( array_key_exists('oembed-url', $post_meta) || array_key_exists('self-hosted-url', $post_meta) ):
if( array_key_exists('oembed-url', $post_meta) ):
$out .= wp_oembed_get($post_meta['oembed-url']);
elseif( array_key_exists('self-hosted-url', $post_meta) ):
$out .= wp_audio_shortcode( array('src' => $post_meta['self-hosted-url']) );
endif;
endif;
else:
if( has_post_thumbnail() && $show_feature_image != 'false'):
$out .= '<a href="'.get_permalink().'" title="'.get_the_title().'">';
$attr = array('title' => get_the_title()); $out .= get_the_post_thumbnail($post->ID, $feature_image, $attr);
$out .= '</a>';
elseif($pholder != "true" && $show_feature_image != 'false'):
$out .= '<a href="'.get_permalink().'" title="'.get_the_title().'">';
$out .= '<img src="http://placehold.it/840x340&text='.get_the_title().'" alt="'.get_the_title().'" />';
$out .= '</a>';
endif;
endif;
//POST FORMAT ENDS
$out .= '</div>';
$out .= '<div class="entry-detail">';
$out .= '<h2>'.get_the_title().'</h2>';
if($excerpt_length != "" || $excerpt_length != 0) $out .= dt_theme_excerpt($excerpt_length);
$out .= '</div>';
if($show_meta != "true"):
$out .= '<div class="post-meta">';
$out .= '<div class="opsirnije">';
$out .= ''.'Opširnije »'.'';
$out .= '<ul>';
$out .= '<li><span class="fa fa-user"></span>'.get_the_author_meta('display_name').'</li>';
$categories = get_the_category();
$thiscats = "";
if($categories) {
foreach($categories as $category) {
$thiscats .= ''.$category->cat_name.', '; }
$thiscats = substr($thiscats,0, (strlen($thiscats)-2));
$out .= '<li><span class="fa fa-thumb-tack"></span>'.$thiscats.'</li>';
}
$out .= get_the_tag_list('<li><span class="fa fa-pencil"></span>', ', ', '</li>');
$out .= '</ul>';
$out .= '</div>';
endif;
$out .= '</div><!-- Post Content Ends -->';
$out .= '</article>';
$out .= '</div><!-- Post Ends -->';
endwhile;
if($wp_query->max_num_pages > 1):
$out .= '<div class="pagination-wrapper">';
if(function_exists("dt_theme_pagination")) $out .= dt_theme_pagination("", $wp_query->max_num_pages, $wp_query);
$out .= '</div>';
endif;
wp_reset_query($wp_query);
else:
$out .= '<h2>'.__('Nothing Found.', 'iamd_text_domain').'</h2>';
$out .= '<p>'.__('Apologies, but no results were found for the requested archive.', 'iamd_text_domain').'</p>';
endif;
return $out;
}
add_shortcode('dt_blog_posts', 'dt_blog_posts');
add_shortcode('dt_sc_blogposts', 'dt_blog_posts');
}
Answer was simple. Just added
'post__not_in' => array( get_the_ID() ))
in array.
Thx all...
I'm trying to display a "featured products slider" on the Category Page of the WooCommerce shop in such a way that only 'featured' products of the current category (or a specific child category) will be displayed in the slider.
I've placed the code below in the functions.php file of my child-theme.
I tried adding this conditional in archive.php:
if(is_product_category('slug'){echo featured_products_slider_fun($atts, $content);}
I can't figure out how to combine this condition with the function below to essentially show 'featured products' of a certain category'.
===========
function featured_products_slider_func( $atts, $content ) {
extract( shortcode_atts( array(
'num' => 4
), $atts ) );
$return = '';
$first_product_title = "";
$first_product_excerpt = "";
function get_the_popular_excerpt(){
$excerpt = get_the_content();
$excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, 190);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
$excerpt = $excerpt.'. view product';
return $excerpt;
}
$post = new WP_Query(array(
'post_status' => 'publish',
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
'posts_per_page' => $num
));
$return .= '<div class="featured-slider-outer box clearfix"><div class="featured-slider-inner">';
$return .= '<ul class="products featured-slider">';
$selected_class = " selected";
if ($post ->have_posts()) : while ($post ->have_posts()) : $post ->the_post();
global $product;
if( $selected_class != "" ) {
$first_product_title = get_the_title();
$first_product_excerpt = get_the_popular_excerpt();
}
$return .= '<li class="product' . $selected_class . '">';
$return .= get_the_post_thumbnail($post->ID, "blog-two-column");
if ($price_html = $product->get_price_html()) :
$return .= '<span class="price">';
$return .= $price_html;
$return .= '</span>';
endif;
$return .= "<div class='none'>";
$return .= "<h3 id='infos-title'>" . get_the_title() . "</h3>";
$return .= "<div id='infos-excerpt'>" . get_the_popular_excerpt() . "</div>";
$return .= "</div>";
$return .= '</li>';
$selected_class = "";
endwhile;
$return .= '</ul>';
$return .= '</div>';
$return .= '<div class="featured-slider-right">';
$return .= '<button class="btn-left"></button><button class="btn-right"></button><h2>' . $first_product_title . '</h2>';
$return .= '<div class="description">' . $first_product_excerpt . '</div>';
$return .= '</div>';
$return .= '</div>';
else:
$return = __("No featured products found!", "shopifiq");
endif;
return $return;
}
add_shortcode( 'featured_products_slider', 'featured_products_slider_func' );
I've created a variable in template.php that let's me print terms by vocabulary. The problem is that I want to be able to pass in a vocabulary id to select a specific vocabulary. My code looks like this:
function xnalaraart_classic_print_terms($node, $vocabularies){
foreach($vocabularies as $vocabulary){
if($terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid)){
$output .= '<div>';
$output .= '<ul class="links inline">';
foreach ($terms as $term){
$output .= '<li class="taxonomy_term_' . $term->tid . '">';
$output .= l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
$output .= '</li>';
}
$output .= '</ul>';
$output .= '</div>';
}
}
return $output;
}
and in the preprocess_node function:
$vars['terms_split'] = xnalaraart_classic_print_terms($vars['node']);
How do I write it so that I can pass in an id to $vocabularies?
I think you made this more difficult on yourself than it really is. See below for final function.
function xnalaraart_classic_print_vocab_terms($node, $vid){
if($terms = taxonomy_node_get_terms_by_vocabulary($node, $vid)){
$output .= '<div>';
$output .= '<ul class="links inline">';
foreach ($terms as $term){
$output .= '<li class="taxonomy_term_' . $term->tid . '">';
$output .= l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
$output .= '</li>';
}
$output .= '</ul>';
$output .= '</div>';
}
return $output;
}
And then call
$vars['terms_split'] = xnalaraart_classic_print_terms($vars['node'], 10); //Where 10 is the vocab ID