This could be a dumb question, but I just can't figure it out. I'm working on a custom WordPress theme I created by converting a html website. But when I use WP_Query, it doesn't load my blog posts. Yes, it displays a list of blog posts as it should with the loop, but when I click on each title or the 'read more' link of a post, it only changes the url in the address bar to the link of the post and then simply refreshes the hope page. It doesn't open I'm really tired. Here's the query loop in my index.php
<?php
$categories = get_categories();
foreach ( $categories as $category ) {
//define args for query
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '30',
);
//The main query
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
<?php the_excerpt(); ?>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
}
?>
</div><!-- blog-main-content-->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Please what do I do
Based on your comment above, you are missing a template file for your posts page. In your current configuration, the posts loop will be displayed on all pages of your website since you only have an index.php file. You should create a single.php template to display your single blog posts.
Here is the WordPress template hierarchy for reference: https://developer.wordpress.org/themes/basics/template-hierarchy/
Related
I have the code to display the latest posts on my website, but I wonder if there is a way to make a list of the latest posts, displaying only one post per category. Let's say I have 7 categories, so only 7 posts will be displayed on the page. What should I do?
<?php if ( ! is_single() ) { ?>
<div class="post-container">
<?php } ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
//Post Title code
//Post Thumbnail code
//Post Content / Excerpt code
//Post Meta code
</article> <!-- /post -->
<?php if ( ! is_single() ) { ?>
</div>
<?php
<?php } ?>
It's very easy to add latest post from each category.
First of all get all the categories of blog by using below code:
$categories = get_categories();
Then use foreach ( $categories as $category ) {} to tell WordPress to run through each of these categories in turn and run the code inside the braces.
Now you need to define the arguments for your query. Inside the braces, add this:
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
);
Next, insert your query, using the WP_Query class:
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
<?php the_excerpt( __( 'Continue Reading <span class="meta-nav">→</span>', 'twentyfourteen' ) ); ?>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
This will display each category posts in your home page. Please try to use it and let me know if you have any issue.
I'm creating a home page that will pull in different posts to display them in a custom format. I can get most of the information I want but I haven't been able to to get the featured image for any of them. I set a featured image for all three posts and I'm using my own theme that's a child of the twentysixteen theme. I get has_post_thumbnail returning false on all of them even though I've uploaded a featured image for each post. I only have this on local right now but here's the code I'm using to get the posts:
<?php
global $post;
$myposts = get_posts('numberposts=3&category=1');
foreach($myposts as $post) :
setup_postdata( $post );?>
<div class="article-box">
<div class="article-box-image">
<?php if (has_post_thumbnail($post->ID)) { ?>
has one
<?php } else { ?>
not working
<?php } ?>
<img src="<?php the_post_thumbnail_url(); ?>">
</div>
<div class="article-box-title">
<?php the_title(); ?>
</div>
<div class="article-box-excerpt">
<?php the_excerpt(); ?>
</div>
<div class="article-box-edit">
<?php edit_post_link('Edit'); ?>
</div>
</div>
<?php endforeach; ?>
Use a wp query:
<?php
$args = array(
'cat' => '1',
'posts_per_page' => '3',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="article-box">
<div class="article-box-image">
<?php //check for thumbnail
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
else {
//Give up and start new life picking apples
} ?>
</div>
<div class="article-box-title">
<?php the_title(); ?>
</div>
<div class="article-box-excerpt">
<?php the_excerpt(); ?>
</div>
<div class="article-box-edit">
<?php edit_post_link('Edit'); ?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
And then double check that thumbnails are supported in your theme's functions.php
add_theme_support( 'post-thumbnails' );
I finally realized that the "preview" post parameters were the problem. I was viewing the page through preview page. I don't know why images won't work in preview mode but apparently it won't.
List post categories that link to a page showing those posts.
Hi all
I have posts with different categories.
I have a blog page that lists the posts like this.
<?php
$blog_args = array(
'post_type' => 'post',
'order_by' => 'date',
'order' => 'ASC'
);
$blog_loop = new WP_Query($blog_args);
if($blog_loop->have_posts()):
while($blog_loop->have_posts()):
$blog_loop->the_post();
?>
<div class="row">
<div class="col-sm-3 img-responsive">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('post-thumbnail', array( 'class' => "img-responsive"));
}
?>
</div>
<div class="col-sm-9">
<h3><?php echo the_title(); ?></h3>
<p><?php echo the_content(); ?></p>
</div>
</div>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
I would like to have a list of the different categories on the blog page. If I click the category I would like to show the post with that catogory.
I can list the categories like so.
<ul>
<?php
wp_list_categories('title_li=>');
?>
</ul>
If I click the link it takes me to an empty page with the name of the category in the URL.
How can I list the categories and link to show the post with that category.
add a file in your theme called category.php then add the loop code into it.
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title();?>
<?php the_content();?>
<?php endwhile; wp_reset_postdata(); endif; ?>
<?php get_footer(); ?>
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 -->
I have a blog, and I have added one custom post type 'Movies'. and I have link it to my single-movies.php. and added this code:
<div id="container">
<?php
$args = array( 'post_type' => 'movies', 'posts_per_page' => 1, 'name' => $_GET['movie'] );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1>
<a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a>
</h1>
<div class="entry-content">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
</div>
<?php include(TEMPLATEPATH . '/sidebar_single.php'); ?>
<?php get_footer(); ?>
But Problem is, I'm clicking on any movie, its showing me only same movie, which i have added last on custom field Movies (i.e Avatar Movie, if I click on stargate movie, Its showing me 'Avatar' Movie link. and same others)
Please help me and change this code, if I did anything wrong here.
when you are in single-movies.php
it means you are already on the right path like /movies/avatar
so doing a simple loop
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); //.... ?>
<?php endwhile; ?>
gives you all details of the avatar movie, you don't need to query, because the query is already made by the time you arrive to a SINGLE Movie page!
/movies/avatar means:
post type = movies
post name = avatar
also if you have pretty links, $_GET['movie'] it normally contains nothing.
it is prefered to use the $wp->query_vars['post_name'] to get the movie name
try just putting the loop and see what you get, if not dump the $_GET and $wp to see where you have the movie name.
you have a problem with the loop.
I would say to use a foreach...look at this example, maybe it will help you. it is exactly the same as you want to achieve.
<?php
$cat_id = $category->cat_ID; // YOU CAN CHANGE THIS OR REMOVE
global $post;
$args = array( 'category' => $cat_id, 'numberposts' => -1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : start_wp();
?>
<li class="subcat-post-title">
<span class="trigger">
<a class="ficha" href="<?php the_permalink() ?>">
<?php echo the_title(); ?>
</a>
<?php endforeach; ?>