I just created a new page template and want to print all posts with category news only.
Used this code based on wordpress codex when it works.
<?php
query_posts(
array (
'post_type' => 'post',
'category_name' => 'news',
'category' => 1,
'posts_per_page' => 3 )
);
// The Loop
while ( have_posts() ) : the_post();
the_title();
the_content();
endwhile;
// Reset Query
wp_reset_query();
?>
How can wrap the title into an h1 tag and the content into a div box?
I tried this but it is giving me syntax error:
<h1><?php the_title(); ?></h1>
Hope you can help.
<?php global $post;
$args = array(
'posts_per_page' => 3,
'post_type' => 'post',
'category' => 1
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
the_title( '<h1 class="entry-title">', '</h1>' );
endforeach;
wp_reset_postdata();
?>
I think you should remove category = 1 in a parameter of the arguments.
<?php
global $post;
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'offset'=> 1,
'category_name' => 'news'
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
the_title();
the_content();
endforeach;
wp_reset_postdata();
?>
Note: Please use slug of category in category_name. Also you have syntax error in
<?php the_content(); ?>"> //removed ">
Hope this will work for you. Thank you
I have added div tag for content & h1 tag for title & updated your code.
Please find your updated code:
<?php
query_posts(
array (
'post_type' => 'post',
'category_name' => 'news',
'posts_per_page' => 3
)
);
// The Loop
while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<div class="wrap">
<?php the_content(); ?>
</div>
<?php endwhile;
// Reset Query
wp_reset_query();
?>
Hope, this will help you.
Related
If I select post on frontend in WP other posts not display in loop. If not selected sticky post all post is normally display.
`
<?php
$sticky = get_option( 'sticky_posts' );
$args = array (
'post_type' => 'post',
'post__in' => $sticky,
'post_status' => 'publish',
'posts_per_page' => 8,
);
$the_query = new WP_Query($args);
?>
<?php if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('Not found'); ?></p>
<?php endif; ?>
`
I want to display posts in the subcategory but don't know what to do. But this code does not show the post in the category but the entire post in the category. Can you help me?
<?php
$query = new WP_Query( array(
'post_type' => 'hotcheck',
'topics__in' => array($cat->term_id),
) );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="entry">
<h2 class="title"><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<!-- show pagination here -->
<?php else : ?>
Please try below code
$args = array(
'post_type' => 'hotcheck',
'tax_query' => array(
array(
'taxonomy' => 'category', // Add your taxonomy name here
'field' => 'term_id',
'terms' => $cat->term_id,
),
),
);
$query = new WP_Query($args);
Hope its work for you Thanks!
I am using Custom Post Type to get its contents by using Custom Taxonomy in different pages.
However, the contents are being displayed even if the post type has no taxonomy.
P.S. I am using ACV as well to get the fields of Taxonomy
Here's the code:
<?php if( get_field('review_category_name') ):
$taxonomy_slug = get_field('review_category_name'); //Advanced Custom Field here
endif; ?>
<?php $args = array( 'post_type' => 'reviews','tax_query' => array(
'taxonomy' => 'reviews_category',
'field' => 'slug',
'terms' => $taxonomy_slug,
), 'posts_per_page' => 5 );
$loop = new WP_Query( $args ); ?>
<div class="">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
Am I missing anything here?
$taxonomy_slug is a variable to get the custom post type slug.
Wrap everything in the get_field('review_category_name') if check:
<?php if( get_field('review_category_name') ): ?>
<?php
$taxonomy_slug = get_field('review_category_name'); //Advanced Custom Field here
$args = array( 'post_type' => 'reviews','tax_query' => array(
'taxonomy' => 'reviews_category',
'field' => 'slug',
'terms' => $taxonomy_slug,
), 'posts_per_page' => 5 );
$loop = new WP_Query( $args );
?>
<div class="">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
PS your example was missing the endwhile
I have used the following code to display the one post from each post.
$post_types = array('a', 's','d','f','g');//post type names
foreach( $post_types as $post_type) :
// The Query
$the_query = new WP_Query( array( 'post_type' => $post_type,
'orderby' => 'post_date',
'order' => 'DESC',
));
// The Loop
?>
<?php
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if ( has_post_thumbnail() ) : ?>
<div class="issue-content masonItem <?php echo $post_type; ?>">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id() );
$resizedUrl = get_bloginfo('template_url')."/libs/timthumb.php?src=".$url."&w=327&h=204&zc=1"; ?>
<img src="<?php echo $resizedUrl; ?>" alt="<?php the_title(); ?>" class="masonry-thumb" />
<?php get_template_part( 'content-issues', 'page' );?>
</div><!--issue-content--><!--Mason Item-->
<?php endif; ?>
<?php endwhile;
Now i want to display 2 posts from each post type. How can i do this ? I got the $post_count idea. But i cant know use it in my code.
Try this :
'posts_per_page' => 2,
You try This
$the_query = new WP_Query( array( 'post_type' => $post_type,'orderby' => 'post_date','posts_per_page' => 2,'order' => 'DESC'));
<ul>
<?php
global $post;
$args = array( 'numberposts' => 12, 'orderby' => 'date', 'year' => '2012', 'order' => 'ASC', 'document_language' => 'english');
$myposts = get_documents( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php endforeach;
?>
</ul>
How can I put the into if have posts query? It's basically custom post type query but its using document revisions plugin (so get_posts is get_documents)?
Many thanks!
If you just want to check if the query returned any posts. You can still do it with your code.
Here is example:
<?php
global $post;
// This is your query
$args = array( 'numberposts' => 12, 'orderby' => 'date', 'year' => '2012', 'order' => 'ASC', 'document_language' => 'english');
$myposts = get_documents( $args );
if (count($myposts) > 0 ) : // If the query returned results
?>
<ul>
<?php foreach( $myposts as $post ) : setup_postdata($post); // for each post in array ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>