My WordPress loop for posts fails - wordpress

i use that code to post an ad in my homepage every 3 posts but instead of that the ad appears behind the 1st post and it seems like it doesnt recognise the loop at all
<div id="mason-layout" class="transitions-enabled fluid">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'blog' ); ?>
<?php if ($i == 3) { ?> ADHERE <?php } ?>
<?php $i++; ?>
<?php endwhile; ?>
any ideas?
thanks

You're missing part of your loop.
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
Edit: If you want to break the if statement onto multiple lines you'll probably want to use a different syntax. Eg:
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
Recommended reading: http://codex.wordpress.org/The_Loop

Related

Wordpress custom loop in template part

I'm using a theme for some Wordpress-integrated software which means that I am basically stuck with the template hierarchy, otherwise I'd just set up a custom template to get around this.
Anyway, I have my page.php, which looks a little like the following
<?php //start the main loop
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
?>
<?php
if ( is_page('contact-us') ) {
get_template_part( 'content', 'contact' );
}else{
get_template_part( 'content', 'page' );
}
?>
<?php //end the main loop
endwhile;
else:
?>
Something is missing
<?php
endif;
?>
This works fine, and as expected, I am able to add html within content-page.php
However, I would like to add a custom loop within content-page.php to display customer testimonials. I've attempted this with the code below inside content-page.php:
<?php //close the main loop
endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php
//close the main loop and open a custom loop
wp_reset_postdata();
$args = array (
'post_type' => 'testimonial',
'posts_per_page' => 5,
'orderby' => 'rand'
);
$the_query = new WP_Query ( $args );
if ( have_posts() ) : while ( $the_query ->have_posts() ) : $the_query ->the_post(); ?>
Do HTML stuff here
<?php //close the custom loop
endwhile; else: ?>
Uh oh, there is meant to be a testimonial here. Please create some.
<?php endif; ?>
<?php //re-open the main loop
wp_reset_postdata();
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
This code creates a PHP error (unexpected endwhile where I attempt to close the main loop). However, I put this exact same code straight inside page.php, it works. It only errors when inside content-page.php. Am I not able to include custom loops with get_template_part?
the last endwhile and endif are not closed
<?php //re-open the main loop
wp_reset_postdata();
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
endwhile;
endif;
?>

why the_author() doesn't working

I am learning WordPress theme development and I have developed a theme. All work has been done, but the author name not displaying in a blog page. I used the following:
<?php the_author(); ?>
but it is not working. Is it necessary to do any function or code in functions.php?
Basically, this is the loop of a page in wordpress:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
some wordrpess code
<?php endif; ?>
You have to enter the get request INTO the loop, something like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php $author = get_the_author(); ?>
or
<?php the_author(); ?>
<?php endif; ?>
If not within the loop you can try this
<?php the_author_meta( 'display_name', $userID ); ?>
From Codex

first class for every first post on page wordpress

how could i get for every post on a page a post_class "first"?
with this code a managed a class "first_post" for the really first post of the loop, but i need it also on the second, third and so on page of the loop.
function.php
function firstpost_class($class) {
global $post, $posts;
if ( is_home() && !is_paged() && ($post == $posts[0]) ) $class[] = 'firstpost';
return $class;
}
add_filter('post_class', 'firstpost_class');
Thanks for your help. Google couldn't help yet.
I've seen this kind of thing done on the loop itself -- comment to clarify if you want to do this site-wide, or particularly want to hook into post_class (then again, you can edit loop.php or a content-LOOPTYPE.php and reuse it over and over). Here's an example loop:
<?php
$counter = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post();
$counter++;
?>
<div class="post <?php echo 'item-' . $counter; ?>">
<?php the_title(); ?>
<?php the_content(); ?>
</div>
<?php endwhile; endif; ?>
This would give you item-1, item-2, etc. etc.

WordPress Multiple Loops

I am getting horribly frustrated trying to build multiple loops in WordPress. I've looked at loads of articles - what am I doing wrong. I placed the following in the loop.php file (because I've built the homepage on this)...
<!--Loop 1-->
<?php global $query_string; // required
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php global $query_string2; // required
$posts = query_posts($query_string2.'category_name=jobs&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
// your output html
<?php endwhile; ?>
You have nested the loops (missing endwhile).
You cannot just invent a variable "query_string2" and expect it to work ;)
Try this:
<!--Loop 1-->
<?php
global $query_string; // required
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php
$posts = query_posts($query_string.'category_name=jobs&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
If that doesn't work (I don't remember whether wp_reset_query() resets the $query_string global off the top of my head), try the following:
<?php
global $query_string; // required
$query_string_backup = $query_string;
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php
$posts = query_posts($query_string_backup.'category_name=jobs&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
This will work:
<?php
global $query_string; //make sure these are in the correct format for post queries
global $query_string1;
// First loop
$first_loop = new WP_Query( $query_string.'category_name=news&posts_per_page=3');
while ( $first_loop->have_posts() ) : $first_loop->the_post() :
// do your thing
// e.g: $first_loop->the_content();
endwhile;
// second loop
$second_loop = new WP_Query( $query_string1.'category_name=news&posts_per_page=3');
while ( $second_loop->have_posts() ) : $second_loop->the_post() :
//do your thing
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
I could help out, but I think that first, I should understand 1 fundamental: WHAT are you trying to do exactly? What is your goal? Why do you think you need nested loops?
This will help me aswering more correctly.
I solved it with the following code, which I put in a page template (my friend advised that it's best leaving the loop.php alone)
<?php $custom_query = new WP_Query('category_name=news'); // only News category
while($custom_query->have_posts()) : $custom_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
<?php $custom_query = new WP_Query('category_name=jobs'); // only Jobs category
while($custom_query->have_posts()) : $custom_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php endwhile; ?>
There's a function called rewind_posts() that rewinds the loop posts in order to let you re-use the same query in different locations on the same page
https://codex.wordpress.org/Function_Reference/rewind_posts

WordPress search only works with posts, not pages

I'm working on my own custom WordPress theme, using a blank, default template to start. I haven't edited the search.php file.
Many of my WordPress posts are Pages. Unfortunately, the site search only retrieves posts, not pages.
Any idea how to get the theme to search both posts and pages?
Here is the majority of search.php:
<?php if (have_posts()) : ?>
<h3>Search Results</h3>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h4><?php the_title(); ?></h4>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h3>No posts found.</h3>
<?php endif; ?>
Add this code to your functions.php file.
function wpshock_search_filter( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array('post','page') );
}
return $query;
}
add_filter('pre_get_posts','wpshock_search_filter');
http://wpth.net/limit-wordpress-search-results-to-specific-post-types
WP Search http://wpsear.ch/ has that ability.You can adjust what post types you want to show in results page.
In your search.php, find The Loop and insert this code just after it. You can recognize the Loop because it usually starts with:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
Code to be inserted:
if (is_search() && ($post->post_type=='page')) continue;
So, your code should be like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php if (is_search() && ($post->post_type=='page')) continue; ?>
Let me know if it worked.
Lack of page search and search results ranked by post date instead of relevance is a typical WP problem. Try http://wordpress.org/extend/plugins/relevanssi/

Resources