WP_Query Not working or not displaying post content - wordpress

I'm quite new to WordPress Development been following some online tuts and read bits and bobs of documentation but my WP_Query still displays nothing and when I add an else to my loop the else condition displays. I have created the page I'm trying to pull from the database what am I doing wrong here's the code:
<section id="home">
<?php
$query = new WP_Query( array( 'pagename' => 'home' ) );
if ($query->have_post() ) {
while ($query->have_posts() ){
$query->the_post();
echo '<div class="entry-content">';
the_content();
echo '</div>';
}
}
wp_reset_postdata();
?>
</section>

simply change
$query->have_post()
to
$query->have_posts()

use the below code for display content
$args=array(
'post_type' => 'post'
);
$the_query = null;
// the query
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?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; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Related

How do I use get_content WordPress function?

I am not able to fetch content while using the the_content WordPress function.
<!-- Start the Loop. -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( in_category( '3' ) ) : ?>
<div class="post-cat-three">
<?php else : ?>
<div class="post">
<?php endif; ?>
<!-- Display the Title as a link to the Post's permalink. -->
<h2><?php the_title(); ?></h2>
<!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
<small><?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?></small>
<!-- Display the Post's content in a div box. -->
<div class="entry">
<?php the_content(); ?>
</div>
<!-- Display a comma separated list of the Post's Categories. -->
<p class="postmetadata"><?php _e( 'Posted in' ); ?> <?php the_category( ', ' ); ?></p>
</div> <!-- closes the first div box -->
<!-- Stop The Loop (but note the "else:" - see next line). -->
<?php endwhile; else : ?>
<!-- The very first "if" tested to see if there were any Posts to -->
<!-- display. This "else" part tells what do if there weren't any. -->
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<!-- REALLY stop The Loop. -->
<?php endif; ?>
I have really tried but get empty description result kindly help
Here a example fetch description from post in Wordpress
<?php
$categories = get_the_category( $id );
if( $categories ){
// Assumes you just want the first category
print 'You’re in the ' . $categories[ 0 ]->name . ' category';
}
?>
<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$attachments = get_posts( array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null
) );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
?>
<li><?php echo wp_get_attachment_image( $attachment->ID, 'full' ); ?>
<p><?php echo apply_filters( 'the_title', $attachment->post_title ); ?></p>
</li>
<?php
}
}
endwhile; endif; ?>
</ul>
<p><?php echo apply_filters( 'the_title', $attachment->post_title ); ?></p> <!-- do you need change post_title for description -->

How to link a post to its category in WordPress

I want to add a link when my post gets click on. The link has to go to the category of the post. I've got this far but now I'm stuck. Can anyone show me how I can do this?
<?php
$args = array(
'category_name' => 'portriats',
'posts_per_page' => 1
);
$qry = new WP_Query($args);
if ( $qry->have_posts() ) :
while ( $qry->have_posts() ) : $qry->the_post();
$postcat = get_the_category( $post->ID );
?>
<div class="hometile">
<a href="<?php get_category_link( $postcat) ?>">
==> **I need to get the category of the post and then let PHP print the link of the categorey in the href**
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php
endwhile;
endif;
?>
It will display primary category of post with link
<?php $qry = new WP_Query($args ); ?>
<?php if ( $qry->have_posts() ) : ?>
<?php while ( $qry->have_posts() ) : $qry->the_post(); $postcat = get_the_category();?>
<div class="hometile">
<a href="<?php echo get_category_link( $postcat[0]->term_id ); ?>">
<?php echo $postcat[0]->name; ?>
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>

Display custom post type categories (terms) in loop

I am using the following code to load all the posts from a custom post type, in this loop I show a title but I also want to show the categories (terms) that are connected to this particular post but I can't seem to make it work
Loop:
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<?php the_title();?><br/>
! HERE I WANT THIS POSTS CATEGORY !
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I tried:
<?php echo $term->name; ?>
You need to use get_the_terms() for getting category
you can get this by below code...
you need to put second argument a custom post-type's category slug
you can refer this link https://developer.wordpress.org/reference/functions/get_the_terms/
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<?php the_title();?><br/>
! HERE I WANT THIS POSTS CATEGORY !
<?php
$terms = get_the_terms( get_the_ID(), 'category-slug' ); // second argument is category slug of custom post-type
if(!empty($terms)){
foreach($terms as $term){
echo $term->name.'<br>';
}
}
?>
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

Why does wp_query make my website loop endlessly?

