PHP's MOD autogenerates rows bootstrap & wordpress - php

I have this code in my wordpress. Is in the collection.php
This code makes rows when there are 3 posts and when there are 3 posts the closes.
When I have 3, 6 or 9 posts works fine but the problem is when I have 4 or 5 posts because the doesn't close and the code stays open.
Anyone can help me. I appreciate this very much.
Regards
$i = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post();
$i++;
if($i%3 == 1){echo '<div class="row">'; }
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<div class="col-md-4">';
echo '<a href="';
echo the_permalink();
echo '">';
echo wp_get_attachment_image( $attachment->ID, 'full' );
echo '</a>';
echo '<h3 class="category-title"><a href="';
echo the_permalink();
echo '">';
echo the_title();
echo '</a></h3>';
echo '</div>';
}
}
if($i%3 == 0){echo '</div>';}
endwhile; endif;

if($i%3 == 0){echo '</div>';}
endwhile; endif;
change to :
if($i%3 == 0){echo '</div>';}
endwhile; endif;
if($i%3 != 0){echo '</div>';}

Related

wordpress has_post_thumbnail if/else statement not working

I have the following code, it seems to ignore the if/else for the displaying the thumbnail. It will display all thumbnails should a post have a thumbnail set however it will not show the placeholder if there is no thumbnail. Am I blind or is there something wrong with this?
<?php $x = 0;
$displayed = [];
function runQuery($args) {
global $displayed;
global $x;
$query = new WP_Query( $args );
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
$cat_terms = get_the_terms($post->id, 'product_cat');
$datagroups = '';
if ( in_array( get_the_ID(), $displayed ) ){
continue;
}
// update array with currently displayed post ID
$displayed[] = get_the_ID();
foreach ($cat_terms as $key => $cat) {
if (count($cat_terms) == ($key + 1)) {
$datagroups .= '"' . $cat->name . '"';
} else {
$datagroups .= '"' . $cat->name . '", ';
}
}
?>
<div class="flex-item product-single flex-cols-4" data-groups='[<?php echo $datagroups; ?>]' data-date-created="<?php the_modified_date('Y-m-d') ?>" data-title="<?php the_title() ?>">
<figure class="picture-item__inner">
<figcaption class="picture-item__title">
<a data-open="product-<?php echo $x; ?>">
<div class="product-wrap">
<div class="product-image">
<?php if( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail('medium');?>
<?php } else { ?>
<img src="<?php echo get_template_directory_uri(); ?>/armco-old/assets/img/products/placeholder.jpg" alt="Coming Soon" />
<?php } ?>
</div>
<div class="product-title">
<p><?php the_title(); ?></p>
</div>
</div>
</a>
</figcaption>
</figure>
</div>
<?php $x ++;
endwhile;
endif;
wp_reset_postdata();
}?>
<?php if ( $terms && !is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$args = array(
'post_type' => 'products',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $term->slug,
),
),
'order' => 'asc',
);
runQuery($args);
}
} ?>
</div>
Any help would be appreciated as I cannot see what the issue is, when I remove the first if statement and just tell it to echo out the image it works with brining in the image however as soon as it's inside the else statement it doesn't work.
Due to whatever reasoning my original code did not work, so I swapped it to check if the get_the_post_thumbnail() function was empty or not:
<?php if( !empty(get_the_post_thumbnail()) ) { ?>
<?php the_post_thumbnail('medium');?>
<?php } else { ?>
<img src="<?php echo get_template_directory_uri(); ?>/armco-old/assets/img/products/placeholder.jpg" alt="Coming Soon" />
<?php } ?>

Array wordpress php

