Display all posts in category wordpress - php

I am trying to display all posts in the category on the category archive page.
I have used the following code, but it displays all posts from all categories.
Can someone help me please?
<?php
// the query
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1)); ?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<ul>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
<!-- end of the loop -->
</ul>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Thanks

You need to add a category parameter to the WP_Query array.
Check this reference in the "Category Parameters" section: https://codex.wordpress.org/Class_Reference/WP_Query
Something like this should work:
$wpb_all_query = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'category_name' => 'your_category_slug'
));

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

Show recent blog posts, but exclude current post

I want to display the 3 most recent posts at the bottom of my single-blog page, but without the current posts.
my code:
<div class="blog__posts">
<h2><?php esc_html_e('andere blogberichten', 'dfib-theme'); ?></h2>
<?php esc_html_e('alle blogberichten', 'dfib-theme'); ?>
<div class="blog__posts--wrapper">
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="blog__single">
<div class="blog__single--img" style="background-image: url(<?php echo get_the_post_thumbnail_url();?>);"></div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
lees meer
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
</div>
</div>
It displays 3 posts, but when I visit the most recent post, the same post is displayed at the bottom again. Is there a way to exclude the current one every time?
I just found the solution on this website: https://pineco.de/snippets/exclude-current-post-from-wp_query/
I just added this piece to my query:
'post__not_in' => array(get_the_ID())
You have to exclude the current post with the post__not_in.
Add post__not_in in WP_Query array like below.
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
'post__not_in' => array( get_the_ID() )
));
?>

Wordpress template part show only selected posts from post type

I have this template part:
<?php
while (have_posts()) : the_post();
get_template_part('template-parts/content', 'page');
do_action('flash_before_comment_template');
// If comments are open or we have at least one comment, load up the comment template.
if (comments_open() || get_comments_number()) :
comments_template();
endif;
do_action('flash_after_comment_template');
endwhile; // End of the loop.
?>
And I have a Post Type named food. I want to show only thoose posts (post with title and whole body) which contains this Post type. How should I modify the template to make it work?
Custom post type get the query
<?php
$custom_args = array(
'post_type' => 'food',
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page'=> -1
);
$get_news_query = new WP_Query( $custom_args ); ?>
<?php if ( $get_news_query->have_posts() ) : ?>
<?php while ( $get_news_query->have_posts() ) : $get_news_query->the_post(); ?>
<h2> <?php the_title(); ?> </h2>
<p><?php the_content(); ?></p>
<?php endwhile; ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
wp_reset_query();
?>

How to get 2 post_type work with each other

How do i get the permalink from cubeportofolio, and the title/thumbnail from posts? With the post_type syntax from wordpress.
here is my code:
<?php
$posts = get_posts(array(
'posts_per_page' => 1,
'post_type' => 'cubeportfolio' ));
if( $posts ): ?>
<?php foreach( $posts as $post ): setup_postdata( $post ) ?>
<li>
<a href="<?php the_permalink(); ?>">
until here the code is oke.
So i am getting the good permalink but my title is wrong, i havent even tried to get the thumbnail.
<?php
$args = array(
'post_type'=> 'post',
'order' => 'date'
);
echo get_the_title($recent)."<br/>";
wp_reset_postdata();
?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I have read some things in the documentation but i still cant figure it out.
i want an ul so people can click through the posts but the problem is, i am making my blogs in an plugin. thats the wrong title and thumbnail, So i want the permalink of cubeportfolio(this is an post_type). and I want the title from posts(this is an post_type)
Hole code:
$posts = get_posts(array(
'post_type' => 'cubeportfolio',
'order' => 'date' ));
if( $posts ): ?>
<?php foreach( $posts as $post ): setup_postdata( $post ) ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php wp_reset_postdata(); ?>
<?php
$args = array(
'post_type'=> 'post',
'order' => 'date'
);
echo get_the_title($recent)."<br/>";
wp_reset_postdata();
?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Any help would be appreciated,
Id love to help you out but I would need to understand what it is you are trying to do. From the above code you are pulling in 1 "cubeportfolio" and then doing another query to pull in "posts" and then output their titles. If you are trying to create a unordered list of links to the "cubeportfolio" there is not need for the second query.
<?php $args = array(
'post_type' => 'cubeportfolio',
'posts_per_page' => 1,
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ) { ?>
<ul>
<?php while ( $the_query->have_posts() ) { $the_query->the_post();
<li>
<?php the_title(); ?>
</li>
<?php } wp_reset_postdata(); ?>
</ul>
<?php endif; ?>
This will only pull in 1 of the "cubeportfolio" items because the query is set to 1 post_per_page. Hope this helps, if not please help me understand your plan and I can update the code to help you out.

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