I'm trying 3 columns layout of related articles, but if the number of related articles is less than 3 to be different layout. If the number of related articles is 2 the first column to have class col-sm-offset-2, and if the columns is 1 - to have class col-sm-offset-4.
$category = get_post_category();
$relatedArticles = get_posts( [
'exclude' => $post->ID,
'category' => $category->term_id,
'posts_per_page' => 3
] );
?>
<?php if ( count( $relatedArticles ) > 0 ): ?>
<section class="related-articles">
<div class="container">
<div class="row">
<h2 class="text-center"><?php echo __( 'Related Articles' ) ?></h2>
<?php foreach ( $relatedArticles as $relatedArticle ): ?>
<div class="col-sm-4">
<article id="" class="post">
<div class="post-image">
<a href="<?php echo get_permalink( $relatedArticle ) ?>">
<?php echo get_the_post_thumbnail( $relatedArticle, 'post-category' ) ?>
</a>
</div>
<h3 class="post-title">
<a href="<?php echo get_permalink( $relatedArticle ); ?>">
<?php echo get_post_title($relatedArticle) ?>
</a>
</h3>
<div class="post-summary">
<p><?php echo get_summery($relatedArticle);?></p>
</div>
</article>
</div>
<?php endforeach; ?>
</div>
</div>
</section>
<?php endif ?>
You can add an if/else clause for the opening tag of the div which contains the article, adding the desired classes (applied only to the first article - I added a counter for that) in the different versions:
$category = get_post_category(); $relatedArticles = get_posts( [ 'exclude' => $post->ID, 'category' => $category->term_id, 'posts_per_page' => 3 ] ); ?>
<?php if ( count( $relatedArticles ) > 0 ): ?>
<section class="related-articles">
<div class="container">
<div class="row">
<h2 class="text-center">
<?php echo __( 'Related Articles' ) ?>
</h2>
<?php $counter = 1; ?>
<?php foreach ( $relatedArticles as $relatedArticle ): ?>
<?php if ( count( $relatedArticles ) >= 3 ) { ?>
<div class="col-sm-4">
<?php } elseif (( count( $relatedArticles ) == 2) && ($counter == 1)) { ?>
<div class="col-sm-4 col-sm-offset-2">
<?php } elseif (( count( $relatedArticles ) == 1) && ($counter == 1)) { ?>
<div class="col-sm-4 col-sm-offset-4">
<?php } ?>
<article id="" class="post">
<div class="post-image">
<a href="<?php echo get_permalink( $relatedArticle ) ?>">
<?php echo get_the_post_thumbnail( $relatedArticle, 'post-category' ) ?>
</a>
</div>
<h3 class="post-title">
<a href="<?php echo get_permalink( $relatedArticle ); ?>">
<?php echo get_post_title($relatedArticle) ?>
</a>
</h3>
<div class="post-summary">
<p>
<?php echo get_summery($relatedArticle);?>
</p>
</div>
</article>
</div>
<?php $counter++; ?>
<?php endforeach; ?>
</div>
</div>
</section>
<?php endif ?>
Related
i have a custom taxonomy genre, i want to show all taxonomy in genre.php page. when I try to use this code php echo $term->slug;
the result does not meet my expectations :
http://localhost/site/action
I want the result like this :
http://localhost/site/genre/action
I don't know where the error is, I've been looking but haven't found a solution.
this is my code genre.php
<?php
/*
Template Name: Genre
*/
get_header(); ?>
<div class="content">
<div class="main-content">
<div class="main-container">
<div id="list_categories_categories_list">
<?php get_template_part( 'template-parts/ads-bottom' ); ?>
<div class="headline">
<h1>
Genre
</h1>
</div>
<div class="box">
<div class="list-categories">
<div class="margin-fix" id="list_categories_categories_list_items">
<?php
$terms = get_terms( array(
'taxonomy' => 'genre',
'hide_empty' => false,
'number' => 20
) );
foreach ($terms as $term){ ?>
<?php $image = get_term_meta( $term->term_id, 'image', true ); ?>
<a class="item" href="<?php echo $term->slug; ?>" title="<?php echo $term->name; ?>">
<div class="img">
<?php if ( $image != '' ) {
echo wp_get_attachment_image( $image, "", ["class" => "thumb"]);
}
?>
</div>
<strong class="title"><?php echo $term->name; ?></strong>
<div class="wrap">
<div class="videos">0 videos</div>
<div class="rating positive">
81%
</div>
</div>
</a>
<?php } ?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
get_footer();
You can use this code it will give results as per your requirement.
<?php
/*
Template Name: Genre
*/
get_header(); ?>
<div class="content">
<div class="main-content">
<div class="main-container">
<div id="list_categories_categories_list">
<?php get_template_part( 'template-parts/ads-bottom' ); ?>
<div class="headline">
<h1>
Genre
</h1>
</div>
<div class="box">
<div class="list-categories">
<div class="margin-fix" id="list_categories_categories_list_items">
<?php
$terms = get_terms( array(
'taxonomy' => 'genre',
'hide_empty' => false,
'number' => 20
) );
foreach ($terms as $term){ ?>
<?php $image = get_term_meta( $term->term_id, 'image', true ); ?>
<a class="item" href="<?php echo get_term_link($term->term_id); ?>" title="<?php echo $term->name; ?>">
<div class="img">
<?php if ( $image != '' ) {
echo wp_get_attachment_image( $image, "", ["class" => "thumb"]);
}
?>
</div>
<strong class="title"><?php echo $term->name; ?></strong>
<div class="wrap">
<div class="videos">0 videos</div>
<div class="rating positive">
81%
</div>
</div>
</a>
<?php } ?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
get_footer();?>
In first img code is working with posts == 5
In second image code getting broken if posts == 3
.row doenst close
How to use wordpress Lopp with Bootstrap grid I wanna to know the best way to optimize my code. This code work correctly but I wanna just to optimize this code I think this isn't the best solution. Please show me how I can optimize this code. Thank you.
<div class="container">
<?php
$args = [
'post_status' => 'publish',
'posts_per_page' => 6,
'no_found_rows' => true,
];
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
if ( $query->current_post === 0 )
{
echo '<div class="row">';
echo '<div class="col-12 col-lg-6">'; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'post 1' ); ?>>
<figure>
<?php the_post_thumbnail('full', array('class' => 'img-fluid') ); ?>
<figure>
<div class="entry">
<div class="entry-title">
<?php the_title(); ?>
</div>
</div>
</article>
<?php
echo '</div>';
}
if ( $query->current_post === 1 )
{
echo '<div class="col-12 col-lg-6">'; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'post 2' ); ?>>
<figure>
<?php the_post_thumbnail('full', array('class' => 'img-fluid') ); ?>
<figure>
<div class="entry">
<div class="entry-title">
<?php the_title(); ?>
</div>
</div>
</article>
<?php
}
if ( $query->current_post === 2 )
{ ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'post 3' ); ?>>
<figure>
<?php the_post_thumbnail('full', array('class' => 'img-fluid') ); ?>
<figure>
<div class="entry">
<div class="entry-title">
<?php the_title(); ?>
</div>
</div>
</article>
<?php
}
if ( $query->current_post === 3 )
{ ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'post 4' ); ?>>
<figure>
<?php the_post_thumbnail('full', array('class' => 'img-fluid') ); ?>
<figure>
<div class="entry">
<div class="entry-title">
<?php the_title(); ?>
</div>
</div>
</article>
<?php
echo '</div>';
echo '</div>';
}
endwhile; wp_reset_postdata(); ?>
</div>
<div class="container">
<div class="row">
<div class="col-6">
<img src="" alt="">
<h1>TITLE</h1>
<div class="exerpt">
exerpt
</div>
</div>
<div class="col-6">
<article class="row">
<div class="col-4">
<img src="" alt="">
</div>
<div class="col-8">
title
</div>
</article>
<article class="row">
<div class="col-4">
<img src="" alt="">
</div>
<div class="col-8">
title
</div>
</article>
<article class="row">
<div class="col-4">
<img src="" alt="">
</div>
<div class="col-8">
title
</div>
</article>
</div>
</div>
<div class="row">
<div class="col-6">
post-4
</div>
<div class="col-6">
post-5
</div>
</div>
You don't need to write the same part of code each time you check a condition.
<div class="container">
<?php
$args = [
'post_status' => 'publish',
'posts_per_page' => 6,
'no_found_rows' => true,
];
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
$post_num = strval($query->current_post + 1);
$post_count = wp_count_posts()->publish;
if ( $query->current_post === 0 || $query->current_post === 4 ) : ?>
<div class="row">;
<?php ;endif; ?>
<?php if ( $query->current_post === 0 || $query->current_post === 1 || $query->current_post === 4 || $query->current_post === 5 ) : ?>
<div class="col-lg-6">'
<?php ;endif; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( $post_num ); ?>>
<figure>
<?php the_post_thumbnail('full', array('class' => 'img-fluid') ); ?>
</figure>
<?php if ( $query->current_post === 0) : ?>
<div class="post_excerpt">
<?php the_excerpt(); ?>
</div>
<?php ;endif; ?>
<div class="entry">
<div class="entry-title">
<?php the_title(); ?>
</div>
</div>
</article>
<?php if ( $query->current_post === 0 || $query->current_post === 3 || $query->current_post === 4 || $query->current_post === 5 || $query->current_post === $post_count-1 ) : ?>
</div>
<?php ;endif; ?>
<?php if ( $query->current_post === 3 || $query->current_post === 5 || $query->current_post === $post_count-1 ) : ?>
</div>
<?php ;endif; ?>
<?php endwhile; wp_reset_postdata(); ?>
</div>
i've got a own wordpress template (still in progress). It includes of course search.php template which looks like this:
<?php
get_header(); ?>
<section class="row page_intro">
<div class="row m0 inner">
<div class="container">
<div class="row">
<h5><?php
/* translators: %s: search query. */
printf( esc_html__( 'Search Results for: %s', 'vetsandpets' ), '<span>' . get_search_query() . '</span>' );
?></h5>
<h1><?php _e('News and veterinary advices', 'vetsandpets'); ?></h1>
</div>
</div>
</div>
</section>
<section class="row breadcrumbRow">
<div class="container">
<div class="row inner m0">
<?php
if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('
<p id="breadcrumbs">','</p>
');
}
?>
</div>
</div>
</section>
<section class="row content_section">
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-8 blog_list">
<?php
global $post;
setup_postdata( $post );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'posts_per_page' => '6',
'paged' => $paged
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="row m0 blog blog2">
<div class="image_row row m0">
<?php the_post_thumbnail('looppostthumbnail', array( 'class' => "img-responsive loop-post-image")); ?>
</div>
<h3><?php echo get_the_title(); ?></h3>
<div class="row m0 meta"><?php _e('Posted on', 'vetsandpets'); ?>: <?php the_time('j F Y'); ?></div>
<p><?php echo excerpt(50); ?></p>
<?php _e('Read more', 'vetsandpets'); ?>
</div> <!--Single Post-->
<?php endwhile; ?>
<?php echo wpse247219_custom_pagination(); ?>
<?php else : ?>
<div class="center"><?php _e('Nope:( no posts yet.', 'vetsandpets'); ?></div>
<?php endif; wp_reset_postdata(); ?>
</div>
<div class="col-sm-12 col-md-4 sidebar">
<div class="row m0 widget categories">
<h5 class="widget_heading"><?php _e('Categories', 'vetsandpets'); ?></h5>
<ul class="list-unstyled">
<?php
$args = array(
'orderby' => 'count',
'depth' => 0,
'title_li' => '',
'use_desc_for_title' => '',
'order' => 'DESC',
'hide_empty' => 0
);
wp_list_categories($args);
?>
</ul>
</div>
<div class="row m0 widget recent_posts">
<h5 class="widget_heading"><?php _e('Recent posts', 'vetsandpets'); ?></h5>
<?php
// the query
$the_query = new WP_Query( array(
'posts_per_page' => 3
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="media recent_post">
<div class="media-left">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail('recentpostthumbnail', array( 'class' => "img-responsive recentpostimage")); ?>
</a>
</div>
<div class="media-body">
<h5><?php the_title(); ?></h5>
<p><?php _e('Posted on', 'vetsandpets'); ?>: <?php the_time('j F Y'); ?></p>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e('Nope:( no posts yet.', 'vetsandpets'); ?></p>
<?php endif; ?>
</div>
</div>
</div>
</div>
</section>
<?php
get_sidebar();
get_footer();
?>
And that's it. Then I have a search form which is included always in a navigation modal. Below you can check the php code:
<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" >
<div>
<input class="form-control" type="search" value="<?php get_search_query(); ?>" id="example-search-input" name="s" id="s" />
<button type="submit" class="btn searchbtn" id="searchsubmit"><?php _e('Submit', 'vetsandpets') ?></button>
</div>
</form>
but it doesnt work - meaning: it always displays all posts.. what im doing wrong? it is somehow linked to arguments in this code/loop?
The problem is that you reset the global $post object by the query_posts() call. As it is stated in the WordPress Docs: This function will completely override the main query and isn’t intended for use by plugins or themes. Its overly-simplistic approach to modifying the main query can be problematic and should be avoided wherever possible.
So, you should delete these lines:
query_posts(array(
'post_type' => 'post',
'posts_per_page' => '6',
'paged' => $paged
));
The first while loop <?php while ( have_posts() ) : the_post(); ?> will already iterate over the search results.
I'm trying to use the Infinite Scroll plugin for Wordpress. I set it up and it doesn't work.
Screen of settings:
Template file
<div id="posts-wrapper" class="row main">
<?php $counter = 1; ?>
<?php if ( have_posts() ){ while ( have_posts() ){ the_post(); ?>
<?php $url = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' ); ?>
<div class="single-post <?php if( $counter == 5 ) { echo "col-xs-12 col-sm-12 col-md-8 col-lg-8"; } else { echo "col-xs-12 col-sm-6 col-md-4 col-lg-4";} ?>">
<div class="thumbnail">
<div class="category-wrapper"><?php the_category(', ');?></div>
<a class="href-overlay" href="<?php the_permalink(); ?>"><div class="wrapper">
<div class="<?php if( has_term( 'hot', 'hot', $post->ID ) ){ echo "inside inside-hot"; } else { echo "inside";} ?>"><div class="image-cover" style="background-image: url(<?php echo $url[0]; ?>)"></div></div>
</div></a>
<div class="post-wrapper">
<p class="post-created-by">Napisane przez <strong><span class="special"><?php echo get_the_author(); ?></span></strong> | <i class="glyphicon glyphicon-comment"></i> <?php comments_number( '0' , '1' , '%' ); ?></p>
<h2 class="post-title"><?php the_title(); ?></h2>
</div>
</div>
</div>
<?php $counter++ ; ?>
<?php }else{ ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php } ?>
</div>
<div id="paginacja">
<?php next_posts_link('wczytaj więcej »')?>
</div>
What am I doing wrong?
I am using Advanced Custom Fields in order to show different banner image for each language with qTranslate plugin.
<?php get_header(); ?>
<section id="linea">
<?php if(qtrans_getLanguage()=='en') {
?>
<img src="<?php the_field('banner')?>">
<?php }
else if(qtrans_getLanguage()=='es') {?>
<img src="<?php the_field('banner_image_es')?>">
<?php }?>
</section>
<section id="categoria1">
<div class="pagewidth clearfix">
<div class="breadcrumbs" xmlns:v="http://rdf.data-vocabulary.org/#">
<?php if(function_exists('bcn_display'))
{
bcn_display();
}?>
</div>
<h1 class="categoria-title"><span class="left"></span><b><?php single_term_title(); ?></b><span class="right"></span></h1>
</div>
</section>
<section id="categoria2">
<div class="pagewidth clearfix">
<ul class="product-list">
<?php $my_query = new WP_Query(array('post_type' => 'productos','taxonomy'=>'categoria','term'=>'conformink-cunero-luxy','paged' => get_query_var('paged'))); ?>
<?php while ( $my_query->have_posts() ) : $my_query->the_post();?>
<li>
<h5><a style="color: #f79646;" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
<a class="verdetalle" href="<?php the_permalink(); ?>"><?php if( qtranxf_getLanguage() == 'es' ){ ?>Ver detalle<?php }else { ?>See More<?php } ?></a>
<!-- <p><?php echo substr(get_the_excerpt(), 0,120); ?></p> -->
<?php the_post_thumbnail('thumbnail'); ?>
</li>
<?php
endwhile;
wp_pagenavi( array( 'query' => $my_query ) );?>
<?php wp_reset_query(); ?>
</ul>
</div>
</section>
<?php get_footer(); ?>
The problem is that it keep showing the image of only one post on all templates, although I have already assigned the different images to different categories.
Can anyone help?
If you want the banner to show up on every post you'll have to move the section inside the loop:
<section id="categoria2">
<div class="pagewidth clearfix">
<ul class="product-list">
<?php
$my_query = new WP_Query(array('post_type' => 'productos','taxonomy'=>'categoria','term'=>'conformink-cunero-luxy','paged' => get_query_var('paged')));
if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post();?>
<li>
<section id="linea">
<?php if ( qtrans_getLanguage() == 'en' ) : ?>
<img src="<?php the_field('banner'); ?>">
<?php elseif ( qtrans_getLanguage() == 'es' ) : ?>
<img src="<?php the_field('banner_image_es'); ?>">
<?php endif; ?>
</section>
<h5><a style="color: #f79646;" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
<a class="verdetalle" href="<?php the_permalink(); ?>"><?php if( qtranxf_getLanguage() == 'es' ){ ?>Ver detalle<?php }else { ?>See More<?php } ?></a>
<!-- <p><?php echo substr(get_the_excerpt(), 0,120); ?></p> -->
<?php the_post_thumbnail('thumbnail'); ?>
</li>
<?php
endwhile; endif;
wp_pagenavi( array( 'query' => $my_query ) );
wp_reset_query(); ?>
</ul>
</div>
</section>