Id like to show the list of post thumbnails from specified user, using this code:
global $user;
$numposts = get_posts( array('author' => $user->ID) );
<?php foreach ($numposts as $numpost) {
the_post_thumbnail();
} ?>
Thumbnails not showing up though. Am I doing something wrong? This code is inserted in plugin php file. thx
Reworked and works:
$numposts = get_posts( array('author' => $user->ID) );
<?php
$my_query = new WP_Query( array( 'author' => $user->ID ) );
while ( $my_query->have_posts() ) : $my_query->the_post();
{
echo the_post_thumbnail();
}
endwhile;
?>
although not sure why it didnt work first time.
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
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'.
I am trying to modify my tag.php inside Wordpress. Basically, I have my general loop set to the default five posts per page. When users click on a tag from my tag cloud, I'd like it to display all of the relevant results by title. Here is what I have in tag.php:
<p>Tag: <?php single_tag_title(); ?></p>
<?php if (have_posts()) : while (have_posts ()) : the_post (); ?>
<p><?php the_title(); ?></p>
<?php endwhile; endif; ?>
This works perfectly, however it only returns the default amount of five. When I try to add a wp_query using ('posts_per_page' => 1000) before the loop, it returns all of my sites posts, rather than just for the appropriate tag. How can add more results? Thanks!
Thank you very much for the reply SilverKenn, I appreciate it. I was able to sort it out using this instead.
<?php $args = array( 'post_type' => 'post', 'posts_per_page' => -1, 'tag'=> get_query_var('tag') );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<p><?php the_title(); ?></p>
instead of editing a php file whenever you try to customize something via wordpress, you can modify almost everything using functions.php depending on how you code the theme,
Check out post_limits filter http://codex.wordpress.org/Plugin_API/Filter_Reference/post_limits
or pre_get_posts http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
you can limit the results using the above filters,
e.g. pre_get_posts
function cmk_custom_result( $wp_query ) {
$post_type = $wp_query->query['post_type'];
if ( $post_type == 'your-post-type' && is_tag() ) {
$wp_query->set('posts_per_page', '25');
}
}
add_filter('pre_get_posts', 'cmk_custom_result');
post_limist
function cmk_post_result_limits( $limit, $query ) {
if ( !is_admin() && $query->is_main_query() && is_tag() ) {
return 'LIMIT 0, 25';
}
return $limit;
}
add_filter( 'post_limits', 'cmk_post_result_limits', 10, 2 );
Use get_query_var to get the correct tag, like: 'tag'=> get_query_var('tag') then use -1 in your posts array for 'posts_per_page' to get an unlimited number of posts for that tag, otherwise change the number to whatever you want to limit the output.
<?php $args = array( 'post_type' => 'post', 'posts_per_page' => -1, 'tag'=> get_query_var('tag') );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; ?>
I have been searching for two days now and have not found a solution. Hopefully someone out there can help. Whenever I add an additional post_type the pagination will not work beyond the first page.
Example:
mywebsite.com/myreport/ works fine and 10 content are displayed
mywebsite.com/myreport/page2/ leads to a 404 error.
Code Inside archive-myreport.php
<?php
$paged = 1;
if(get_query_var('paged')) {
$paged = get_query_var('paged');
} elseif(get_query_var('page')) {
$paged = get_query_var('page');
}
$temp = $wp_query;
$wp_query=null;
$args = array(
'post_type' => array ( 'annual-reports', 'current-reports'),
'post_status' => 'publish',
'paged' => $paged,
);
$wp_query = new WP_Query( $args );
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<a target="blank" href="<?php the_permalink(); ?>">My Report</a>
<?php
endwhile;
previous_posts_link('Previous 10'); ?> <?php next_posts_link('Next 10');
//clear again
$wp_query = null;
//reset
$wp_query = $temp;
?>
Tried Alternative via pre_get_posts:
I tried adding to functions.php, after doing so, the page would redirect to index.php:
function wpa_post_types( $query) {
if ( is_post_type_archive( 'myreport' )) {
$query->set( 'post_type', array( 'annual-reports', 'current-reports' ) );
}
}
add_action( 'pre_get_posts', 'wpa_post_types' );
Instead of WP_query try using query_posts and see if this solves your problem.
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');