Custom fields value in Woocommerce product description - php

I've been looking through answers on how to get this work but can't find a solution that works.
I have organised my plugins so that each upload automatically generates a custom field value depending on the uploaded information.
Screenshot of custom fields
I want it to be so that when I type the field name, i.e "track_bpm" into the description or short description of a product in Woocommerce that it then shows the value. That way I can automate the creation of product pages. Currently, it doesn't change.
I've added:
<?php echo get_post_meta($post->ID, 'key', true); ?>
To:
<?php while ( have_posts() ) : the_post(); ?>
<?php
if ( flatsome_product_block( get_the_ID() ) ) {
wc_get_template_part( 'content', 'single-product-custom' );
} else {
wc_get_template_part( 'content', 'single-product' );
}
?>
<?php echo get_post_meta($post->ID, 'key', true); ?>
<?php endwhile; // end of the loop. ?>
To the single-product.php file in the loop with the 'key' value replaced with each custom field name, but this just adds the value to the top or bottom of the page depending on the location in the loop.
Any advice here would be really appreciated!

Related

Can't show get_term output on the frontend

So i try to show a custom taxonomy on the frontend.
The custom taxonomy is from a plugin i use and is called wcs-instructor.
I just want to show a Single Page with the content of the taxonomy by listing the names and images of the Instructors, the image is a custom field.
When i try to Query the taxonomy with get_term or with WP_Term_Query everything shows up nicely as long as im logged in with my Admin Account (havent actually tested another account).
When i log out and open the Page again its blank.
The structur is as follows.
Custom Taxonomy: wcs-instructor
Page Endpoint: /trainer
Custom Single page: page-trainer.php
get_header();
?>
<main id="primary" class="site-main">
<?php
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content', 'trainer' );
endwhile;
?>
</main><!-- #main -->
<?php
get_footer();
content-trainer.php (striped some html thats unnecessary)
<div class="content">
<?php
$term_query = new WP_Term_Query(
array( 'taxonomy' => 'wcs-instructor',) );
if ( ! empty( $term_query->terms ) ) {
foreach ( $term_query ->terms as $term ) {
echo $term->name;
}}
else {
echo 'No term found.';
}
?>
</div>
I know that the code above only gives me the results that are not empty, but the taxonomy got entries that aren't.
Also the Code below also doesn't work.
<?php
$terms = get_terms( array(
'taxonomy' => 'wcs-instructor',
'hide_empty' => false,
) );
foreach ( $terms as $term )
{
echo $term->term_id;
echo '<br>';
echo $term->name;
}
?>
And i just cant find the reason why because when i go to /wcs-instructor/testinstructor i can view the content even when im not logged in.
Any hints on how to debug this behavior or why it's behaving like this in the first place?
Sounds like that taxonomy might have the publicly_queryable arg set to false. Not sure which plugin you are using to manage your custom taxonomies but the CPT UI Plugin has a setting for that argument.
The Public Queryable setting for taxonomies in CPT UI plugin

How to add a custom block as a usual "product" to shop page / archive-product.php

I'm looking for a possibility to add a custom html block/post to the woocommerce "shop page" inside the products grid, as a product.
What I mean.. I have a grid of products on the "shop" page (archive-product) and I want to create a special post/page/html block with some text information, that will be inserted into the products grid as a one of "product", but with no price, with no title and unclickable. I've attached the screenshot of the final result I want to have, it's really self explaining - here it is exactly what I'm looking for.
As an idea probably I can create a special product with specific slug or title and the corresponding script with pre_get_posts hook will find this post/product and modify it to look like I need. I'm looking for some code/ideas how to insert this specific block/page/post into the archive-product page on some position in the grid. Thanks!
Thanks for help, guys! I've implemented the functionality I was looking for. I've found the corresponding loop in archive-product.php and as was suggested by JapanGuy, I've added a simple "if i equal let's say 5 then echo < li>[Custom block]< /li>" .
The original snippet from archive-product.php:
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
Modified code with inserted custom block:
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
if ($i == 5) {
echo "<li>[Custom block]</li>";
}
$i++;
?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
Im such simple way I can add any content to the created [Custom block] and have an usual products grid with extra custom designed block. I'm not very experienced programmer, so probably my code is not perfect, but it works. Thanks!
Edit: Previous code was wrong, changed it here
$i=0;
while ($row = mysqli_fetch_array($query))
{
if ($i == 2) {
echo "Cusom block";
}
echo "<p> Product block " . $row['column'] . " </p>";
$i++;
}
Creating WordPress Custom Post Archives : Hope this meets your requirement.
Custom Post Archives list your custom content. You probably already know the standard WordPress archives. So you can follow this to display both together .
Ref here : https://wp-types.com/documentation/user-guides/creating-wordpress-custom-post-archives/

