Wordpress looping issue - php

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

Related

How do I get the link of a current WordPress post while it is in the Loop?

Below is my loop:
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php get_post_permalink(); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
Get <?php get_post_permalink(); ?> should display the link yet this is what is being rendered. It is not displaying the permalink of the post
None of the other answers are correct. get_the_permalink() (you can use get_permalink() as well, since it's an alias) RETURNS the data, not ECHO. So, it will never be printed to the screen (most WP functions with get_ prefix work this way.)
You have two options:
Use get_permalink( get_the_ID() ) pass the current post id (if not in the loop) and echo it.
Use the_permalink() which will echo out the permalink (in the loop);
the_permalink():
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php the_permalink(); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
get_permalink():
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php echo get_permalink(); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
This will echo out the URL, but will not make the link clickable - you need to add it to an <a> tag:
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
Click Here
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
If you want to get Post Permalink you should use get_permalink($post_id)
Wordpress get_permalink function Reference
Please try this:
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post();
$id = get_the_ID();
?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php get_the_permalink($id); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>

How to display wordpress first post different from the others?

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; ?>

How to make a second loop in single.php work?

-simple blog
-twenty twelve child theme
I need: a second loop in single.php that shows the selected post and all the other posts below.
What I have so far in single.php (results in a blank page) :
<?php get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
<?php endif; ?>
<?php wp_reset_postdata(); // reset the post data so we can run another query ?>
<?php get_sidebar(); ?>
<?php
// The Second Query
$the_query = new WP_Query();
// The Loop
if ( $the_query->have_posts() ):
while ( $the_query->have_posts() ):
$the_query->the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); // Restore original Post ?>
</div><!-- #content -->
</div><!-- #primary -->
This should do the trick:
<?php get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_postdata(); // reset the post data so we can run another query ?>
<?php
$args_second = array(
'posts_per_page' => -1,
);
// The Second Query
$second_query = new WP_Query( $args_second );
// The Loop
if ( $second_query->have_posts() ):
while ( $second_query->have_posts() ):
$second_query->the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); // Restore original Post ?>
</div><!-- #content -->
</div><!-- #primary -->
Notes:
You need to properly show the title and content using the_title() and the_content() inside the single loop.
To show other posts, you need to query them, you'll quickly understand by looking at the code above.
I'll leave the styling for you.
It is tested and working.

php if statements inside an if statement

I can't seem to get this to work. I have an if statement opening with <?php if (have_posts())...etc.. Then, below that I have a conditional statement determining whether or not the post is in a certain category <?php if (is_category())...etc.. This is the part I can't get right. What am I doing wrong?
<?php get_header();?>
<section id="content">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="article-wrapper">
<article id="post-<?php the_ID(); ?>">
<time datetime="<?php the_time('c'); ?>"><?php the_time('F j, Y'); ?></time>
<?php if (is_category('news')) { ?>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
<p class="read-more">Read more</p>
<?php } ?>
<?php if (is_category('podcasts')) {
$custom = get_post_custom($post->ID);
$buzzsprout_code = $custom["buzzsprout_code"][0];
echo do_shortcode($buzzsprout_code);
echo '<p class="read-emails">View emails and comment on this episode</p>';
} ?>
</article>
</div>
<?php endwhile;endif; ?>
<div id="pagination">
<?php my_paginate_links(); ?>
</div>
</section>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
From wordpress docs:
is_category() tests to see if you are displaying a category archive, but you are displaying a single post, so this will always return false
in_category() tests to see if a given post has a category in that specific category and must be used in The Loop, but you are trying to figure out what the category is, before you get to the loop.
The short try in_category() instead.

How to use shortcode in a template

Below you can see my home page template. I used to have this line in the content of my page, it loads the thumbnail and the video player with video. I now want to have this in a template but i dont know how to call it properly. You can see I am trying to call the video from my theme options panel.
Before
[KGVID poster="http://www.muratgokmen.co.uk/wp-content/uploads/2013/09/thumbnail-home.jpg" width="1005" height="565"]http://www.muratgokmen.co.uk/wp-content/uploads/2013/09/sample.mp4[/KGVID]
After
[KGVID poster="http://www.muratgokmen.co.uk/wp-content/uploads/2013/09/thumbnail-home.jpg" width="1005" height="565"]http://www.muratgokmen.co.uk/wp-content/uploads/2013/09/sample.mp4[/KGVID]
Any help would be much appreciated.
<?php /* Template Name: Home Page */ get_header(); ?>
<!-- section -->
<section role="main">
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<!-- article -->
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
[KGVID poster="http://www.muratgokmen.co.uk/wp-content/uploads/2013/09/thumbnail-home.jpg" width="1005" height="565"]<?php echo $smof_data['home-page-video-url'];?>[/KGVID]
</article>
<!-- /article -->
<?php endwhile; ?>
<?php else: ?>
<!-- article -->
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
</article>
<!-- /article -->
<?php endif; ?>
</section>
<!-- /section -->
<?php get_footer(); ?>
For using shortcode in templates do this
<?php echo do_shortcode("[KGVID poster='http://www.muratgokmen.co.uk/wp-content/uploads/2013/09/thumbnail-home.jpg' width='1005"' height='565']http://www.muratgokmen.co.uk/wp-content/uploads/2013/09/sample.mp4[/KGVID]"); ?>

Categories