I have a custom post type named " actualites ".
I want to wp_query the post data ( title and content for now ).
Here's the code I used :
<div class="row">
<div class="col-sm-8 blog-main">
<?php
// the query
$query = new WP_Query( array( 'post_type' => 'actualites' ) ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$the_query->the_post();
echo '<div class="blog-post">';
echo '<h2 class="blog-post-title">' . get_the_title() . '</h2>';
echo '<p class="the-content">' . the_content() . '</p>';
echo '</div>';
endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php esc_html_e( 'Désolé, aucun post ne correspond à votre requête.' ); ?></p>
<?php endif; ?>
</div>
<!-- /.blog-main -->
<div class="col-sm-3 col-sm-offset-1 blog-sidebar">
<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
<ul id="sidebar">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</ul>
<?php endif; ?>
</div><!-- /.blog-sidebar -->
For some reason this makes my website loop endlessly. Nothing appears, not even the header or the footer. Why does this happen ? It's 100% the wp_query part.
I tried the standard query and had an "expected statement " that probably was the cause of my issue. I'm trying the alternate wp query code now, and I don't have any clue as to what doesn't work. Any help ? Thanks !
Your Code should be as follows. In your code $the_query variable is not defined. I think that's why it's not working. Also turn on WP DEBUG MODE when you are developing. So you will be able to see errors and warnings.
<?php
// the query
$query = new WP_Query( array( 'post_type' => 'actualites' ) ); ?>
<?php if ( $query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $query->have_posts() ) : $query->the_post();
echo '<div class="blog-post">';
echo '<h2 class="blog-post-title">' . get_the_title() . '</h2>';
echo '<p class="the-content">' . the_content() . '</p>';
echo '</div>';
endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php esc_html_e( 'Désolé, aucun post ne correspond à votre requête.' ); ?></p>
<?php endif; ?>

WordPress Latest News Page Showing Custom Fields From All News Stories

I have my own custom WordPress theme and I am building out the 'Latest News' section at the moment. I have a custom field for my page header image and alt tag for each page on my site, I am using the following code to display those custom fields on each page:
<section class="page-heading">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<img src="<?php the_field( 'header_image' ); ?>" alt="<?php the_field( 'main_header_image_alt_tag' ); ?>">
<?php endwhile; endif; ?>
</section>
Which works fine. But on my 'Latest News' page all the custom fields from all the news stories (posts) are being displayed, so for example if I have 3 post displayed per page then I am getting 3 images and 3 alt tag displayed.
In my settings I have my 'Posts Page' set to 'Latest News'.
Is there a way I can only have 1 image and 1 alt tag displayed and not all 3?
Thanks.
EDIT
I've added an image to better explain... The images that span the screen are from the news stories and not the custom field for the 'Latest News' page.
I don't know if I understand your question, but you want your first post show the image and the rest not?
I guess you have to write something like this:
<section class="page-heading">
<php $show_first_image = true; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if($show_first_image): $show_first_image = false; ?>
<img src="<?php the_field( 'header_image' ); ?>" alt="<?php the_field( 'main_header_image_alt_tag' ); ?>">
<?php endif; ?>
<?php endwhile; endif; ?>
I think I understand your problem, your latest news is also a Page (post type), which has the header image.
Your index file/archive is like this:
<?php
get_header();
if(have_posts()):
while(have_posts()):
// your latest news * 3.
endwhile;endif;
get_footer(); ?>
the problem is, got its items loaded before the get_header(), where your header image will be showed.
What you do is something like this:
<?php
global $wp_query;
$original_query = $wp_query;
$wp_query = null;
$page = get_query_var( 'pagename' );
$wp_query = new WP_Query( array('post_type' => 'page', 'pagename' => $page ) );
get_header(); // in the get_header() you will probably have the <section class="page-heading">
$wp_query = null;
$wp_query = $original_query;
if(have_posts()):
while(have_posts()):
// your latest news * 3.
endwhile;endif;
get_footer(); ?>
I hope this is your answer to your question.
I got around this by creating a new template for my news page and using WP_Query inside that template to bring in the latest news stories. I also unset 'Posts Page' in my reading settings as that's what was causing all the issues.
I'm not sure if that is the most efficient way of doing things but it worked for me.
VVC, I appreciate your help and effort.
<!-- WP Query Arguments -->
<?php
$args = array(
'page_id' => '103'
);
$the_query = new WP_Query( $args );
?>
<!-- End WP Query Arguments -->
<!-- WP Query loop -->
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else: ?>
<h2>No posts to display</h2>
<?php endif; ?>
<!-- End WP Query loop -->
<!-- WP Query Arguments -->
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => '5'
);
$the_query = new WP_Query( $args );
?>
<!-- End WP Query Arguments -->
<!-- WP Query loop -->
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="news-story">
<div class="news-heading">
<h5><?php the_title(); ?></h5>
<p class="publish-date"><strong>Posted on:</strong> <?php the_time('j M, Y'); ?></p>
</div>
<?php the_excerpt(); ?>
<p>Full Story <i class="fa fa-angle-double-right"></i></p>
</div>
<?php endwhile; ?>
<?php else: ?>
<h2>No posts to display</h2>
<?php endif; ?>
<!-- End WP Query loop -->

Resources