I use this code to get posts via Wordpress REST API, this code work very and get posts well but i dont know how i can get post thumbnail wit this. I added ?_embed to end of URL in wp_remote_get function and thumbnail address are in output but i don't no how i can fetch and display image url in php.
<?php
function get_posts_via_rest() {
// Initialize variable.
$allposts = '';
// Enter the name of your blog here followed by /wp-json/wp/v2/posts and add filters like this one that limits the result to 2 posts.
$response = wp_remote_get( 'https://video.amniat98.com/wp-json/wp/v2/posts?_embed' );
// Exit if error.
if ( is_wp_error( $response ) ) {
return;
}
// Get the body.
$posts = json_decode( wp_remote_retrieve_body( $response ) );
// Exit if nothing is returned.
if ( empty( $posts ) ) {
return;
}
// If there are posts.
if ( ! empty( $posts ) ) {
// For each post.
foreach ( $posts as $post ) {
// Use print_r($post); to get the details of the post and all available fields
//print_r($post);
// Show a linked title and post date.
$allposts .= '' . esc_html( $post->title->rendered ) . '';
}
return $allposts;
}
}
// Register as a shortcode to be used on the site.
add_shortcode( 'rest_posts', 'get_posts_via_rest' );
You're looking for has_post_thumbnail, the_post_thumbnail and the_post_thumbnail_url.
Example
<?php
/**
* The Loop
* #link https://developer.wordpress.org/themes/basics/the-loop/
*/
if ( have_posts() ):
while ( have_posts() ): the_post();
/**
* has_post_thumbnail()
* #link https://developer.wordpress.org/reference/functions/has_post_thumbnail/
*/
if ( has_post_thumbnail() ):
the_post_thumbnail();
/**
* get_the_post_thumbnail_url()
* #link https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/
*/
the_post_thumbnail_url();
endif;
endwhile;
endif; ?>
Learn more
https://developer.wordpress.org/themes/basics/the-loop/
https://developer.wordpress.org/reference/functions/has_post_thumbnail/
https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/
Related
I need the last post not to be displayed on the post archive page.
What should I do to do this?
You can see the page code of my page archive below.
<?php
/* Start the Loop */
$i=0;
while ( have_posts() ) :
if($i==5) {
echo '
adv code
';
}
the_post();
get_template_part( 'template-parts/content', 'archive' );
$i++;
endwhile;
custom_pagination();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
note: Only the last post. Because the last post of the page archive is displayed manually in the slider of that page. I do not want the last post in the list to be displayed and repeated.
Using offset to the query will do the trick like
function exclude_last_post( $query ) {
if ( $query->is_main_query() && !is_admin() && is_archive() ) {
$query->set( 'offset', '1' );
}
}
add_action( 'pre_get_posts', 'exclude_last_post' );
What I'm trying to achieve in WooCommerce is that on the single product page, the Description tab, I'm trying to add the product page title before the word Description.
Here is my current WooCommerce code:
defined( 'ABSPATH' ) || exit;
global $post;
$heading = apply_filters( 'woocommerce_product_description_heading', __( 'Description', 'woocommerce' ) ); ?>
<?php if ( $heading ) : ?>
<h2>PRODUCT PAGE TITLE HERE <?php echo esc_html( $heading ); ?></h2>
<?php endif; ?>
<?php the_content(); ?>
The problem here, however, is that:
I have to overwrite the template file, is it possible via a hook?
The product title is not dynamic.
As a result, id like the tab to go from Description to BLACK NIKE SHOES Description
Example:
Any advice?
You can use the woocommerce_product_{$tab_key}_tab_title composite filter hook. Where $tab_key is in your case description
Use global $product and $product->get_name() to get the product title. This result can then be added to the existing string.
So you get:
function filter_woocommerce_product_description_tab_title( $title ) {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get title and append to existing string
$title = $product->get_name() . ' ' . $title;
}
return $title;
}
add_filter( 'woocommerce_product_description_tab_title', 'filter_woocommerce_product_description_tab_title', 10, 1 );
Optional: to change the WooCommerce product description heading instead, use the woocommerce_product_description_heading filter hook:
function filter_woocommerce_product_description_heading( $heading ) {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get title and append to existing string
$heading = $product->get_name() . ' ' . $heading;
}
return $heading;
}
add_filter( 'woocommerce_product_description_heading', 'filter_woocommerce_product_description_heading', 10, 1 );
I want to know what is the logic of the code that they use for this type of restriction and how can I do it on my WordPress website.
This is just a simple example of how you could do this.
Method A:
You need to update the excerpt of each article.
Edit single.php:
while ( have_posts() ) {
the_post();
if ( is_user_logged_in() ) :
// Also you can include the template part here.
the_content();
else :
// Show just the article excerpt.
the_excerpt();
endif;
}
Method B
Here you need to edit articles and add Read More tag (Shift + Alt + T) in the place where you want to restrict the article visibility.
Edit single.php:
while ( have_posts() ) {
the_post();
// Show part of article for visitors.
if ( ! is_user_logged_in() ) :
global $more;
$more = 0;
endif;
the_content();
}
Edit functions.php:
/**
* Change read more link.
*
* #return mixed Sign up page link.
*/
function modify_read_more_link() {
$url = wp_login_url(); // Your sign up page url goes here.
$title = __( 'Login or Sign up to view the rest of the article.', 'text-domain' );
return sprintf( '<a class="more-link" href="%s" title="%s">%s</a>',
esc_url( $url ),
esc_attr( $title ),
$title
);
}
add_filter( 'the_content_more_link', 'modify_read_more_link' );
I want to display the value of a custom-field in the content-product.php in woocommerce. I did like this, but the output is only the word "array".
where is my mistake?
Thanks a lot!
rabox
<?php if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $product;
// Ensure visibility
if ( empty( $product ) || ! $product->is_visible() ) {
return;
}
?>
<li <?php post_class(); ?>>
<span class="product_additional_info"><?php echo get_post_meta($post-
>ID, ‚additional-info‘, true); ?></span>
I found a piece of code that works here https://wordpress.stackexchange.com/questions/179451/unable-to-display-custom-fields-on-woocommerce-product-pages
It goes like that:
<?php
$custom_fields = get_post_custom($post->ID);
$my_custom_field = $custom_fields["Name of your Field"];
foreach ( $my_custom_field as $key => $value ) {
echo "<strong>$key: </strong> $value <br />";
}
?>
Would be interesting to know why I cannot use the "wordpress-way" of using custom fields.
Woocommerce content-product.php uses global $product; so you can get the product id with $product->get_id()
Now, you can get the value of custom field by passing the product id and custom field name in get_post_meta();
Example: get_post_meta( $product->get_id(), '_your_custom_field', true );
i created a custom field in a product called course-date. I asissnged it a date e.g Jan 30. This is what I have in the email yet nothing shows.. am i missing something? code edited with new snippet below
<?php
/**
* Customer processing order email
*
* #author WooThemes
* #package WooCommerce/Templates/Emails
* #version 2.4.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<?php
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
$course_date=get_post_meta($product_id, 'course-date', true);
//Note $course_date will get overwritten if there are more than one items in the order.
}
?>
<?php do_action('woocommerce_email_header', $email_heading); ?>
<p>This email is to notify you of your recent registration for the <b>Level 1 - Think and Grow Rich Institute Course</b>!</p>
<p>You are registered to attend on:</p>
<p><?php printf( __( '<b>Registration ID:</b> %s', 'woocommerce' ), $order->get_order_number() ); ?></p>
<p><b>Course Registration Date:</b> <?php echo get_post_meta($post->id, 'course-date', true); ?></p>
<p><b>Time:</b> 8am Registration - 9am Event Begins (See Full Schedule)</p>
<p><b>Location:</b> 240 Belfield Rd, Toronto, ON M9W 1H3</p>
<p>If you have any questions please contact us by e-mail: contact#thinkandgrowrichinstitute.com.</p>
<p>Sincerely,<BR/>
The Think and Grow Rich Institute Team</p><BR/>
<p><em><b>"Whatever The Mind Can Conceive and Believe, It Will Achieve."</b> - Napoleon Hill</em></p>
<?php do_action( 'woocommerce_email_before_order_table', $order, $sent_to_admin, $plain_text ); ?>
<?php do_action( 'woocommerce_email_after_order_table', $order, $sent_to_admin, $plain_text ); ?>
<?php do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text ); ?>
<?php do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text ); ?>
<?php do_action( 'woocommerce_email_footer' ); ?>
Hope you get to learn more from this answer and it adds to you knowledge of WooCommerce.
For the Snippet you sent of the "Customer processing order email".
The system is unable to find the value of $_product->id here. So it is not working.
You have to debug it by print the Product id itself in the initial test emails.
If you are unable to get the product id. Here is a bit more information.
An order may have more than one products. So finding the product with the meta fields can be troublesome especially when you are having a question of which product you should show the meta field of.
Easy way is to use the given object $order retrieve the order id from this object then find all the products associated in this order and then find meta field value for those products.
<?php
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
$course_date=get_post_meta($product_id, 'course-date', true);
if(isset($course_date) && $course_date!=""){
echo "<p><b>Course Registration Date:</b>".$course_date."</p>";
}
}
?>
Let us know whether this gave you any insight of the issue.
Here's an untested example of how to add a string to the "customer_processing_email" without overriding the template. You would add this code to your theme's functions.php and the text should appear prior to the order table.
add_action( 'woocommerce_email_order_details', 'kia_custom_order_details', 5, 4 );
function kia_custom_order_details( $order, $sent_to_admin, $plain_text, $email ){
if( $email->id == "customer_processing_order" ){
$course_date = null;
// borrowing the loop from WisdmLabs's answer
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
$course_date = get_post_meta($product_id, 'course-date', true);
if( $course_date != '' ) break; //break out of the loop if we find a course date
}
if( $course_date && $plain_text ){
echo "Course Registration Date: " . $course_date . "\n\n";
} else if( $course_date && ! $plain_text ){
echo "<p><b>Course Registration Date:</b> " . $course_date . "</p>" . "/n/n";
}
}
}
Don't forget the different formatting for rich text versus plain text emails. I left most of the formatting for the rest of your text to you as the important part was finding/printing the course date meta.
I still think that using a separate email would be the best approach here, but as far as I can tell you were only missing echoing out $course_date in the right place.