Wordpress bringing in template_part based on custom field

I have a Wordpress loop on my index.php which brings in a specific template-part.
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part( 'content' );
?>
<?php endwhile; ?>
<div class="clearfix"></div>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
content.php has an article with the blog post information (title, featured image, etc) with specific html.
I have a custom-field, ExtraCSS, applied to each post through the content.php with values 'post-right' or 'post-left' (so I can change css of individual posts).
content.php code
<?php $extraCSS = get_post_meta(get_the_ID(),'ExtraCSS',true);
<article class="<?php echo $ExtraCSS?>">
...
I want to have 2 different template_parts which can get called with the loop depending on the custom-field or value applied to the post.
EX. so if the post has the ExtraCSS custom-field value 'post-right' applied to it, the loop will bring in
get_template_part( 'content' , 'right' );
if the post has the ExtraCSS custom-field value 'post-left' it will bring in
get_template_part( 'content' , 'left' );
This may not be the right way to do it, and I'm open to other suggestions, but thats the overall idea. Wanting two different post-templates for one loop There will be numerous posts that have either one, and I want them all brought in one after the other.
As you said it's either one or the other template:
<?php
$extraCSS = get_post_meta( get_the_ID(), 'ExtraCSS', true );
if ( $extraCSS == 'post-right' ) {
get_template_part( 'content' , 'right' );
}
else {
get_template_part( 'content' , 'left' );
}
?>

PHP/WordPress: Custom Field Value in Loop returns Array instead of Single Result

For the search results page in WordPress Im making a custom template. And in that custom template I want to display custom meta field values.
However, when I do this:
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
$pName = get_post_meta($post->ID, $productName, true);
echo $pName;
);
?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif;
The result I get is not the value of the custom field but:
Array
When I var_dump the $pName it shows all the right custom field content.
Question: Why am I getting an array as the result (when Ive told it its a single result with 'true') and how do I fix it so it displays the proper content?
Thanks!

Wordpress - Displaying content from custom posts

Im using advanced custom fields and have setup a custom post type for testimonials, on the testimonial page there is a relationship field (display_on_page) where you would select which page to display the testimonial on.
The issue is that the testimonial displays on everypage when a page is chosen
Does anyone know what I am doing wrong on the below please?
<?php query_posts( 'post_type=Testimonial'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $posts = get_field('display_on_page');
if( $posts ): ?>
<?php the_field('the_testimonial'); ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
If I understood correctly, you should check if the value that you get from get_field('display_on_page') matches the current page ID.
(I'm assuming that the page ID is what is stored by the custom field you created).
If that's the case, you may query your posts filtering by custom field values, like such:
<?php
// Fetches current page ID, assuming that this is page.php or similar
$current_page_id = get_the_ID();
query_posts(
'post_type=Testimonial',
'meta_key' => 'display_on_page',
'meta_value' => $current_page_id // Only gets Testimonials that are set for this page
);
while ( have_posts() ) : the_post();
the_field('the_testimonial');
endwhile;
// Resets default WP Post Data *AFTER* the loop is complete
wp_reset_postdata();
?>

Categories