I want to create a portfolio in wordpress.
It is working properly but I would like to see the categories of the portfolio in h3 under the h2 title.
Other question.
Now the link will only work on the title h2, I wish it were all over the caption as link.
any advice on the code are welcome! Thank you so much
<?php
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
$terms = get_the_terms( $post->ID, 'portfolio-categories' );
if ( $terms && ! is_wp_error( $terms ) ) :
$links = array();
foreach ( $terms as $term ) {
$links[] = $term->name;
}
$tax_links = join( " ", str_replace(' ', '-', $links));
$tax = strtolower($tax_links);
else :
$tax = '';
endif;
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="row-masonry">';
echo '<div class="item">';
echo '<div class="well portfolio-item no-gutter">';
echo '<div class="thumbnail no-gutter">'. get_the_post_thumbnail() .'</div>';
echo '<div class="caption">';
echo '<div class="vertical-align">';
$link = get_the_permalink();
echo "<a href=$link>";
echo '<h2>'. get_the_title() .'</h2>';
I want to see here the portfolio image category in h3
echo "</a>";
echo '</div>'; /*close caption*/
echo '</div>'; /*close caption*/
echo '</div>';
echo '</div>';
endwhile;
echo '</div>';
echo '</div>';
echo '</div>';
?>
</div><!-- #page -->
This should work for you. You can google how to get the category for a post. It will yield your answer - but this will work.
<?php
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
$terms = get_the_terms( $post->ID, 'portfolio-categories' );
if ( $terms && ! is_wp_error( $terms ) ) :
$links = array();
foreach ( $terms as $term ) {
$links[] = $term->name;
}
$tax_links = join( " ", str_replace(' ', '-', $links));
$tax = strtolower($tax_links);
else :
$tax = '';
endif;
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="row-masonry">';
echo '<div class="item">';
echo '<div class="well portfolio-item no-gutter">';
echo '<div class="thumbnail no-gutter">'. get_the_post_thumbnail() .'</div>';
echo '<div class="caption">';
echo '<div class="vertical-align">';
$link = get_the_permalink();
echo "<a href=$link>";
echo '<h2>'. get_the_title() .'</h2>';
// GET THE CATEGORY -- Returns an array of all categories
// $categories = get_the_category();
$categories = get_the_terms( get_the_ID(), 'portfolio-categories' ); // for custom taxnomies
// If not an empty array then show the first category set
if ( ! empty( $categories ) ) {
echo "<h3>" . esc_html( $categories[0]->name ) ."</h3>";
}
echo "</a>";
echo '</div>'; /*close caption*/
echo '</div>'; /*close caption*/
echo '</div>';
echo '</div>';
endwhile;
echo '</div>';
echo '</div>';
echo '</div>';
?>
</div><!-- #page -->

Getting Featured Image as a Background Image

