I researched several tutorials and nothing ...
can someone tell me how do I print only the category "news" in the article page?
Theme Onepress*
<main id="main" class="site-main" role="main">
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', 'list' );
?>
<?php endwhile; ?>
<?php the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php endif; ?>
</main><!-- #main -->
Maybe the easiest way is to create your own query:
// WP_Query arguments
$args = array (
'post_type' => array( 'news' ),
'post_status' => array( 'publish' ),
'posts_per_page' => '-1',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
This gives you all the query results that are: published and of the 'news' custom post type.
// WP_Query arguments
$args = array (
'post_status' => array( 'publish' ),
'category_name' => 'news',
'posts_per_page' => '-1',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
The second one gives you all the query results, that are published and in the category 'news'.
Related
it loops correctly 10 times but with wrong content. it alloys using the content with id 11
show_posts with 10 posts (thats correct) but always first content
my function show_posts in Theme.php :
public function show_posts() { # show_posts220914() {
$args = array(
'numberposts' => 200
);
$my_posts = get_posts( $args );
if( ! empty( $my_posts ) ){
foreach ( $my_posts as $post ){
get_template_part( 'content','content');
}
}
}
so result in http://localhost/wordpress/ is
<li>11</li><li>11</li><li>11</li><li>11</li><li>11</li><li>11</li><li>11</li><li>11</li><li>11</li><li>11</li>
show_posts with correct constant but to less posts:
in this next try i have correct constant but to less posts:
public function show_posts(){
global $wp_query;
$args = array_merge( $wp_query->query_vars, ['posts_per_page' => 200 ] ); # has no effect here
query_posts( $args ); # has no effect here
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$post= the_post();
get_template_part( 'content','content');
endwhile;
endif;
}
so result in http://localhost/wordpress/ is
<li>11</li><li>12</li><li>13</li><li>14</li><li></li>
my content.php:
<li>
<?php the_title(); ?>
</li>
my home.php:
<ol>
<?php
do_action( 'home_content' );
?>
</ol>
i call it with http://localhost/wordpress/.
i don't use any pages. means http://localhost/wordpress/wp-admin/edit.php?post_type=page tells me No pages found.
i found inspiration here:
get_template_part/#parameters
show-the-same-post
query_posts('posts_per_page=20')
i using WordPress 6.0.2
any idea?
Your second WordPress loop looks like it could almost work ... I'm not sure what $wp_query->query_vars could contain that you are merging with your post_per_page constraint.
Try this, more standard loop (and/or read about the loop on WordPress' Codex):
function show_post(){
$args = [
'posts_per_page' => 10,
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
];
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
get_template_part( 'content','content');
endwhile;
endif;
// Restore original Post Data
wp_reset_postdata();
}
i get this correct output
<li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li>
if i use this:
Theme.php :
public function show_posts_so() {
$posts = get_posts(array('numberposts' => -1));
if ( count($posts)>0 ) :
foreach($posts as $this_special_post){
get_template_part( 'content','content', $this_special_post);
}
endif;
}
content.php :
<li><?php echo $args->ID ?></li>
functions.php :
add_action( 'home_content', [$theme ,'show_posts_so']);
home.php :
<?php do_action( 'home_content' ); ?>
it helps me reading this: What is the real purpose of magic method __set_state in PHP?
and the use of the var_export command sometimes
Added this code to display wp custom post by category, but unable to get pagination to work when added posts_per_page="5"
<?php query_posts('post_type=encounters_news'); while (have_posts()) : the_post(); ?>
<?php $terms = get_the_terms($post->ID, 'encounters_news_categories');
foreach($terms as $item):
if($item->slug == "chapter-news"):?>
<?php get_template_part( 'content-news', 'page' ); ?>
<?php //comments_template( '', true ); ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endwhile; wp_reset_query(); ?>
<?php encounters_content_nav( 'post-nav' ); ?>
Rewrote and change to this code
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$post_type = 'encounters_news';
$tax = 'chapter-news';
$tax_terms = get_terms($tax);
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$query_args = array(
'post_type' => $post_type,
'$tax' => $tax_term->slug,
'showposts' => 5,
'paged' => $paged
); wp_reset_query();
}}
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<?php get_template_part( 'content-news', 'page' ); ?>
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<div class="navigation">
<div class="alignleft"><?php echo get_previous_posts_link( '« Previous' ); // display newer posts link ?></div>
<div class="alignright"><?php echo get_next_posts_link( 'More »', $the_query->max_num_pages ); // display older posts link ?></div>
This code creates the pagination as desired, but displays all the Encounters News Categories instead of just the Chapter News category.
Suggestions will be greatly appreciated.
Resolved
<?php
$query = new WP_Query( array(
'post_type' => '', // name of post type.
'tax_query' => array(
array(
'taxonomy' => '', // taxonomy name
'field' => '', // term_id, slug or name
'terms' => , // term id, term slug or term name
)
),'showposts' => 5,
'paged'=>$paged
) );
while ( $query->have_posts() ) : $query->the_post();
// do stuff here....
endwhile;
/**
* reset the orignal query
* we should use this to reset wp_query
*/
wp_reset_query();?>
im trying to display only children posts, i don't need pagination. In theory this code should query all posts and in second query get posts that have parent. But in reality it displaying all posts with parent.
<?php
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$args1 = [
'post_type' => 'drama',
'post_parent' => 0, // Only return top level pages
'nopaging' => true, // Alias of posts_per_page => -1, Get all top level pages
'fields' => 'ids' // Only get pages ID's for performance
];
$exclude_parents = get_posts( $args1 );
// Now we can run our query as normal
$args = [
'post_type' => 'drama',
'post__not_in' => $excluse_parents, // Exclude parent pages
'paged' => $paged,
];
$query = new WP_Query( $args );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php $parent_p = wp_get_post_parent_id( $post_ID ); ?>
<?php if ( 0 == 0 ) { ?>
<div class="col-md-12"><span><?php the_title(); ?></span></div>
<?php } ?>
<?php endwhile; wp_reset_postdata(); ?>
<div class="clearfix"></div>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
If you are looking for an algo to get those posts which have no child post then the algo should be
Step 1 : get all post id in an array(post_id_arr).
Step 2: get all post's parent id in another array(parent_id_arr);
Step 3: run a loop for parent_id_arr and remove the parent ids from post_id_arr.
Step 4: Now the post_id_arr will have only child post ids ,
If I want to loop 3 posts from a specific category in index.php of WordPress than what I've to do?
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part( 'content', get_post_format() );
?>
<?php endwhile; ?>
<?php the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
I want to loop 3 post from a category before <?php get_template_part( 'content', 'none' ); ?>
Try this one
$args = array(
'cat' => <your category ID>,
'posts_per_page' => 3
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
// ## write your code here..
}
}
You have to put the category ID.
If you have the category name, then use this:
$args = array(
'category_name' =>
);
For more information check this out:
http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
You can make use of pre_get_posts to alter the main query to get 3 posts from a specific category in the homepage. Add the following to functions.php and be sure to add the category id in the appropriate place
add_action( 'pre_get_posts', function ( $q ) {
if( $q->is_home() && $q->is_main_query() ) {
$q->set( 'cat', CATEGORY_ID );
$q->set( 'posts_per_page', 3 );
}
});
I'm using already designed theme for wordpress, and now instead of regular blog posts I would like to display WooCommerce products (which are custom post types I persume).
This is the current query with display loop:
<?php
$args = array(
//'posts_per_page' => '2',
'paged' => get_query_var('paged')
);
$homepage_query = new WP_Query($args);
?>
<?php //query_posts('posts_per_page=4&paged='.get_query_var('paged')); ?>
<?php if ( have_posts() ) : ?>
<?php while ( $homepage_query->have_posts() ) : $homepage_query->the_post(); ?>
<?php if($style == 'blog_style') { ?>
<div id="blog-style" class="post-box">
<?php get_template_part('content', 'blog'); ?>
</div>
<?php } else { ?>
<div class="post-box grid_4 <?php aero_post_box_class(); ?>">
<?php get_template_part('content', ''); ?>
</div>
<?php } ?>
<?php endwhile; ?>
Is there a way to add options to $args so the loop displays WooCommerce products? I'm also using pagination with this loop, which is required on this project, so that's why it's important to use this loop.
You should be able to access products through the loop, setting the post_type arg to product:
<?php
// Setup your custom query
$args = array( 'post_type' => 'product', ... );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>">
<?php the_title(); ?>
</a>
<?php endwhile; wp_reset_query(); // Remember to reset ?>
This is the proper way to re-create and customize the WooCommerce product loop:
if(!function_exists('wc_get_products')) {
return;
}
$paged = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1; // if your custom loop is on a static front page then check for the query var 'page' instead of 'paged', see https://developer.wordpress.org/reference/classes/wp_query/#pagination-parameters
$ordering = WC()->query->get_catalog_ordering_args();
$ordering['orderby'] = array_shift(explode(' ', $ordering['orderby']));
$ordering['orderby'] = stristr($ordering['orderby'], 'price') ? 'meta_value_num' : $ordering['orderby'];
$products_per_page = apply_filters('loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page());
$products_ids = wc_get_products(array(
'status' => 'publish',
'limit' => $products_per_page,
'page' => $paged,
'paginate' => true,
'return' => 'ids',
'orderby' => $ordering['orderby'],
'order' => $ordering['order'],
));
wc_set_loop_prop('current_page', $paged);
wc_set_loop_prop('is_paginated', wc_string_to_bool(true));
wc_set_loop_prop('page_template', get_page_template_slug());
wc_set_loop_prop('per_page', $products_per_page);
wc_set_loop_prop('total', $products_ids->total);
wc_set_loop_prop('total_pages', $products_ids->max_num_pages);
if($products_ids) {
do_action('woocommerce_before_shop_loop');
woocommerce_product_loop_start();
foreach($products_ids->products as $featured_product) {
$post_object = get_post($featured_product);
setup_postdata($GLOBALS['post'] =& $post_object);
wc_get_template_part('content', 'product');
}
wp_reset_postdata();
woocommerce_product_loop_end();
do_action('woocommerce_after_shop_loop');
} else {
do_action('woocommerce_no_products_found');
}
Using the code above, you would customize the wc_get_products() arguments to get the IDs of the products you want (if you have specific criteria). Once that code is in place, all the features of a native WooCommerce loop will be available to you—pagination, ordering, etc. This method is superior to WP_Query and get_posts() because those two methods can break.
I've written a more detailed blog post about custom WooCommerce loops here: https://cfxdesign.com/create-a-custom-woocommerce-product-loop-the-right-way/
You can Also get Category using thi code
$terms = get_terms('product_cat');
foreach ($terms as $term) {
$term_link = get_term_link( $term, 'product_cat' );
echo '<li>' . $term->name . '</li>';
}
If You want only parent category then
wp_list_categories('taxonomy=product_cat&orderby=order&title_li=&depth=1');