Closing a while conditonally in diffrent places - php

I am trying to create a single php file I can use to display the content for the index or a page in wordpress. This question is not wordpress specific though.
I have a 'while' which needs to be closed in two different places depending on whether the index or a page is being displayed. I am trying to close the 'while' with an 'if'
if (is_home()) { endwhile; }
// otherphp
if (is_page()) { endwhile; }
This is not working though. The full code is below:
<section id="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if (is_home()) : ?> <article <?php post_class() ?> id="post-<?php the_ID(); ?>"> <?php elseif (is_page()) : ?> <article class="post" id="post-<?php the_ID(); ?>"> <?php else : NULL; endif; ?>
<div class="entry">
<?php the_content(); ?>
</div>
</article>
<?php if (is_home()) { endwhile; } ?> <!-- ENDWHILE MUST BE HERE ON INDEX PAGE -->
<?php if (is_home()) : get_template_part('nav','default'); elseif (is_page()) : NULL; else : NULL; endif; ?>
<?php if (is_page()) { endwhile; } ?> <!-- ENDWHILE MUST BE HERE ON ALL OTHER PAGES -->
<?php endif; ?>
</section>
I realise this code is somewhat inefficient and messy but at the moment I am not concentrating on efficiency. I am simply trying to make it work.
How can I conditionally end the 'while'?

Just do a normal condition inside a normal while:
while (/* something */) :
// same for all
if (/* is home */) {
// do something only for home
}
endwhile;

Related

HTML only print once in Wordpress Loop

I have a code chunk that is inside the_content(); I'm using acf repeater as well. So when I post a blog, I'll either use the_content(); or the acf field. I have h2 tag ( latest articles ) that I only want printed one time, but it's printing everytime I make a post.
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="container">
<div class="row">
<div class="col-md-4 sidebar-r">
<?php echo the_content(); ?>
</div><!-- end sidebar-r -->
<?php
$i = $wp_query->post_count;
if($i <=1) {
echo '<h2 class="link-title">
<?php the_sub_field('link_title'); ?>,
</h2>';
}else{
echo '';
}
?>
<div class="col-md-8 links-wrap">
<?php if(have_rows('daily_links')): ?>
<?php while(have_rows('daily_links')): the_row(); ?>
<a href="<?php the_sub_field('link_url'); ?>" target="_blank">
<h2 class="link-title">
<?php the_sub_field('link_title'); ?>,
</h2>
<h3 class="link-source">
<?php the_sub_field('link_source'); ?>
</h3>
</a>
<?php endwhile; ?>
<?php endif; ?>
</div><!-- end links wrap -->
</div><!-- end row -->
</div><!-- end container -->
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
You'll see I tried using php to count the posts and if more than one post, don't print the tag, but couldn't figure out the exact logic and syntax.
I am honestly struggling a bit to understand exactly what you are trying to do and since I do not even have the posts and other key pieces of information so that I can properly replicate your issue so that I can help you better, this is a little bit challenging. That being said, looking into some ideas I came across another stackoverflow question/answer that might be relevant for you in catching the first post and does something to it. The answer to the referenced question instance was this:
<?php if (have_posts()) : $postCount = 1; while (have_posts()) : $postCount++; ?>
<?php if($postCount == 2) { ?>
// SOMETHING TO DO WITH FIRST POST
<?php } else { ?>
// SOMETHING TO DO WITH ALL OTHER POSTS
<?php } ?>
This was suggested by user Bora in this answer from 2013.
Let me know if that helped!

Why is this basic if statement looping?

I have an if/else to call the template for a post type, and for some reason I cannot seem to figure out why the first portion is stuck in an infinite loop.
Here's the main page loop:
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<?php if ( has_term('bands', 'work_family')):
get_template_part('content', 'single-bands');
else:
get_template_part('content', 'single-work');
endif; ?>
<?php endwhile; endif; ?>
I'm pretty sure the problem is in the single-bands template that is getting loaded, because if I comment out that line and stick some text in there it works just as it should. Here's that template:
<header class="fl-post-header">
<h1 class="fl-post-title" itemprop="headline">
<?php
// strip "Songs by" from titles for archive page
$title = get_the_title();
$prefix = 'Songs by ';
$stripper = $title;
if (substr($stripper, 0, strlen($prefix)) == $prefix) {
$stripper = substr($stripper, strlen($prefix));
}
?>
<?php echo $stripper; ?>
<?php edit_post_link( _x( 'Edit', 'Edit post link text.', 'fl-automator' ) ); ?>
TEMPLATE LOADED CORRECTLY IF YOU SEE THIS
</h1>
</header><!-- .fl-post-header -->
<div class="fl-content <?php FLTheme::content_class(); ?>">
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<article <?php post_class( 'work-post' ); ?> id="fl-post-<?php the_ID(); ?>" itemscope="itemscope">
<?php the_content(); ?>
<?php get_template_part('content', 'band-performances') ?>
<?php endwhile; endif; ?>
</div>
Nothing else loop related on the page and all the html works fine when I load the template directly instead of in that outer loop from the other page. I'm relatively certain the problem is in the header, because the rest of the template does not get looped, just that.

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.

Wordpress looping issue

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

Display wordpress search results

I'm trying to make a custom search page, by creating all new search.php file, for my wordpress template...so far, so good.
The problem is that when I search for something it doesn't show any results.
I'm guesing that it has something to do with some php script or something I don't know.
How can I fix that ?
P.S The function for the number of the results works fine, but there isn't any results.
Here is the content of search.php
<?php
get_header();
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h1>Search Results</h1>
<?php endwhile; ?>
<?php else : ?>
<?php _e( 'Nothing Found' ); ?>
<?php endif; ?>
<?php
get_footer();
?>
The problem is that you don't have anything in your loop to print the results, i.e.
<?php while (have_posts()) : the_post(); ?>
<h1>Search Results</h1>
<!-- Needs something here -->
<?php endwhile; ?>
To fix the problem, simple replace <!-- Needs something here --> with the following
<a href="<?php the_permalink() ?>">
<h2><?php the_title(); ?></h2>
</a>
<p><?php the_excerpt(); ?></p>
You also need to move <h1>Search Results</h1> to above the loop to stop it from displaying multiple times. It may be best to move it above the if statement if you don't intend on adding it to your else statement as well.

Categories