How use corect function for show thumbnail in post?
And how insert 'order' => 'asc'?
My actual code:
function show_thumb() {
$thumb_id = get_post_thumbnail_id($post_id);
$thumb_url = wp_get_attachment_url( $thumb_id );
}
and
<?php echo show_thumb(); ?>
WordPress has built in functions for this
inside the loop you can use
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
Codex Reference
and outside of the loop
<?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>
Codex Reference
you can use this in a loop showing posts...
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
Related
I am customizing an old theme called AmazeTheme which was not specifically made with WooCommerce support. And an woocommerce.php was at theme's root. However with help of plugins like Simply Show Hooks and Show current template I am able to see where to put which info.
But I am having an weird problem. The ACF fields I have added are not visible at all. I even tried to assign the field group inside product loop section.
I also tried the following method.
add_action( 'woocommerce_after_single_product_summary', 'view_acf_field_for_single_product', 10 );
function view_acf_field_for_single_product(){
if (function_exists('the_field')){
the_field('shop_support');
}
}
And inside the loop of single-product.php
<?php while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'single-product' );
$dump = get_fields();
?>
<?php endwhile; // end of the loop. ?>
And then did var_dump($dump) at desired place of the file.
This site's php version is 5.6.40 and WooCommerce version is 3.4.8
WP version is 4.9.18
I have looked up many solutions. Also tried the WooCommerce Hooks but still no clue why ACF not showing.
Can you try this code:
add_action( 'woocommerce_after_single_product_summary', 'view_acf_field_for_single_product', 10 );
function view_acf_field_for_single_product(){
global $post;
if (function_exists('the_field')){
the_field('shop_support', $post->ID);
}
}
Not tested.
For some weird reasons, this approach worked for me (path/to/theme/woocommerce/single-product.php):
<?php if (get_field_objects()):
foreach ( get_field_objects() as $field_id => $field ) :
$value = trim($field['value']);
if (!empty($value)) :
?>
<div class="product_field" id="field-<?php echo $field_id; ?>">
<?php the_field($field_id); ?>
</div>
<?php endif; ?>
<?php endforeach; // end of the loop. ?>
<?php endif; ?>
Also the following one for Category (path/to/theme/woocommerce/archive-product.php) pages:
<?php
$extra_info = get_field_object( 'category_details', get_queried_object() );
if ( !empty($extra_info) ) :
?>
<div class="category_field" id="field-<?php echo $extra_info['key']; ?>">
<?php echo $extra_info['value']; ?>
</div>
<?php endif; ?>
Even at these locations, get_fields() did not work.
Just wanted to show the title of a Wordpress post. I've tried with no success. Pretty new to this.
You can try this, I hope this will help you.
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
and if you want to add custom post type, in that case you need to add WP_Query(); function to get the data like:
<?php
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
for displaying post title in specific pages either you can write:
<?php get_the_title($post_id);?>
into that page's template file or you can make a custom file in templates folder and add this code:
<?php
if (is_page( 'Page Title' ) ):
get_the_title($post_id);
endif;
?>
and include this to all of your pages.. you can check for multiple pages by using OR condition in if statement.
I use wordpress CMS and CMB2 METABOXES and Custom fields. Its works perfect but when I'm making loop If data in metabox not exists , this script anyway rendering empty group field. I want to give the command to script, if it is empty ----> show my custom markup code. I have this
<?php $our_advantages_items = get_post_meta( get_the_id(), 'our_advantages_block_box', true );
if( !empty( $our_advantages_items ) ) {
foreach( $our_advantages_items as $our_advantages_item ) { ?>
<div class="cey96zggvcich3">
<div class="cey96zggvcich3_cvz">
<h2><?php echo $our_advantages_item['our_advantages_block_heading']; ?></h2>
<div class="io2ji39349959jeij">
<?php echo wpautop( $our_advantages_item['our_advantages_block_content'], 1 ); ?>
</div>
</div>
</div>
<?php }
} ?>
Will be happy for any help !
Not sure I understand what you're asking, but just add an else to your if statement
<?php if( !empty( $our_advantages_items ) ) : ?>
//If not empty write your html here
<?php else: ?>
//If empty write your html here
<?php endif; ?>
I'm displaying a list of sibling pages using the below code. As well as retrieving the page title and link. I need to grab a custom field called excerpt and output it for each sibling item. How do I do this using get_pages? Currently the var_dump I'm using is grabbing the current pages excerpt, which is incorrect.
<?php
global $post;
if ( is_page() && $post->post_parent ) {
$children = get_pages('child_of='.$post->ID);
$parent = $post->post_parent;
$our_pages = get_pages('child_of='.$parent.'&exclude='.$post->ID);
$ex = get_field('excerpt', $our_pages);
if (!empty($our_pages)):
foreach ($our_pages as $key => $page_item):
<a href="<?php echo esc_url(get_permalink($page_item->ID)); ?>" title="<?php the_title(); ?>">
<?php echo $page_item->post_title ; ?>
<?php var_dump($ex) ?>
<?php endforeach;
endif;
}
UPDATE
Ok so I have put in <?php print_r($page_item); ?> as suggested and it has returned the following:
I don't see any of my ACF fields here, so how do I retrieve those? What are my next steps?
You should be using the_field(), or echo get_field(), within your forloop, and should pass the post ID as the second parameter of the function:
if (!empty($our_pages)):
foreach ($our_pages as $key => $page_item):
<a href="<?php echo esc_url(get_permalink($page_item->ID)); ?>" title="<?php the_title(); ?>">
<?php echo $page_item->post_title ; ?>
<?php the_field('excerpt', $page_item->ID); ?>
</a>
<?php endforeach;
endif;
You can read more about how get_field() works, in the documentation.
You full code like below:
<?php
global $post;
if ( is_page() && $post->post_parent ) {
$children = get_pages('child_of='.$post->ID);
$parent = $post->post_parent;
$our_pages = get_pages('child_of='.$parent.'&exclude='.$post->ID);
$ex = get_field('excerpt', $our_pages);
if (!empty($our_pages)):
foreach ($our_pages as $key => $page_item):
<a href="<?php echo esc_url(get_permalink($page_item->ID)); ?>" title="<?php the_title(); ?>">
<?php echo $page_item->post_title ; ?>
<?php echo get_post_meta( $page_item->ID, 'excerpt', true); ?>
<?php endforeach;
endif;
}
If you didn't get the data like above then change you custom filed name with any other name and then try.
I've got the same problem with AFC and get_pages().
Try to access ACF fields by retrieving all posts by page type.
$args = array(
'post_type' => 'page',
'numberposts' => -1, // Accepts -1 for all. Default 5.
);
$pages = get_posts($args);
Now ACF get_field('excerpt'); function will work, because get_pages() doesn't return all needed data.
If you'd like to get specific pages like siblings you could pass theirs id's as get_posts() 'include' argument (arr).
Read more about get_posts() in documentation.
Using the Advanced Custom Fields plugin, I am trying to render an image on a single product page in WooCommerce.
I found this snippet of code from someone who is doing the same thing, but is rendering a text field instead. How would I change this to render an image?
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_top_category_desc', 1 );
function woocommerce_template_top_category_desc (){
$terms = get_the_terms( $post->ID, 'wc-attibute-class' );
if ( !empty($terms)) {
$term = array_pop($terms);
$text= get_field('txt-field', $term);
if (!empty($text)) {
echo $text;
}
}
}
So far I have this, but it's not working. The add action is correct because I was using it for something else, but starting at $terms is where I get lost and is obviously not right.
add_action( 'woocommerce_product_thumbnails' , 'add_below_featured_image', 9 );
function add_below_featured_image() {
$terms = get_the_terms( $post->ID, '496' );
if ( !empty($terms)) {
$term = array_pop($terms);
$image = get_field('banner_feedback', $term);
if (!empty($image)) {
echo $image;
}
}
}
Figured it out.
add_action( 'woocommerce_product_thumbnails' , 'add_below_featured_image', 9 );
function add_below_featured_image() {
if( is_single( pageidhere ) ) {
echo '<br><img src="urlhere">';
}
}
Steps to add custom gallery below featured image in woocommerce single product page(you can modify suitably to add just a single image):
Create an ACF for Image Gallery.
Copy the template for product-image.php from /plugins/woocommerce/templates/single-product to themes/your-child-theme/woocommerce/single-product/
Add the following code to product-image.php just before the closing div of
"images"
Add the images you want in the gallery to your single product admin page in the ACF custom field.
`
<?php $images = get_field('product_image_gallery'); ?>
<div class="mycustom_image_gallery">
<?php if($images): ?>
<div class="popup-gallery">
<?php foreach( $images as $image ): ?>
<a href="<?php echo $image['url']; ?>"
class="lightbox-link"
title="<?php echo $image['caption']; ?>"
data-description="<?php echo $image['description']; ?>">
<div class="image-wrap">
<img src="<?php echo $image['sizes']['thumbnail']; ?>">
</div>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
`