I've figured a way to enable adding categories to a PAGE (not a post). And I was just wondering if there was a way to display PAGES in a post loop, this is my code:
<?php query_posts('cat=540'); ?>
<div class="blog_module">
<?php if(has_post_thumbnail()) {
the_post_thumbnail(array(150,150));
} else {
echo '<img class="alignleft" src="'.get_bloginfo("template_url").'/images/empty_150_150_thumb.gif" width="150" height="150" />';
}
?>
<div class="entry">
<h3 class="blog_header"><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
<a class="button_link" href="<?php the_permalink(); ?>"><span>Read More</span></a>
</div>
</div>
However, this displays:
Which isn't what I want, it ONLY DISPLAYS POSTS and not the pages I have assigned to Category ID 540.
Please could someone help with a loop that would display pages that have been assigned to a category.
Thank you in advance.
Sure its easy. I guess the following should work (untested):
query_posts(array('cat'=>540,'post_type'=>page));
You can also use WP_Query which is more flexible than query_posts:
$q = new WP_Query(array('cat'=>540,'post_type'=>page));
while($q->have_posts()): $q->the_post();
// your post here
endwhile;
This should be even more flexible:
$args = array(
'post_type' => 'page',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => 540
),
),
);
$query = new WP_Query( $args );
If you need posts AND pages try this:
'post_type' => 'any'
All information you need you can find on these links: https://developer.wordpress.org/reference/functions/query_posts/
https://codex.wordpress.org/Class_Reference/WP_Query
Related
I've have created a custom post type with a taxonomy. I need to sort the taxonomy posts by a meta value in my custom post type.
I am using the plugin Advanced Custom Fields to insert a separate meta value field to my posts in my custom post types. So far I've
been able to get posts taxonomies sort by its value. My problem is that I get "all" of my taxonomies post printing out on the front-end.
I would like to get the posts for each taxonomies it's related to, not ALL. I've been testing to get the term id by implement it on the
wp_query array but with no success. I simply want to display my taxonomies individually and grab its related posts, is this possible, what am I doing wrong?
Here's my code...
<?php
get_header();
if( have_posts() ) {
?>
<section class="section container">
<div class="row">
<?php
// gets our taxonomy title
global $wp_query;
$term = $wp_query->get_queried_object();
$title = $term->name;
?>
<!-- show our taxonomy title on the front-end of the site -->
<header id="<?php echo $term->slug;?>" class="col-md-12">
<h1><span class="glyphicon glyphicon-circle-arrow-left" aria-hidden="true"></span><?php echo $title; ?></h1>
</header>`enter code here`
<?php
// wp_query-loop for our custom post typ "Personal", sort by meta_value
$wp_query = new WP_Query(array(
'post_type' => 'Personal',
'posts_per_page' => -1,
'meta_key' => 'sorterings_nummer',
'orderby' => '$term->term_id meta_value',
'ordertax' => 'DESC',
'order' => 'ASC'
));
// gets our custom post type posts with a thumbnail, title and contact details
while( $wp_query->have_posts() ) {
$wp_query->the_post();
$class = get_field('sorterings_nummer') ? 'class="sorterings_nummer"' : '';
$titel = get_post_meta(get_the_ID(), 'titel');
$telefon = get_post_meta(get_the_ID(), 'telefon');
$mobil = get_post_meta(get_the_ID(), 'mobil');
$mail = get_post_meta(get_the_ID(), 'mail');
add_filter('the_content', 'wpautop');
?>
<article class="col-xs-6 col-sm-4 col-md-4 col-lg-3" >
<div class="align-center">
<div class="content--thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<header class="content--title">
<h2><?php the_title(); ?></h2>
</header>
<section class="content--contactDetails">
<h3 class="titel"><?php echo $titel[0] ? $titel[0]: ''; ?></h3>
<p class="telefon"><strong><?php echo $telefon[0] ? $telefon[0]: ''; ?></strong></p>
<p>
<a class="mail" href="mailto:<?php echo $mail[0] ?>"><?php echo $mail[0] ? $mail[0]: ''; ?></a>
<a class="mobil" href="tel:<?php echo $mobil[0] ?>"><?php echo $mobil[0] ? $mobil[0]: ''; ?></a>
</p>
</section>
</div>
</article>
<?php
} // while content
?>
</div> <!-- .row -->
</section> <!-- .container -->
<?php
} // if
get_footer();
Please try tax_query() for retrieving posts assigned to the particular taxonomy term.
Please find your updated $wp_query() code below:
$wp_query = new WP_Query(array(
'post_type' => 'Personal',
'posts_per_page' => -1,
'tax_query' => array(array('taxonomy' => 'taxonomy_slug_name', 'field' => 'id', 'terms' => $term->term_id )),
'meta_key' => 'sorterings_nummer',
'orderby' => 'meta_value',
'ordertax' => 'DESC',
'order' => 'ASC'
));
Please write your 'taxonomy_slug_name' in the taxonomy column of tax_query().
Hope, this may be helpful to you.
I have a custom taxonomy (support) and a custom post type (question) both related.
In my taxonomy-support.php template file, I use the following query:
<?php
$current_category = get_term_by('id', get_queried_object()->term_id, 'support');
$questions = new WP_Query(array(
'post_type' => array('question'),
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => ((get_query_var('paged')) ? get_query_var('paged') : 1),
'nopaging' => false,
'tax_query' => array(
array(
'taxonomy' => 'support',
'terms' => array($current_category->term_id)
)
),
'orderby' => 'menu_order',
'order' => 'ASC'
));
?>
And the loop
<?php if ($questions->have_posts()): ?>
<ul>
<?php while ($questions->have_posts()) : $questions->the_post(); ?>
<li>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</ul>
<div class="clearfix">
<div class="pull-left">
<?php previous_posts_link('← ' . __('Previous', 'my-theme' ), $questions->max_num_pages); ?>
</div>
<div class="pull-right">
<?php next_posts_link(__('Next', 'my-theme') . ' →', $questions->max_num_pages); ?>
</div>
</div>
<?php endif; ?>
As you can see I defined 2 posts per page.
When I visit the page, it shows me 2 posts then I go to page 2, it still works but when I go to page 3 or up, it shows 404.
Any idea?
I'm using WordPress 3.8.2 with no plugin installed.
Thanks
I was happy to find your post as I was fighting for the exact same issue (less happy to find no answer yet ;) ).
Anyway I finally found something interesting here: https://wordpress.org/support/topic/set-number-of-posts-for-custom-taxonomy
This made the trick for me.
I reckon you were into the situation where the value for your global posts_per_page option (as set up in the admin) was greater than the one for your taxonomy, which made the system screw up.
Well, not that much when you think about it: during its lifecycle, having the global posts_per_page option as is, the request does not validate the "paginated" url, thus never even load your taxonomy-support.php template file, where you tell it that the posts_per_page should be different.
You then must tell it earlier, during the intialization process. The 'pre_get_posts' action is the one.
Hope it helps someone!
I want to show all the posts of my wordpress blog on a single blog page, for some reason the solutions that I am finding in Google are 5-6 years old, I was wondering which is the best and easiest way to do it ?
Thanks
I think this will be the best one in my case ;)
$args = array(
'posts_per_page' => -1,
'post_type' => 'page',
'orderby' => 'DESC'
);
$home_query = new WP_Query($args);
if($home_query->have_posts()): while($home_query->have_posts()) : $home_query->the_post();
get_template_part('pages');
endwhile;
else:
get_template_part( 'no-results', 'home' );
endif;
and in pages.php
<section class="page-<?php echo get_post_type();?>-<?php the_ID();?> id="<?php echo $post->post_name;?>">
<div class="container">
<?php
the_content();
?>
</div>
</section>
Thing is, In the homepage of my theme, I want to show post from different category in Different Div. Each DIV will contain 3 post from a category. I need a loop that can pick last 3 post from a specific Category. Can't find any suitable ans for it.
To explain things more easily, here is a demo picture of the Content section,
http://i.imgur.com/5QSzAIS.png
It will be a great help, if someone help me with the code !
<?php query_posts('cat=10&posts_per_page=3'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
This should get you started. You need to use this code twice. Where it says cat=10, you should enter your category ID (you can check this when you click on a Category from the admin panel, the the browser it will show something like this http://yourwebsite.com/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=4&post_type=post)
Where it says tag_ID is the ID of your category.
I'm currently using a different method on a page of the site I'm building which allows me to run multiple loops in one page and specify the category for each one. This method I personally like better as it is more straightforward to me, and I can define the category with the slug instead of the ID.
Instead of using have_posts() and such, you use WP_Query() after defining your array and then wp_reset_postdata() to end your loop. The benefit is that you can keep running loops this way.
I'm also loading the data from custom fields in my posts using get_post_meta, but this method will work without that stuff.
<div class="audioGrid">
<?php
$args = array( 'post_type' => 'post',
'category_name' => 'audio',
'posts_per_page' => 3,
'order' => 'DESC' );
$query1 = new WP_Query($args);
while ( $query1->have_posts() ) {
$query1->the_post();
?>
<div id="<?php echo( basename(get_permalink()) ); ?>" class="grid_item">
<?php the_post_thumbnail( 'audio-thumb' ); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_post_meta($post->ID, 'post_description', true); ?></p>
<a target="blank" href="<?php echo get_post_meta($post->ID, 'audio_link', true); ?>"></a>
</div>
<?php the_content(); ?>
<?php } ?>
</div> <?php // end Audio Grid ?>
<?php wp_reset_postdata(); ?>
<div class="videoGrid">
<?php
$args2 = array( 'post_type' => 'post',
'category_name' => 'video',
'posts_per_page' => 3,
'order' => 'DESC' );
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ) {
$query2->the_post();
?>
<div id="<?php echo( basename(get_permalink()) ); ?>" class="grid_item">
<?php the_post_thumbnail( 'video-thumb' ); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_post_meta($post->ID, 'post_description', true); ?></p>
<a target="blank" href="<?php echo get_post_meta($post->ID, 'video_link', true); ?>"></a>
</div>
<?php the_content(); ?>
<?php } ?>
</div> <?php // end Video Grid ?>
<?php wp_reset_postdata(); ?>
Another cool thing I'm doing is using a custom field to define the order of things and using meta_key and meta_value_num to get that number and force the order how I want, and since this site isn't complicated, defining the order this way is convenient. I just use leading zeroes to make it easy: 001, 002, 003, etc
<?php
$args2 = array( 'post_type' => 'post',
'category_name' => 'video',
'posts_per_page' => 3,
'meta_key' => 'video_order',
'orderby' => 'meta_value_num',
'order' => 'ASC' );
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ) {
$query2->the_post();
?>
Anyway, hope this helps if you need to use multiple loops to pull posts from different categories.
I have add custom post type field name 'Movies'. Now I have done these things and its working great, but the problem is, (i.e When I'm click on any movie, its showing me only one movie post, (i.e I'm click on avatar movie its showing me avatar movie post, but when I'm click on stargate movie its showing me avatar movie post. Please help its a big issue) anyone who can help me to make this code exactly which I want.
in my functions.php I have add this code:
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'Movies',
array(
'labels' => array(
'name' => __( 'movie' ),
'singular_name' => __( 'movie' )
),
'public' => true,
'has_archive' => true,
)
);
}
Then in my template file add this where I want to show post:
<?php
$args = array( 'post_type' => 'movies', 'posts_per_page' => 1 );
$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; ?>
I'm new for this thing, so please explain me as much as you can, where I paste it or what I do?
I can't see how you structured your links to those movie-posts, but you should give them a variable, so the page where the movie-post shows up knows what to show!
e.g $moviename
and in your template you modify the $args array to:
$args = array( 'post_type' => 'movies', 'posts_per_page' => 1, 'name' => $_GET['movie'] );
Should work, at least that explains why it always displays the same movie-post:
Your query hast no information what movie to display, at the moment it just takes the movie-posts table and displays the first one - because of the posts_per_page limit to 1.
Hope that makes sense...