I have a code in my page.php file that creates a list of child pages. I want every li to have a background-image added by Featured image function. Here is the entire code I have
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
<?php
if (is_page('eventsphotography')) {
$query = new WP_query('pagename=eventsphotography');
$eventsphotography_id = $query->queried_object->ID;
//The loop
if($query->have_posts() ) {
while($query->have_posts() ) {
$query->the_post();
the_content();
}
}
/* Get the children of the eventsphotography page */
$args = array (
'post_parent' => $thePostID,
'post_parent' => $eventsphotography_id,
'order' => 'ASC'
);
$eventsphotography_query = new WP_query($args);
//The Loop
if($eventsphotography_query->have_posts() ) {
echo '<ul class="events-list">';
while($eventsphotography_query->have_posts() ){
$eventsphotography_query->the_post();
$background = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
echo '<li style="background:url(' . $background[0] . '); background-repeat:no-repeat; background-size:cover;">';
echo '<div class="events-centered">';
echo '<a href="' . get_permalink() . '">';
echo '<h4>' . get_the_title() . '</h4>';
echo '</a>';
echo '<div class="view-events-details">';
echo '<a href="' . get_permalink() . '">';
echo '<h5>View Images</h5>';
echo '</a>';
echo '</div>';
echo '</div>'; /* end of events-centered */
echo '</li>';
}
echo'</ul>';
}
}
?>
I only need help for these lines:
$background = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
AND
echo '<li style="background:url(' . $background[0] . '); background-repeat:no-repeat; background-size:cover;">';
Here's the screenshot of the result of my code:
http://oi68.tinypic.com/10xzdhl.jpg
I marked the first <li> with a red rectangle. As I said before, I want URL of the featured image to be placed in <li style="background:url(URL of the featured image)">
I have found a solution. First, I created a new WP_Query:
$subs = new WP_Query( array( 'post_parent' => $post->ID, 'post_type' => 'page', 'meta_key' => '_thumbnail_id'));
Then in my loop I added this lines:
if($eventsphotography_query->have_posts() && $subs->have_posts()) {
echo '<ul class="events-list">';
while($eventsphotography_query->have_posts() && $subs->have_posts()){
$eventsphotography_query->the_post();
$subs->the_post();
echo '<li>';
echo get_the_post_thumbnail($post->ID);
...rest of the code...

how to echo/display the custom field value of a specific custom field name?

I have tried this code:
<?php
query_posts(array(
'meta_key' => 'custom_cat',
'meta_value' => 'creative',
'post_type' => 'page'
));
echo '<ul id="creative_services" class="clearfix row">';
if ( have_posts() ) while ( have_posts() ) : the_post();
echo '<li class="col-md-3">';
echo '<a class="popover-dismiss" data-toggle="popover" title="';
echo $post->post_title;
echo '" data-placement="bottom" data-content="';
echo $post->post_content;
echo '"><i class="';
get_post_meta( get_the_ID(), 'fa_icon' );
echo '"></i></a>';
echo '<h3>';
the_title();
echo '</h3>';
endwhile;
echo '</ul>';
wp_reset_query(); ?>
to display the custom field value as a class name, but the value is not displaying. Please help me find the problem or solution to display the custom field value as class name. I'm having hard time to debug this codes because i'm not a programmer, i'm a designer, still in the process of exploring wordpress.
Try adding echo before get_post_meta( get_the_ID(), 'fa_icon' );
so it would be:
<?php
query_posts(array(
'meta_key' => 'custom_cat',
'meta_value' => 'creative',
'post_type' => 'page'
));
echo '<ul id="creative_services" class="clearfix row">';
if ( have_posts() ) while ( have_posts() ) : the_post();
echo '<li class="col-md-3">';
echo '<a class="popover-dismiss" data-toggle="popover" title="';
echo $post->post_title;
echo '" data-placement="bottom" data-content="';
echo $post->post_content;
echo '"><i class="';
echo get_post_meta( get_the_ID(), 'fa_icon' );
echo '"></i></a>';
echo '<h3>';
the_title();
echo '</h3>';
endwhile;
echo '</ul>';
wp_reset_query(); ?>

Alphabetizing posts in query posts / array

please can someone tell me how to alphabetize this?
I've tried quite a few things and nothing seems to work.
<?php query_posts( array('post__not_in' => array(46,2401), 'cat' => $category_id,'posts_per_page'=>'-1')); ?>
<?php while ( have_posts() ) : the_post();
echo '<li';
if (($ID = get_the_ID()) == $pagepostid) echo ' class="proadv-current-item"';
//else echo ' class="post-id'.$ID.'-id'.$pagepostid.'"';
echo '>';
echo '<div class="sponsor-thumb">';
echo '<a href="'.get_permalink().'">';
the_post_thumbnail( 'category-thumb' );
echo '</a>';
echo '</div>';
echo '<a href="'.get_permalink().'">';
the_title();
echo '</a>';
echo '</li>';
endwhile; ?>
<?php wp_reset_query(); ?>
Try specifying orderby in args array:
query_posts( array('post__not_in' => array(46,2401), 'cat' => $category_id,'posts_per_page'=>'-1','orderby'=>'name','order'=>'ASC'));

Categories