How do I show custom recent posts in WordPress? - wordpress

Now it's showing like this: now it's showing like this
I want it to look like this: I want it to look like this

There are two ways to do:
In your WordPress dashboard, go to Appearance » Widgets and add the ‘Recent Posts’ widget to your sidebar.
https://prnt.sc/1rhjdc4
Using wp query
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
);
$loop = new WP_Query($args);
while ( $loop->have_posts() ) {
$loop->the_post();
?>
<div class="entry-content">
<?php the_title(); ?>
<?php the_content(); ?>
</div>
<?php
}

wp_get_recent_posts( array $args = array(), string $output = ARRAY_A )
Please read this article
https://developer.wordpress.org/reference/functions/wp_get_recent_posts/
<?php
$recent_posts = wp_get_recent_posts();
foreach( $recent_posts as $recent ) {
printf( '<li>%2$s</li>',
esc_url( get_permalink( $recent['ID'] ) ),
apply_filters( 'the_title', $recent['post_title'], $recent['ID'] )
);
}
?>

Related

Woocommerce product loop - show all product variation-images

In the wordpress front-page.php theme file i loop through all the woocoommerce products and show the featured-image.
<ul class="products">
<?php
// Setup your custom query
$args = array( 'post_type' => 'product' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>">
<?php the_post_thumbnail( ); ?>
</a>
</li>
<?php endwhile; wp_reset_query(); // Remember to reset ?>
</ul>
Now i have some products which are variable products (different colors). Every color variation has its own image. How can i show all the different variation-images inside this loop? My plan is to create an image-slider with these images.
<?php
$args = array( 'post_type' => 'product' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$product_s = wc_get_product( $loop->post->ID );
if ($product_s->product_type == 'variable') {
$args = array(
'post_parent' => $plan->ID,
'post_type' => 'product_variation',
'numberposts' => -1,
);
$variations = $product_s->get_available_variations();
echo '<pre>';
print_r($variations);
// You may get all images from $variations variable using loop
echo '</pre>';
}
endwhile; wp_reset_query(); // Remember to reset ?>
I do not have tested yet. But hopefully it will work.
Use below code to get image urls
For Wc3+
foreach ( $variations as $variation ) {
echo $variation['image']['url'];
}
And for old Wc versions
foreach ( $variations as $variation ) {
echo $variation['image_src'];
}
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
);
$product_arrray = get_posts($args);
foreach($product_arrray as $prod)
{
$product_id = $prod->ID;
$product = wc_get_product($product_id);
$pvariation = $product->get_available_variations();
echo "<pre>";print_r($pvariation);echo "</pre>";
}

display wp custom posts by catgeory

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();?>

wordpress - post thumbnail is not showing up

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.

Wordpress shortcode always at top of page

I have the following shortcode that I am using to display a custom post on a page:
add_shortcode( 'page-section', 'page_section_shortcode' );
function page_section_shortcode( $atts ) {
global $post;
$post_slug=$post->post_name;
$a = shortcode_atts( array(
'post-name' => 'qwerty',
'bg-color' => 'white'
), $atts );
$post_slug=$post->post_name;
$post_name = $a['post-name'];
$query = new WP_Query( array(
'post_type' => 'page_section',
'name' => $post_name,
) );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div style="background-color: <?php echo $a['bg-color']; ?>" id="<?php global $post; $post_slug=$post->post_name; echo $post_slug; ?>" class="page-section">
<div class="row">
<?php the_content(); ?>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
Everything is working the way I want it to, I can set the content in my custom post and use a short code to pull that content into a page.
The problem is that the content that is pulled in with the short code is always at the top of the page. Using multiple shortcodes in a row they keep their order, but any other content on the page is displayed at the bottom (below all of the shortcode content).
I have tried removing the 'echo' as suggested in another stackoverflow post but cannot seem to find what I am doing wrong.
Replace your code to this code. This code returns generated html so it will not echo at the top of the page
add_shortcode( 'page-section', 'page_section_shortcode' );
function page_section_shortcode( $atts ) {
global $post;
$post_slug=$post->post_name;
$a = shortcode_atts( array(
'post-name' => 'qwerty',
'bg-color' => 'white'
), $atts );
$post_slug=$post->post_name;
$post_name = $a['post-name'];
$query = new WP_Query( array(
'post_type' => 'page_section',
'name' => $post_name,
) );
$returnhtml = '';
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
global $post;
$returnhtml .= '<div style="background-color: '. $a['bg-color'].'" id="'.$post->post_name.'" class="page-section">';
$returnhtml .= '<div class="row">'.get_the_content().'</div>';
$returnhtml .= '</div>';
endwhile;
wp_reset_postdata();
return $returnhtml;
}
}
function header_notification()
{
echo '<div><strong>Any html goes here</strong></div>';
}
add_action('wp_head', 'header_notification');

Making custom WooCommerce loop

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');

Resources