ACF fields on WooCommerce Product not displaying - wordpress

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.

Related

Remove Page Header from Blog Posts

I have a page-header that is getting copied over to all of my blog posts that I want to remove completely. I am removing it visually via CSS, but SEO crawlers are still picking it up via the tag.
I am using a standard wordpress them augmented with Elementor. Here is a screenshot of the SEO report.
And here is a screenshot of the actual HTML code
Let me know if any of you have any additional questions! Thank you for your help!
You can make a conditional statement that just outputs the title of the post if on single blog posts, so something like this
<div class="container clr page-header-inner">
<?php
// Return if page header is disabled
if ( oceanwp_has_page_header_heading() ) { ?>
<!-- check to see if single blog posts -->
<?php if (is_single()) : ?>
<h1><?php echo get_the_title(); ?></h1>
<!-- otherwise show the existing title format -->
<?php else: ?>
<<?php echo esc_attr( $heading ); ?> class="page-header-title clr"<?php oceanwp_schema_markup( 'headline' ); ?>><?php echo wp_kses_post( oceanwp_title() ); ?></<?php echo esc_attr( $heading ); ?>>
<?php endif; ?>
<?php get_template_part( 'partials/page-header-subheading' ); ?>
<?php } ?>
<?php if ( function_exists( 'oceanwp_breadcrumb_trail' ) ) {
oceanwp_breadcrumb_trail();
} ?>
</div><!-- .page-header-inner -->
you would have to replace the existing code

ACF Post Object empty return

I have custom post type called "Projects" and inside single-project.php view want to show Posts related to Project.
I have tried to solve that problem by using ACF's Post Object but got nothing from The Loop.
I searched for the solution on Stackoverflow and AFC support page and I could not find a problem.
Maybe this 'Post Object' option is not even created for something like this. I don't know.
Code inside single-project.php
if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post );
?>
<div>
<h3><?php the_title(); ?></h3>
</div>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
ACF configuration
https://i.imgur.com/FNnUbiw.jpg
New post configuration
https://i.imgur.com/IHQqR0P.jpg
Custom post type output
https://i.imgur.com/YFugBZl.jpg
In the place of question marks, I want to show Posts related to that Project.
Since you have allowed multiple values for the field, obtained value will be in array. And since you have set to return value as post object, the value obtained will be the array of objects. After array is obtained, you can loop through those to show related projects.
<div class="related-projects">
<?php
$projects = get_field( 'povezani_projekt' );
global $post;
?>
<?php if ( ! empty( $projects ) ) : ?>
<h3>Related Projects</h3>
<?php foreach ( $projects as $post ) : ?>
<?php setup_postdata( $post ); ?>
<h4><?php the_title(); ?></h4>
<?php wp_reset_postdata(); ?>
<?php endforeach; ?>
<?php endif; ?>
</div><!-- .related-projects -->

How to display page excerpt in Wordpress

I am new in using Wordpress, I created a page that contains a long list of items.
* Item 1
* Item 2
* Item 3
* Item 4 ... and so on
I am planning to embed this page with long list of items on a separate page. How am I going to do it?
I followed tutorials online and got the idea of putting this piece of code add_post_type_support( 'page', 'excerpt' ); on functions.php. After putting the code, an new option will be available when you create/edit pages. But after that, how can I display the my page excerpt?
First to put this code on your theme function.php file.
add_action( 'init', 'my_add_excerpts_to_pages' );
function my_add_excerpts_to_pages() {
add_post_type_support( 'page', 'excerpt' );
}
After that enable excerpt for page see below define image:
Using this code to get page excerpt:
<?php echo get_the_excerpt(); ?>
<?php
query_posts("page_id=36");
while ( have_posts() ) : the_post()
?>
<h1><?php echo get_the_title(); ?></h1>
<?php the_excerpt(); ?>
<?php
endwhile;
wp_reset_query();
?>

How to apply pagination for a custom WordPress query?

<?php $query = new WP_Query('posts_per_page=1&'); ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<?php the_title(); ?><br />
<?php echo paginate_links( $args ) ?>
<?php endwhile;?>
<?php wp_reset_postdata(); ?>
Here is the query I'm using, it should display the first post title then displays the pagination links, however it only shows the post title without the pagination links. What's wrong?
The paginate_links function should go outside the loop and you should use the query variable "paged"
Please see this link http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html for more information on using this.

WordPress search only works with posts, not pages

I'm working on my own custom WordPress theme, using a blank, default template to start. I haven't edited the search.php file.
Many of my WordPress posts are Pages. Unfortunately, the site search only retrieves posts, not pages.
Any idea how to get the theme to search both posts and pages?
Here is the majority of search.php:
<?php if (have_posts()) : ?>
<h3>Search Results</h3>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h4><?php the_title(); ?></h4>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h3>No posts found.</h3>
<?php endif; ?>
Add this code to your functions.php file.
function wpshock_search_filter( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array('post','page') );
}
return $query;
}
add_filter('pre_get_posts','wpshock_search_filter');
http://wpth.net/limit-wordpress-search-results-to-specific-post-types
WP Search http://wpsear.ch/ has that ability.You can adjust what post types you want to show in results page.
In your search.php, find The Loop and insert this code just after it. You can recognize the Loop because it usually starts with:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
Code to be inserted:
if (is_search() && ($post->post_type=='page')) continue;
So, your code should be like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php if (is_search() && ($post->post_type=='page')) continue; ?>
Let me know if it worked.
Lack of page search and search results ranked by post date instead of relevance is a typical WP problem. Try http://wordpress.org/extend/plugins/relevanssi/

Resources