Add a div inside wordpress loop? - php

I am a relatively new developer, and just started with wordpress. I want for each post to be wrapped in a class of post. I keep getting this error no matter what i do. If i echo the div or add post_class.
<div class="posts">
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 4
);
// Variable to call WP_Query.
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
// Start the Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div class='post'>'
the_post_thumbnail();
the_title('<h2>','</h2>');
the_excerpt();
echo '</div>'
endwhile;
else:
// If no posts match this query, output this text.
_e( 'Sorry, no posts matched your criteria.', 'textdomain' );
endif;
wp_reset_postdata();
?>
</div>

You need to do this:
echo '<div class="post">';
...
echo '</div>';

Related

Wordpress PHP loop custom post type and display on homepage

I'm currently working on a WordPress site and I've created a custom post type called 'events' using the CPT UI plugin.
I want to display the events on my home page, so I've tried to create a loop in my homepage template in the theme files. I've been using this as a guide https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-post-types-in-wordpress/
but for the life of me, I can't get the PHP that is used in that link to work for me.
<?PHP
$args = array( 'post_type' => 'events', 'posts_per_page' => 4 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
When I try to save that I get this error
syntax error, unexpected 'else' (T_ELSE)
I've been searching for an answer for this for a while and I can't find anything.
I'm pretty new to PHP, so sorry if I'm being incredibly stupid. Any help would be appreciated :)
You have not end while loop , place this code also <?php endwhile; ?>
<?php
$args = array( 'post_type' => 'events', 'posts_per_page' => 4 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Why is my Wordpress Post Loop not working?

Okay, I am trying run the wordpress post loop to print out the post titles and content using the_title() and the_content(). But It is not printing out the posts on the page?
Any ideas?
index.php
<div class="blog-container">
<!-- The POST LOOP -->
<?php
if(have_posts()) :
while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else :
echo '<p>No content found</p>';
endif;
?>
</div>
EDIT Okay, I'm getting ONLY THE content printed out not the post titles, here is an image: http://imgur.com/a/DiBqG.
EDIT 2 Inside wordpress admin post should look like: http://imgur.com/a/1DQAp
You don't define the query and use the Wordpress build in functions in the wrong way. Here is the correction.
<?php
// The Query
$the_query = new WP_Query( );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
add below code for fetch post data
<?php
$post_args=array(
'type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'ASC',
);
$post_my_query = null;
$post_my_query = new WP_Query($post_args);
if( $post_my_query->have_posts() )
{
while ($post_my_query->have_posts()) : $post_my_query->the_post();
?>
<h2><?php echo get_the_title( $post_my_query->ID );?></h2>
<?php
echo get_the_content( $post_my_query->ID );
endwhile;
}
wp_reset_query($post_my_query);
?>

adding a div block to a wordpress loop when no posts are found in a category

I have a wordpress loop I'm running that shows content for posts in a certain category. I need to show some content if there is no posts but don't know enough php to know how to set up the if/else statement.
Here's the loop:
<?php $args = array(
'posts_per_page' => -1,
'paged' => get_query_var('paged'),
'cat' => ('5'),
);
// The Query
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post(); ?>
<div class="company-logo-openings-closings"><?php the_post_thumbnail(); ?></div>
<div class="opening-closings-text"><?php the_excerpt(); ?>
LEARN MORE</div>
<div style="width: 100%; clear: both; height: 50px;"></div>
<?php endwhile;
// Reset Query
wp_reset_query();
?>
I essentially want the above to insert <div>CONTENT GOES HERE</div> if there are no posts in the stated category.
Thanks
Wp_Query Format
<?php
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}

Flex Slider Integration with WordPress

[SOLVED]
I'm trying to integrate Flex Slider (flexslider.woothemes.com) with WordPress. I've included all JS/CSS files needed.
Then, I added this code to show the featured images of a certain category - the famous WP_Query with $args set to a certain category. Then using echo the_post_thumbnail(); I'm showing the posts' images. It's working fine. I just need to make the images linked to post URLs (imgs are hrefed). Please provide help and thanks in advance.
<?php
$args = array(
'post_type' => 'post',
'category_name' => 'one',
'posts_per_page' => 5
);
// The Query
$the_query = new WP_Query( $args );
// Check if the Query returns any posts
if ( $the_query->have_posts() ) {
// Start the Slider ?>
<div class="flexslider">
<ul class="slides">
<?php
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<?php }
// The Slide's Image
echo the_post_thumbnail();
</li>
<?php endwhile; ?>
</ul><!-- .slides -->
</div><!-- .flexslider -->
<?php
// Reset Post Data
wp_reset_postdata();
?>
New code that's working:
<?php
$args = array(
'post_type' => 'post',
'category_name' => 'one',
'posts_per_page' => 5
);
// The Query
$the_query = new WP_Query( $args );
// Check if the Query returns any posts
if ( $the_query->have_posts() ) {
// Start the Slider
?>
<div class="flexslider">
<ul class="slides">
<?php
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php echo the_post_thumbnail(); ?>
</a>
</li>
<?php endwhile; ?>
</ul><!-- .slides -->
</div><!-- .flexslider -->
<?php }
// Reset Post Data
wp_reset_postdata();
?>
If I understand what you're asking, I think the answer is
echo '<a href="'. the_permalink() .'">';
echo the_post_thumbnail();
echo '</a>';

Syntax error in php echo concatenation

I have the following code in a Wordpress page template:
<?php $args = array( 'post_type' => 'casestudies', 'posts_per_page' => 12 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="casestudy">'.''.get_the_post_thumbnail( $post->ID, '180,180' ).'</div>'.'';
echo '<span class="details">'.'<div class="anchor-hover details-h3">'.the_title().'</div>';
echo '<p class="desc">'.get_post($post_id)->post_excerpt.'</p>'.'</span>';
endwhile; ?>
<div class="clear"></div>
I need to wrap the entire display starting with "echo" with this div:
<div<?php post_class('margin') ?> id="post-<?php the_ID(); ?>">
The title and excerpt are only supposed to show when the cursor is hovering over the thumbnail, but I can't get this line of code in without an unexpected syntax error.
I am able to get this to work on the archive for this post type so there should be some way to do it.
| HERE | OR RATHER HERE
echo '<div class="casestudy">'.''.get_the_post_thumbnail( $post->ID, '180,180' ).'</div>'.'';

Categories