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 -->
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.
I have a number of flexible content fields set up.
One of these is called 'product-picker' and another is called 'product-information'.
The product picker uses the post object functionality to pull in selected products.
I can pull in the default wordpress parts such as:
<?php the_post_thumbnail(); ?>
But I can't pull in the values within 'product-information'.
Is this actually possible? Extracts of code below:
<?php if( have_rows('content_blocks') ):
while ( have_rows('content_blocks') ) : the_row();
if( get_row_layout() == 'product_information' ):
include("inc/product-information.php");
elseif( get_row_layout() == 'products_picker' ):
include("inc/products-picker.php");
endif;
endwhile;
endif; ?>
Then within 'products-picker' I am using the following code:
<?php foreach( $post_objects as $post): ?>
<?php setup_postdata($post); ?>
<div class="product">
<?php the_post_thumbnail(); ?>
<div class="description">
/* Echoing post ID returns correct value */
<h1><?php echo $post->ID; ?></h1>
/* Does not work */
<h1><?php the_sub_field('heading', $post->ID); ?></h1>
/* Does not work */
<?php the_sub_field('description', $post->ID); ?>
</div> <!-- .description -->
</div> <!-- .product-->
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
I might have the structure of my content wrong.
Maybe you can't call flexible content within flexible content?
But I thought it should be possible to do this in light of the fact that the default fields such as:
the_post_thumbnail();
and
the_title();
work.
All your time and help is greatly appreciated.
You have to set up the flexible rows part for each of your post objects. So after you setup your post data, replicate your
if( have_rows('content_blocks') ):
while ( have_rows('content_blocks') ) : the_row();
with the product information fields for each post object.
Check out https://www.advancedcustomfields.com/resources/flexible-content/ and the nested loop example. I think that should get you a little closer.
Okay, I have the code pulling the custom fields URL and title of the url. Now I can't seem to get it to show the second featured blog. Here is the working code.
<?php $related = get_post_meta($post->ID, "Featured-Blog", $single=true);
$related=explode(',',$related);
$args = array_merge( array('post__in' => $related, $wp_query->query ) );
query_posts($args);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="<?php the_ID(); ?>">
<p class="caption"><?php the_title(); ?></p>
</div>
<?php endwhile; else: ?>
<p>no related</p>
<?php endif; wp_reset_query();?>
This code example here produces two results, which is almost what I want. Which is caused by the foreach I believe. I do not want to use the code below, but I need to find a way to add the foreach I think to get it to list all of the featured-blogs if I have more than one.
<?php
$custom_fields = get_post_custom($post_id); //Current post id
$my_custom_field = $custom_fields['Featured-Blog']; //key name
foreach ( $my_custom_field as $key => $url )
echo $key ="<a href='".$url."'>TEST</a><br /><br /><br/>";
?>
Here is a screenshot showing my Custom Fields if it helps at all, and the results they are showing on the site. screenshot
Example 1: You are using the actual word 'TITLE' for the link use <?php the_title() ?> instead
Example 2: You are not building link at all. Your href attribute is empty. Cut echo $featured_blog1 and paste it to href attribute to end up like so:
Example 3: Same as 2
Also you can delete dose instructions or put them inside <?php ?> code so they are not visible to viewers.
Hope this helps.
If you need more info just ask. ;)
<?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.
I have a custom post type with multiple taxonomy types. The issue is focused around just one of them.
I need to display all custom posts that have a taxonomy from featured-vendors checked. Right now, there is just one "featured" , but there may be more in the future, such as "highlight" or "sponsor" or something alone those lines.
But, for now, I just need to go through all the "vendors" custom post type, find ones with "featured" checked inside of the "featured-vendors" taxonomy.
I have some some posts out there that state it's not possible, but they were either from 2.8 or from the very first of this year and I know WordPress has released at least one update since then.
Thanks in advance!!
Query Custom Post Types by Taxonomy Term
This sample will assume:
the custom post type was registered with the taxonomy and the taxonomy was registerd with 'query_var' =>true and 'hierarchial' => true
The "checked" term will be the parent and any new ones can be added later on as children.
The Code:
<?php query_posts( array( 'featured-vendors' => 'checked' ) ); ?>
<?php if( is_tax() ) {
global $wp_query;
$term = $wp_query->get_queried_object();
$title = $term->name;
} ?>
<div class="my-custom-post">
<h3><?php echo($title); ?></h3> //this will show the name of the post type
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="entry"> <h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?> //shows the excerpt
</div><!--/end entry-->
</div><!--/end post-->
<?php endwhile; else: ?>
<p><?php _e('No Post Found','your_theme_text_domain'); ?></p>
<?php endif; ?>