I want to have a static front page on my WordPress and this static one should display the latest 5 posts and below that up to 6 custom meta fields. Thats why I changed the reading settings from last posts to "Static page", because otherwise I would not have the possibility to access the custom meta fields.
How can I now include the latest 5 blogposts on my front-page and use the default post template/output.
I did it with this query:
$latest_blog_posts = new WP_Query( array( 'posts_per_page' => 5 ) );
if ( $latest_blog_posts->have_posts() ) : while ( $latest_blog_posts->have_posts() ) : $latest_blog_posts->the_post();
// Loop output goes here
endwhile; endif;
Do i now have to rewrite the code for the post here completly or can I just include the post template in some way?
Thanks!
You need to write this line to show latest posts:
<?php
wp_get_archives(array('type' => 'postbypost', 'limit' => 10, 'format' => 'html'));
?>
Check this link for more details
Instead of WP_Query you should try
wp_get_recent_posts( $args, $output );
Check more details here
Related
I'm trying to only show posts with the tag '44' or '9' on my wordpress home page.
So far I am here:
<?php get_header();?>
<div class="container pt-5 pb-5">
<h1><?php the_title();?></h1>
<?php
$query = new WP_Query( array( 'tag__in' => array( 44, 9 ) ) );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();?>
<?php the_content();?>
<?php endwhile; endif;?>
</div>
<?php get_footer();?>
But I can't see the posts. Where am I going wrong?
In your query, you have specified that are looking for posts with the tags 44 or 9:
$query = new WP_Query( array( 'tag__in' => array( 44, 9 ) ) );
Note that this doesn't get all posts with these tags - there are also other constraints in this query that are not seen here. WP_Query has a number of default values, so if you don't set them in your args, then it will use those. These default args include:
post_status = public (unless the user is logged in - then private is also included)
post_type = post (not that this is a specific post type and does not include all posts e.g. custom post types)
As your query isn't returning anything, and based on the debugging you've done to show that there are posts with these tags, we know that one of these default values are preventing the query from getting your posts.
Confirm that the posts you want to get are published and public
If you are using a custom post type, you need to include it to your WP_Query. If your CPT slug is my_cpt for example, you can include it as follows:
$args = array( post_type' => 'my_cpt',
array( 'tag__in' => array( 44, 9 ) ) );
$query = new WP_Query( $args);
You can also search for multiple post types in one go by using 'post_type' => array( 'post', 'my_cpt')
FYI if you are not sure what the slug is for your post, go to the page in the WP admin that lists all the posts you are trying to get and look at the url - it will include the post type like this:
https://www.example.com/wp-admin/edit.php?post_type=my_cpt
I have the following tag in my WordPress site:
http://www.example.com/tag/collection-100
and I want to change the tag's url to http://www.example.com/legends as well as make the posts displayed on that page to be ordered by date in ascending manner at the moment they are displayed in a descending way.
I've searched for plugin which can do this but I couldn't find anything on WordPress' site.
I've also tried creating a new template and using the following code in it:
query_posts( 'tag=collection-100&order=ASC' );
which fetches the first 18 posts but the pagination doesn't work properly and instead it always shows the first 18 posts.
Any help much appreciated : )
Create a custom template like this "example-template.php" and write your query like this:
get_header();
$loop = new WP_Query( array( 'post_type' => 'your-post-type', 'orderby' => 'date', 'order' => ASC ) );
while ( $loop->have_posts() ) : $loop->the_post();
//print your needs
endwhile;
get_footer();
Agh this is frustrating. I've been searching for hours on how to do this.
I am creating a WP template that pulls in queries of a category on the page.
My question is how can I make the category in the array dynamically load, whether by slug or whatever, based entirely on what the page title is?
The fancy piece is that it will be three sections on the page via an additional category that also need to be loaded in the template, but those are not dynamic.
So if I can break this down:
Section 1 - TitleCat + Section1Cat
Then
Section 2 - TitleCat + Section2Cat
Then
Section 3 - TitleCat + Section3Cat
That way during content creation, my team can simply click two categories, and the website will automatically put the post excerpts where it needs to go!
It needs to be dynamic so I don't need to build a template for each of the 31 categories the posts are divided into.
EDIT: This is how I was approaching it using ACF. That array can be simply switched out for categories if that's the way to go. Same concept.
<?php $pagecat = $post->post_title; //this copies the page title the page title ?>
<?php
// args
$args = array(
'posts_per_page' => 4,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'page',
'value' => $pagecat,
'compare' => '='
),
array(
'key' => 'section',
'value' => 'Second String',
'compare' => '='
)
)
);
$the_query = new WP_Query( $args );
?>
On any custom / template page you can use this many times over, for example on home page you want to show samples of articles in many different categories:
Code Sample
<!-- Create query -->
<?php query_posts( array ( 'category_name' => 'category-slug-goes-here', posts_per_page' => 1 ) ); ?>
<?php // The Loop
while ( have_posts() ) : the_post();?>
// CODE THAT YOU WANT HERE
<?php endwhile; ?>
// IMPORTANT RESET QUERY
<?php // Reset Query
wp_reset_query(); ?>
Now you can do this OVER and OVER again as many times as you need on one template...
Now for displaying your SINGLE article for some category you can do this in a "Single.php" page use http://www.wordpress.org website for more references on this..
Here is the code for single.php page you need an IF statement and you can target your specific categories that you want to display a specific way...
<?php if(have_posts() && (in_category('category-name-here') || in_category('category-name-here-2') || in_category('category-name-here-3') )) : while(have_posts()) : the_post(); ?>
// NOW HERE use HTML to format the article for those categories.
Now you said you have 30+ different categories I assume you want different displays..
Now you will use that same IF statement over and over again and change the HTML format code in the middle. So you will have in the end ALL page layouts inside 1 page and you will target them by category-name
Best of luck!
I am currently working on a personal project and the this page basically has two tabs each will display the archive for specific categories under one custom post type called webinar.
I am calling the category in one of the tabs using
<?php query_posts('category_name=demos-on-demand-videos'); ?>
However when i do this i' just getting the no post's found screen, what am i doing wrong? I am trying to display the post archive from the category demos-on-demand-videos which is under the webinar custom post type.
use this
query_posts( array( 'post_type' => 'webinar','your-custom-taxnomy' => 'demos-on-demand-videos' ) );
while ( have_posts() ) :
the_post();
$post_id = $post->ID;
endwhile;
Follow this link:
http://eyan16.wordpress.com/2013/09/16/how-to-fetch-posts-from-custom-posts-type-with-custom-taxonomy/
Make a page template for each of your tabs and use this custom loop in it. Make sure to adjust it for your specific post type, taxonomy, or term.
<?php $args=array(
'post_type' => 'webinar', //set the post_type to use.
'taxonomy' => 'demos-on-demand-videos', // set the taxonomy to use.
'term' => 'term1', //set which term to use or comment out if not using.
'posts_per_page' => 10 // how many posts or comment out for all.
);
$webinarloop = new WP_Query($args);
if($webinarloop->have_posts()) : while($webinarloop->have_posts()) :
$webinarloop->the_post();
get_template_part( 'content' ); //or whatever method you use for displaying your content.
endwhile; endif; //end the custom post_type loop
?>
This code works for me just fine
* x is taxonomy name name you have created
* y is category slug
$args = array('post_type' => 'client','posts_per_page'=>'8','order'=>'DESC','x'=>'y');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
i have a custom post type and would like to change the number of posts per page. i use the following code to show/limit the posts:
$args = array(
'post_type' => 'movie',
'posts_per_page' => 3,
'tax_query' => array(
array(
'taxonomy' => $wp_query->queried_object->taxonomy,
'field' => 'slug',
'terms' => $wp_query->queried_object->slug,
)
),
'paged' => get_query_var('paged') ? get_query_var('paged') : 1
);
query_posts( $args );
for this time i have 20 posts with this post type and the default posts per page in wordpress admin (settings/read ...) is set to 10.
if i call the url without a page it shows 3 posts, if i call the url with "page/2/" it shows 3 posts but if i call the page with "page/3/" it shows nothing found ...
i wouldn't change the default value for the posts in the admin - does someone have a idea?
note: to "debug" i let me show the value for "$wp_query->post_count;" - this use the default admin posts per page - 10 ...
regards kai
When you load the page, it will do a query before the template file (archive.php) is called.
That first query will use the defaults set in WordPress; ie. 10 posts per page. By the sounds of things, you're running that second 'query_posts' call within archive.php, but it sounds like that is after you have run the appropriate checks.
Two possible solutions for you:
Modify the first query with the posts_per_page
add_filter( 'pre_get_posts', 'modify_movie_query' );
function modify_movie_query( $wp_query ) {
if( $wp_query->query_vars['post_type'] != 'movie' ) return;
$wp_query->query_vars['posts_per_page'] = 3;
}
Because you have modified a reference to $wp_query, you don't need to return it.
Run the query_posts call at the top of the archive.php template
Option 1 is what I would recommend - it's the most efficient, and the most appropriate.
The approved answer doesn't really work if the archive page is for a custom taxonomy. In that case, you might want to use the following:
add_filter( 'pre_get_posts', 'modify_movie_query' );
function modify_movie_query( $wp_query ) {
if(is_tax('movie-category')){
$wp_query->query_vars['posts_per_page'] = 10;
}
return $wp_query
}