Why is my Wordpress Post Loop not working? - php

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

Related

Add a div inside wordpress loop?

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

WordPress loop php function is executing function multiple times

on my WordPress front-page.php theme-page i use the WordPress Loop to show a number of Youtube Videos. I use acf to get the Youtube-URL and create the iframe from the url with the function convertYoutube. Now when i execute the loop each item is showing 5 videos (number of posts im querying) and each video, also the videos from the other posts. What am i doing wrong?
<?php get_header(); ?>
<main>
<div class="inner">
<div class="video-wrapper">
<?php
// video function
function convertYoutube($string) {
return preg_replace(
"/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i",
"<iframe src=\"//www.youtube.com/embed/$2?rel=0\" allowfullscreen></iframe>",
$string
);
}
// args
$args = array(
'post_type' => 'video',
'post_status' => 'publish',
'posts_per_page' => 5,
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="video">
<?php
$title = get_the_title();
echo $title;
$videoURL = get_field('video_url');
$videoEmbedCode = convertYoutube($videoURL);
echo $videoEmbedCode;
?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
</div>
</div>
</main>

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
}

How to retrieve the title of the latest post in Wordpress?

I am working on a website that is based on Wordpress. My client wants to display the latest post snippet under the header on the main page. I was able to do that but instead of fetching the post's title in the loop it is fetching the page title and displaying it there.
Here is my code :
<?php
$postslist = get_posts('numberposts=1&order=DESC&orderby=date');
foreach ($postslist as $post) :
setup_postdata($post);
?>
<div class="entry">
<h3><?php the_title(); ?></h3>
<?php the_time(get_option('date_format')) ?><?php if (has_post_thumbnail() ) {
the_post_thumbnail();
} the_excerpt(); ?>
</div>
<?php endforeach; ?>
I have also tried get_the_title(); but it did not work as well.
The SITE ITSELF
From wordpress codex
https://codex.wordpress.org/Function_Reference/wp_get_recent_posts
$recent_posts = wp_get_recent_posts(array('numberposts' => 1);
foreach( $recent_posts as $recent )
echo $recent["post_title"];
if you want to use the loop
$args = array('posts_per_page' => 1);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<div class="entry">
<h3><?php the_title(); ?></h3>
<?php the_time(get_option('date_format')) ?> <?php the_excerpt(); ?>
</div>
<?php
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Please use this:
<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
<div class="entry">
<h3><?php echo $recent['post_title'] ?></h3>
<?php the_time( get_option( 'date_format' ) ); ?>
<?php echo get_the_post_thumbnail( $recent['ID'] ); ?>
<?php echo get_excerpt($recent['ID']); ?>
</div>
}
?>
It worked. There is a plugin called smart post lists that was not letting it work. Now it is working fine.
It was my mistake to not disable it and test this. My code is correct.

Display div only if exists other posts with same custom field value

I've got a wordpress theme with a set of custom fields.
One of these is named "author".
On single.php I've got a div which show the other posts with the same custom field value.
I would like to display this div only if exists other posts with the same custom field value, else I would like to display nothing.
Thanks for your help!!
This is my actual code:
<?php
$myquery = array(
'meta_key' => 'autore',
'meta_value' => $autore,
'showposts' => 2,
'post__not_in' => array($post->ID)
);
if ( $myquery->have_posts() ) : ?>
<div class="related">
<h3>Altre di <?php the_field('autore'); ?></h3>
<ul>
<?php while ( $your_query->have_posts() ) : $your_query->the_post(); ?>
<?php
echo '<li>'; ?>
<?php
$fotorel = get_field('foto_homepage');
list($width, $height) = getimagesize("$fotorel");
$relheight = $height / 2;
?>
<div class="related-foto" style="background:url(<?php the_field('foto_homepage'); ?>) no-repeat center center; height:<?php echo $relheight.'px' ?>"></div>
<?php the_title(); ?>
<?php echo '</li>';?>
<?php endwhile; ?>
<?php else : // What to do if there are no posts from that author
endif;?>
</ul>
</div>
<?php wp_reset_query(); ?>
Here an example:
http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
Using the condition <?php if ($pageposts): ?> you can print your div or not.
I'm not sure how you are querying for the posts in the custom fields but the $wp_query has built in conditionals for handling queries that don't return posts.
Updated code sample:
$args = array(
'meta_key' => 'autore',
'meta_value' => $autore,
'showposts' => 2,
'post__not_in' => array($post->ID)
);
$your_query = new WP_Query( $args );
if ( $your_query->have_posts() ) : ?>
<div id="your-div">
while ( $your_query->have_posts() ) : $your_query->the_post();
// Do stuff
endwhile;
else : // What to do if there are no posts from that author
endif;

Categories