First part of index.php, which currently have big post display contains these entries:
<?php get_header(); ?>
<!-- Begin Content -->
<div id="content">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<div class="p-heading"><h1><?php the_title(); ?></h1></div>
<div class="p-content">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <?php comments_popup_link('Ni komentarjev', 'En komentar', '% komentarjev'); ?></div>
</div>
<?php endwhile; ?>
and the first part of archive.php contains these entries:
<?php get_header(); ?>
<!-- Begin Content -->
<div id="content-a">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<div class="imgdiv"><img src="<?php echo catch_that_image() ?>" width="250"></div>
<div class="p-heading"><h1><?php the_title(); ?></h1></div>
<div class="p-content">
<?php the_excerpt(); ?>
</div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <img src="http://www.virmodrosti.com/wp-content/uploads/comments.png" alt="Komentarji" width="26" height="12"><?php $totalcomments = get_comments_number(); echo $totalcomments; ?></div>
</div>
<?php endwhile; ?>
I use diffrent style div id="content" for big post, div id="content-a" for smaller post display, 3 in a row.
Now, I would like that only the latest post would be display in big format, as defined by #content in css, and the rest just as they are on archive.php with #content-a. How can I do that?
My site main index page is http://www.virmodrosti.com/ and archive is here http://www.virmodrosti.com/zdravje/
Kindly let me know, thank you.
The absolute easiest way to do this would be to use a ::first-child pseudo-selector in CSS.
If that is not an option, you can add a counter to your while loop and use that to add a class to your items.
<?php get_header(); ?>
<!-- Begin Content -->
<div id="content">
<?php
$counter = 0;
if (have_posts()) :
?>
<?php while (have_posts()) : the_post(); ?>
<div class="post count-<?php echo $counter; ?>">
<div class="p-heading"><h1><?php the_title(); ?></h1></div>
<div class="p-content">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <?php comments_popup_link('Ni komentarjev', 'En komentar', '% komentarjev'); ?></div>
</div>
<?php
$counter++;
endwhile; ?>
There are two problems with your css selectors.
1 - You can't use div id='content-a' to style multiple posts because id is unique. You must use class=content-a.
2 - There's no id='content' inside your posts loop if (have_posts()) :
while (have_posts()) : the_post();. The only id=content is outside the loop. It will be applied to all posts no matter what.
The fix is to use a class that is inside the loop. In your code the best one would be post that is the higher div's class.
Then you need to use the while (have_posts()) loop to flag the first post...
index.php
<?php get_header(); ?>
<!-- Begin Content -->
<div id="content">
<?php if (have_posts()) : ?>
<?php
$first_post = true;
while (have_posts()) : the_post(); ?>
<div class="<?php
if ($first_post){
$first_post = false;
echo 'post-first';
}else{
echo 'post';
}
?>">
<div class="p-heading"><h1><?php the_title(); ?></h1></div>
<div class="p-content">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <?php comments_popup_link('Ni komentarjev', 'En komentar', '% komentarjev'); ?></div>
</div>
<?php endwhile; ?>
In archive.php I don't see why you are creating a new WP_Query object after if (have_posts()). But since it's not part of the question nor the problem I'll leave it that way...
archive.php
<?php get_header(); ?>
<!-- Begin Content -->
<div id="content">
<?php if (have_posts()) : ?>
<?php
$query = new WP_Query(array('posts_per_page'=> 2,));
$first_post = true;
while ($query->have_posts()) : $query->the_post(); ?>
<div class="<?php
if ($first_post){
$first_post = false;
echo 'post-first';
}else{
echo 'post';
}
?>"> <div class="p-heading"><h1><?php the_title(); ?></h1></div>
<div class="p-content">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <?php comments_popup_link('Ni komentarjev', 'En komentar', '% komentarjev'); ?></div>
</div>
<?php endwhile; ?>
Related
I'm building a theme and on my category.php page I want to show several full posts (let's say 3, but need to be able to change this to 2 or 1 easily), and then the rest of the posts in the category as title links.
I have quite a bit of HTML in my loop for styling my posts and adding custom fields so sorry about all the code, but this is what my category.php page looks like now. I've tried a few things that haven't worked so have edited this to show my original code which just has a normal list of posts. I'm somewhat new to editing The Loop so would appreciate as much explanation/clarity as possible.
<?php
/**
* The template for displaying Category Archive pages.
*/
get_header(); ?>
<div id="primary" class="<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>">
<div id="feature-container" class="full-width-container">
<div class="full-width-container content-page" id="tagline-wrapper">
<div id="left-black"></div>
<div class="page-width-container">
<div id="tagline-box">
<h1 class="category-title">Transactions</h1>
</div>
</div>
</div>
</div>
<div id="content-wrapper">
<div id="project-menu" class="page-width-container">
<?php wp_nav_menu( array( 'theme_location' => 'project-types' ) ); ?>
</div>
<div id="content" role="main" >
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="story-container" class="module-container">
<div class="our-story">
<div class="story-image">
<?php
// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
</div>
<div class="story-text">
<article class="post" id="post-<?php the_ID(); ?>">
<div class="entry-container">
<h2><?php the_title(); ?></h2>
<div class="project-details">
<p><span class="details-location"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_project-location', true);
wp_reset_query();
?></span><br />
<span class="details-funding"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_funding-type', true);
wp_reset_query();
?> | <?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_funding-source', true);
wp_reset_query();
?></span><br />
<span class="details-value"><?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, '_project-value', true);
wp_reset_query();
?></span></p>
</div>
<div class="entry">
<?php the_content(); ?>
<?php wp_link_pages(array('before' => __('Pages: ','html5reset'), 'next_or_number' => 'number')); ?>
</div>
<?php edit_post_link(__('Edit this entry','html5reset'), '<p>', '</p>'); ?>
</div>
</article>
</div>
</div>
</div>
<?php endwhile; endif; ?>
</div><!-- #content -->
</div>
</div><!-- #primary -->
<?php get_footer(); ?>
You can achive above thing using following code:
First you have to loop all post and and put counter when it reach more then 2 its stop to print a content.but title will be there always.
<?php $countPost=1;?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<h2 id="post-<?php the_ID(); ?>">
<?php the_title(); ?></h2>
<?php if($countPost>2) : /*Condition for Content*/
the_content();
endif;
?>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft">
<?php posts_nav_link('','','« Previous Entries') ?>
</div>
<div class="alignright">
<?php posts_nav_link('','Next Entries »','') ?>
</div>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center"><?php _e("Sorry, but you are looking for something that isn't here."); ?></p>
<?php endif; ?>
</div>
For more details please refer :
https://codex.wordpress.org/The_Loop_in_Action
I figured out a bit of a workaround solution on my own, although it relies on using plugins/widgets which isn't what I'd prefer.
I simply set the Reading settings to display 2 posts, and then below the Loop I added a widget area and used the Recent Posts Extended widget to display a list of titles/links. This widget allows you to skip a certain amount of posts in the list, so I set it to start at post #3. There was no option to show posts from the current category only, so I had to use the Widget Context plugin as well and make individual widgets with a specific category to show on each corresponding category page. As I said, a bit of a convoluted solution, but the end result is exactly what I wanted to achieve.
I created a page template for join all posts from a specific post_type in one page, everything is working except the pagination. It show the right number of posts for page but, in this case, I have 8 posts and it only show 5. I did some changes but without success. Any ideas?
<?php
/*
Template Name: News
*/
?>
<?php get_header(); ?>
<!-- begin colLeft -->
<div class="container">
<main id="main" class="container" role="main">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array('paged' => get_query_var('paged'), 'posts_per_page'=>5, 'post_type'=>'our-work', 'order' => 'ASC'))
?>
<br><br><br>
<!--header-->
<div class="page-header">
<img src="www.wtk.com/img/3428245.png">
</div>
<div class="inform">
Display text here
</div>
<br>
<div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="news-title">
<h3><?php the_title(); ?></a></h3>
</div>
<div class="news-box">
<?php the_post_thumbnail(); ?>
<?php the_excerpt(); ?>
Read more →
</div>
<br><br>
<?php endwhile;?>
<?php if (function_exists("emm_paginate")) {
emm_paginate();
} ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e('Not found'); ?></p>
<?php endif; ?>
</div>
</div>
<!-- end colleft -->
<?php get_footer(); ?>
I can see in your code you have 'posts_per_page' => 5. That would explain why you are seeing only 5 posts on the page.
So in WordPress, if you run the regular loop, Sticky Posts appear twice. Once at the beginning (as desired) and then once in the chronological order among regular posts. Is there a way to make a post sticky and not appear the second time?
Here's the code from the index.php file.
<?php get_header(); ?>
<div id="content">
<div class="sidebar">
<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('Default') ) : ?>
<?php endif; ?>
</div>
<div class="textContainer">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post">
<h1><?php the_title(); ?></h1>
<div class="featuredImage">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'main-thumb' ); } ?>
</div>
<div class="readMore">
<?php // the content of the post
the_content('Read More'); ?>
</div>
<div class="comments">
<?php _e('', 'surplus'); ?> <?php comments_popup_link('{0 Comments}', '{1 Comment}', '{% Comments}'); ?>
</div>
</div>
<?php endwhile; endif; ?>
<div class="nav3">
<?php posts_nav_link(); ?>
</div>
<?php get_footer(); ?>
Thank you!
Liz
Thanks to some answers on here, I've managed to distinguish my posts into the latest post and all the rest. However, it needs to be the oldest post. Here is my current loop:
<?php if (have_posts()) : ?>
<?php $post = $posts[0]; $c=0;?>
<?php while (have_posts()) : the_post(); ?>
<!-- first post -->
<?php $c++;
if( $c == 1) :?>
<div class="container">
<div class="inner_box">
<ul>
<div class="title">
<a href="<?php the_permalink();?>">
<?php the_title(); ?>
</div>
<?php the_content(); ?>
</a>
</ul>
<div class="down">a</div>
</div>
</div>
<?php else :?>
<!-- second post -->
<div class="container">
<div class="inner_box">
<ul>
<div class="title">
<a href="<?php the_permalink();?>">
<?php the_title(); ?>
</div>
<?php the_content(); ?>
</a>
</ul>
<div class="up">b</div>
</div>
</div>
<?php endif; ?>`
I saw somewhere that you can use a while loop to target the last post from a post.length. However, I am unsure how to implement this.
Yes, you're right. Use count.
Suppose the total posts is 5 for $total_posts = count($posts);.
You'll have to check your $counter for total - 1 as the array is $posts[0], $posts[1], $posts[2], $posts[3], $posts[4].
<?php
if ( have_posts() ) :
$counter = 0;
$total_posts = count($posts) - 1;
while ( have_posts() ) :
the_post();
if( $counter == $total_posts ) {
?>
<div class="container"><?php // CUSTOM CODE FOR LAST ARTICLE ?></div>
<?php
} else {
?>
<div class="container"><?php // CUSTOM CODE FOR THE REST OF ARTICLES ?></div>
<?php
}
$counter++;
endwhile;
endif;
Note that you only need to open <?php and close ?> when changing from PHP to HTML, using them at every line is very confusing.
On the home page of my site I want the latest post to be the biggest then the older posts to be smaller and below it. See image
I have created a wordpress loop which partly does the job, Ive zoomed out so you can get a clearer view.
<?php if (have_posts()): ?>
<section class="latest-blog">
<?php query_posts('showposts=5'); ?>
<?php $i = 0; while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!-- Post Title -->
<h1>
<?php the_title(); ?>
</h1>
<!-- /Post Title -->
<?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
<br class="clear">
<?php edit_post_link(); ?>
</article>
</section>
<section class="archive">
<?php if(++$i === 1): ?>
<?php endif; ?>
<?php endwhile; ?>
</section>
<?php endif; ?>
What seems to be happening is each old post gets given the section archive where as I want all old posts to be inside the section archive as articles.
If I understand your question, I don't think you need multiple loops, rather I think you could just use a "special" case in your loop to handle the first most recent post, but then treat all the older posts normally (it looks like you're trying to do it the other way round?).
How about this:
<?php
$firstPost = true;
query_posts('showposts=5');
while (have_posts()) {
the_post();
if ($firstPost) {
?>
<section class="latest-blog">
my_article();
</section><!-- /latest-blog -->
<section class="archive">
<?php
$firstPost = false;
} // end of if(firstPost)
?>
my_article();
<?php
} // end of the loop
?>
</section><!-- /archive -->
<?php
function my_article() {
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!-- Post Title -->
<h1><?php the_title(); ?></h1>
<!-- /Post Title -->
<?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
<br class="clear">
<?php edit_post_link(); ?>
</article>
<?php
}
?>
If from a data point of view, the posts are all the same, there's no real reason I can think of to execute separate queries to retrieve them. Just present the first one differently. Doing so reduces your code which means less places for errors, and reduces DB overhead which means a better performing site.
Also note, the codex for query_posts() suggests this is not an efficient method to do what you're doing. So once you get this working as is, you might want to investigate the WP recommended approaches of using the pre_get_posts action, although that might not be applicable/appropriate in the case where this is a "page".
Break you code into 2 loops.
First loop for the featured post:
<?php query_posts('showposts=1'); ?>
<section class="latest-blog">
<?php $i = 0; while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!-- Post Title -->
<h1>
<?php the_title(); ?>
</h1>
<!-- /Post Title -->
<?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
<br class="clear">
<?php edit_post_link(); ?>
</article>
<?php endwhile; ?>
</section>
And a second loop for the rest of the posts:
<?php wp_reset_query(); ?>
<?php query_posts('showposts=5&offset=1'); ?>
<section class="archive">
<?php $i = 0; while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!-- Post Title -->
<h1>
<?php the_title(); ?>
</h1>
<!-- /Post Title -->
<?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
<br class="clear">
<?php edit_post_link(); ?>
</article>
<?php endwhile; ?>
</section>
You will notice that we use offset=1 in the query to offset the first post from the second loop (so it doesn't appear twice).
Your final code will look something like this:
<?php if (have_posts()): ?>
<?php query_posts('showposts=1'); ?>
<section class="latest-blog">
<?php $i = 0; while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!-- Post Title -->
<h1>
<?php the_title(); ?>
</h1>
<!-- /Post Title -->
<?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
<br class="clear">
<?php edit_post_link(); ?>
</article>
<?php endwhile; ?>
</section>
<?php wp_reset_query(); ?>
<?php query_posts('showposts=5&offset=1'); ?>
<section class="archive">
<?php $i = 0; while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!-- Post Title -->
<h1>
<?php the_title(); ?>
</h1>
<!-- /Post Title -->
<?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
<br class="clear">
<?php edit_post_link(); ?>
</article>
<?php endwhile; ?>
</section>
<?php endif; ?>
You'll want to run 2 loops, one for the main posts, and one for the archived posts. You can't just put a counter in the middle and hope that the HTML will magically format itself properly.
Something like this might work.
<section class="latest-blog">
<?php query_posts('showposts=1'); ?>
<?php while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!-- Post Title -->
<h1>
<?php the_title(); ?>
</h1>
<!-- /Post Title -->
<?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
<br class="clear">
<?php edit_post_link(); ?>
</article>
<?php endwhile; ?>
</section>
<section class="archive">
<?php wp_reset_query(); ?>
<?php query_posts('showposts=5&offset=1'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php // code for "archived" post ?>
<?php endwhile; ?>
</section>
best thing to do is create 2 separate loops,
top loop brings back only 1 post!
the bottom loop returns the other 4
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>
</div>
<div class="subTop">
<?php
query_posts( 'posts_per_page=4' );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>
</div>
hopefully this helps
M