You need to echo this out using php, or close your php tags:
<h2>This is where my latest post will appear</h2>
Like this:
if ( $first_post ) {
echo '<h2>This is where my latest post will appear</h2>';
} else {
echo '<h2>This is where all subsequent posts should appear</h2>';
}
Tried both options the problem persists.
I realise it’s something I’ve done but this is madenning, is this code correct?
<?php
get_header('topbar'); ?>
<div class="blog_container">
<h2 class="blog-title">
Latest News
</h2>
<p>
News information and much more.
</p>
</div>
<?php
$posts_query = new WP_Query( $query_args );
if ( $posts_query->have_posts()) {
$first_post = true;
while ( $posts_query->have_posts() ) {
$posts_query->the_post();
if ( $first_post ) {
echo '<h2>This is where my latest post will appear</h2>';
} else {
echo '<h2>This is where all subsequent posts should appear</h2>';
}
$first_post = false;
}
}
wp_reset_postdata();
?>
<?php get_footer();
?>
Then you’re doing something else wrong.
The syntax is fine after you add the echo in your print statements:
https://postimg.cc/xq14YsZ7
https://phpcodechecker.com
I also tested it on my own development server.
Yes, what you’ve posted back is correct.
Copy and paste the error.
There is no error now only the echoed out content does not appear at all. All I see is the header and footer.
Well then one of your conditions is not being met:
if ( $posts_query->have_posts()) {
or
while ( $posts_query->have_posts() ) {
if the post_query is empty:
$posts_query = new WP_Query( $query_args );
Just check the values right away coming from the post_query :
$posts_query = new WP_Query( $query_args );
Maybe you’re not inside the loop.
Just run this:
https://developer.ww.wp.xz.cn/reference/functions/query_posts/#usage
The queries were both met, I added relevant HTML to both the if and else statement.
I’ll come back to it tomorrow. Completely mind numbingly irritating, all I want to do is display the very latest post at the top of the blog page with the remaining posts in a row below. I’ve found writing HTML and CSS quite easy but this PHP is another level of nonsense 🙂
Thanks ever so much for your help though.
-
This reply was modified 4 years, 8 months ago by
digstertron.
Here’s your main PHP code rewritten to display debugging info. First, note the comment on the first line…if $query_args isn’t defined, the line will generate a notice in PHP7 and a warning in PHP8. If the “no posts” line is displayed, then the query failed. If “this is post number” lines are displayed with a post ID of zero, then something is wrong in the query.
<?php
$posts_query = new WP_Query($query_args); // where is $query_args defined?
$post_count = 0;
if ($posts_query->have_posts()) {
while ($posts_query->have_posts()) {
$post_count++;
$posts_query->the_post();
$post_id = (int) get_the_ID();
echo "<h2>This is post number $post_count with ID $post_id</h2>";
}
}
if (!$post_count) {
echo '<h2>There were no posts!</h2>';
}
wp_reset_postdata();
get_footer();