I have created a grid to display posts on a WordPress site. The grid works great and scales to different devices using minmax and auto-fit, but the problem I'm having is that the last 2 spots in the grid on each page are always empty even if I have more posts that can fill the space. For example, on this category page: https://2ndactcostumes.com/category/fans-collars-belts/
I have two pages of posts, but space is being wasted at the bottom of the first page. Any ideas on how to remedy this? Thank you!!
(edited)
Category.php code:
<?php get_header(); ?>
<div class="page-wrapper">
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<h1><?php the_archive_title( '' ); ?></h1>
<hr />
<div class="article-container">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php get_template_part( 'template-parts/content-posts-costume' ); ?>
<?php endwhile; else : ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php endif; ?>
</div>
<?php echo paginate_links(); ?>
</main>
</div>
</div>
<?php get_footer(); ?>
Template-parts code:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( '<h2>', '</h2>' ); ?>
</header>
<div class="entry-content">
<?php the_post_thumbnail( $size = 'thumbnail' ); ?>
<?php the_excerpt(); ?>
</div>
</article>
CSS for the grid:
.category main .article-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(275px, 1fr));
grid-template-rows: repeat(auto-fill, 1fr)
justify-content: space-between;
}
Related
Why can't I see the post content where as I can easily see the post title if I do a wordpress search. I have a custom post type "Artist" and I am using ACF.
<?php
if ( have_posts() ) : ?>
<?php
/* Start the Loop */
while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( sprintf( '<h2 class="entry-title">', esc_url( get_permalink() ) ), '</h2>' ); ?>
</header><!-- .entry-header -->
<div class="entry-summary">
<?php the_content(); ?>
</div><!-- .entry-summary -->
<footer class="entry-footer">
<?php edigital_entry_footer(); ?>
</footer><!-- .entry-footer -->
</article><!-- #post-## -->
You need to put "the content" function call inside the WordPress loop in order for it to output your content to the page like the following.
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
Hope that helps, Cheers!!!
How can I create a full-width post every 4 post grids? As in the following picture.
Here is my code wordpress loop.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('col-sm-6 grid-item '); ?>>
<div class="entry-image">
<?php the_post_thumbnail('grid-thumb'); ?>
<?php unbranded_entry_footer(); ?>
</div>
<header class="entry-header">
<?php
the_title( '<h3 class="entry-title">', '</h3>' );
if ( 'post' === get_post_type() ) : ?>
<div class="entry-meta">
<?php unbranded_posted_on(); ?>
</div><!-- .entry-meta -->
<?php
endif; ?>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php echo unbranded_string_limit_words(get_the_excerpt(), 18); ?>…</p>
</div><!-- .entry-content -->
</article>
<?php endwhile; endif; ?>
May I know are you using visual composer or creating custom template?
If using visual composer you can do it from back-end or place the condition to disable the sidebar from right using
above the sidebar in page.php
<?php if(!is_page('your-page-slug' or ID)) { ?>
/*sidebar code*/
<?php } ?>
How add list post title wordpress in table
I have code in index.php wp theme like this:
<?php
/**
* #WP-Theme Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php if ( have_posts() ) : ?>
<?php if ( function_exists( 'page_navi' ) ) page_navi( 'items=7&prev_label=Prev&next_label=Next&first_label=First&last_label=Last&show_num=1&num_position=after' ); ?><br/>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<div><a class="judul" href="<?php the_permalink(); ?>">
<?php the_title(); ?></a>
<small><div style="text-indent: 10px;">Oleh: <?php the_tags(' '); ?> | Media: <?php the_category(', '); ?> | Terbit: <?php the_time('l, j F Y'); ?> | Dibaca: <?php if(function_exists('the_views')) { the_views(); }?></div></small>
</div>
<?php endwhile; ?>
<br/><?php if ( function_exists( 'page_navi' ) ) page_navi( 'items=7&prev_label=Prev&next_label=Next&first_label=First&last_label=Last&show_num=1&num_position=after' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<?php if ( current_user_can( 'edit_posts' ) ) :
// Show a different message to a logged-in user who can add posts.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'No posts to display', 'twentytwelve' ); ?></h1>
</header>
<div class="entry-content">
<p><?php printf( __( 'Ready to publish your first post? Get started here.', 'twentytwelve' ), admin_url( 'post-new.php' ) ); ?></p>
</div><!-- .entry-content -->
<?php else :
// Show the default message to everyone else.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Nothing Found', 'twentytwelve' ); ?></h1>
</header>
<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
<?php endif; // end current_user_can() check ?>
</article><!-- #post-0 -->
<?php endif; // end have_posts() check ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This result in frontpage:
I want add list post title wordpress in table like this:
What code and where I put its?
<table border="1">
<?php while ( have_posts() ) : the_post(); ?>
<tr><td colspan="2"><a class="judul" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></td></tr>
<tr><td colspan="2">Oleh: <?php the_tags(' '); ?> | Media: <?php the_category(', '); ?> | Terbit: <?php the_time('l, j F Y'); ?> | Dibaca: <?php if(function_exists('the_views')) { the_views(); }?></td></tr>
<?php endwhile; ?>
</table>
You may need to change while loop with above code.
create a <table> tag before starting while loop and close </table> after while loop.
create <tr> in while loop and <td> as per your need.
It looks like you're trying to style the list.
I suggest using a an unordered list to list your post links in this instance:
<?php if (have_posts()) : ?>
<?php if ( function_exists( 'page_navi' ) ) page_navi( 'items=7&prev_label=Prev&next_label=Next&first_label=First&last_label=Last&show_num=1&num_position=after' ); ?><br/>
<ul>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<li>
<a class="judul" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile;
endif;
wp_reset_query();
?>
</ul>
You can style accordingly:
ul {
list-style-type:none;
border: 2px solid #BCE1F1;
}
li:nth-child(odd) {
background: #95DBFE;
}
The nth-child selector above will give every odd numbered list item a different style, depending on what declarations you use. In my example, I gave every odd numbered list item a light blue background.
Hope this helps!
Please do it like this:
tr:nth-child(odd) {
background: #95DBFE;
}
tr:nth-child(even) {
background: #BCE1F1;
}
My Wordpress site displays all the posts, stacked in articles spanning the full width on the index.php using rows (Bootstrap 3).
index.php - HTML
<div>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part( 'content' );
?>
<?php endwhile; ?>
<div class="clearfix"></div>
<div class="col-md-12">
</div>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
</div>
content.php displays the post in each article (which are stacked on top of each other, full width, down the page)
<article id="post-<?php the_ID(); ?>" <?php post_class('container-fluid'); ?>>
<div class="row">
<div class="col-md-12 horiz-cell">
<h2><?php the_title(); ?></h2>
<?php the_category(', '); ?>
</div>
</div>
</article><!-- /#post -->
I have the title and category showing up properly in each row. I would like each post's post-thumbnail (I added its use in functions.php) to be the background image of each row. Filling the whole space (background-size: cover)
Basically large, '100% width' & '300px(roughly) height' rows, each with corresponding title, category, and their post-thumbnail as a background-image.
If not done yet, enable thumbnails and define custom image sizes:
// Enable thumbnails
add_theme_support( 'post-thumbnails' );
add_image_size('my-fun-size',580, 480, true);
To display the thumbnails:
First, get the featured image URL for the post:
The parameter 'my-fun-size' should be the name of the size of the image you defined in your functions.php file - it can also be 'full' or 'thumbnail'
<?php
if (has_post_thumbnail()) {
$thumbnail_data = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'my-fun-size' );
$thumbnail_url = $thumbnail_data[0];
}
?>
Then add the image as a background:
<article id="post-<?php the_ID(); ?>" style="background-image:url('<?php echo $thumbnail_url ?>')" <?php post_class('container-fluid'); ?> >
<div class="row">
<div class="col-md-12 horiz-cell">
<h2><?php the_title(); ?></h2>
<?php the_category(', '); ?>
</div>
</div>
</article><!-- /#post -->
And finally, apply some CSS to achieve your desired background-size:
article {
background-size: cover;
}
It sounds like you've already figured out the CSS part, so here's the PHP/HTML you're looking for:
<article id="post-<?php the_ID(); ?>" <?php post_class('container-fluid'); ?>>
<?php $imgurl = wp_get_attachment_src( get_post_thumbnail_id($post->ID) ); ?>
<div class="row" style="background-image: url('<?php echo $imgurl[0]; ?>');">
<div class="col-md-12 horiz-cell">
<h2><?php the_title(); ?></h2>
<?php the_category(', '); ?>
</div>
</div>
</article><!-- /#post -->
References:
http://codex.wordpress.org/Function_Reference/get_post_thumbnail_id
http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src
I'm having problem in showing posts in my index page in wordpress the title and contents doesnt show. Currently i'm modifying the index.php of twenty-twelve template to show the custom post by id, my goal is to show 3 posts in my index page, here's my code below:
<?php
/**
* The main template file
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* For example, it puts together the home page when no home.php file exists.
*
* #link http://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<br><br>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentytwelve_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<?php if ( current_user_can( 'edit_posts' ) ) :
// Show a different message to a logged-in user who can add posts.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'No posts to display', 'twentytwelve' ); ?></h1>
</header>
<div class="entry-content">
<p><?php printf( __( 'Ready to publish your first post? Get started here.', 'twentytwelve' ), admin_url( 'post-new.php' ) ); ?></p>
</div><!-- .entry-content -->
<?php else :
// Show the default message to everyone else.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Nothing Found', 'twentytwelve' ); ?></h1>
</header>
<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
<?php endif; // end current_user_can() check ?>
</article><!-- #post-0 -->
<?php endif; // end have_posts() check ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
The code above is the default index.php of twenty-twelve template in wordpress. I replaced this code from:
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentytwelve_content_nav( 'nav-below' ); ?>
To this, because i want to show only post 23 by id.
<?php $the_query = new WP_Query( 'page_id=23' ) ?>
<?php while ( $the_query->have_posts() ) : ?>
<?php $the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
So the whole code for my new index.php is:
<?php
/**
* The main template file
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* For example, it puts together the home page when no home.php file exists.
*
* #link http://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header();
?>
<div id="primary" class="site-content">
<div id="content" role="main">
<br><br>
<?php if (have_posts()) : ?>
<?php $the_query = new WP_Query('page_id=23') ?>
<?php while ($the_query->have_posts()) : ?>
<?php $the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<?php
if (current_user_can('edit_posts')) :
// Show a different message to a logged-in user who can add posts.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e('No posts to display', 'twentytwelve'); ?></h1>
</header>
<div class="entry-content">
<p><?php printf(__('Ready to publish your first post? Get started here.', 'twentytwelve'), admin_url('post-new.php')); ?></p>
</div><!-- .entry-content -->
<?php
else :
// Show the default message to everyone else.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e('Nothing Found', 'twentytwelve'); ?></h1>
</header>
<div class="entry-content">
<p><?php _e('Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve'); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
<?php endif; // end current_user_can() check ?>
</article><!-- #post-0 -->
<?php endif; // end have_posts() check ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
That's all what i have guys. Can you help me? Is there anything wrong on showing the posts? Thanks in advance guys. :)
An approach if u dont want to use Wp_query
Try this:
<div id="primary" class="site-content">
<div id="content" role="main">
<br><br>
<?php if (have_posts()) : ?>
<?php $post = get_post(23);?>
<?php echo $post->post_title; ?>
<?php echo $post->post_content; ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<?php
if (current_user_can('edit_posts')) :
// Show a different message to a logged-in user who can add posts.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e('No posts to display', 'twentytwelve'); ?></h1>
</header>
<div class="entry-content">
<p><?php printf(__('Ready to publish your first post? Get started here.', 'twentytwelve'), admin_url('post-new.php')); ?></p>
</div><!-- .entry-content -->
<?php
else :
// Show the default message to everyone else.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e('Nothing Found', 'twentytwelve'); ?></h1>
</header>
<div class="entry-content">
<p><?php _e('Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve'); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
<?php endif; // end current_user_can() check ?>
</article><!-- #post-0 -->
<?php endif; // end have_posts() check ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
You need to display post with id 23. Right?
<?php if (have_posts()) : ?>
<?php $the_query = new WP_Query('p=23') ?>
<?php while ($the_query->have_posts()) : ?>
<?php $the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
You are missing little like this :
$query = new WP_Query( 'p=7' );
ALternate you can try query_posts() for specific post in query like this:
<?php query_posts('p=23'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;?>
<?php get_footer(); ?>