ACF // Cannot display sub_fields from a repeater - advanced-custom-fields

I created a custom taxonomy "collections". Collections items are available on the page via a relationship field.
I can access the collections and display the collections title. I'm also trying to display the data from a repeater but cannot find a way to access and display the repeater sub_fields.
When I do a var_dump($piecesList), I have an array showing the datas. How can I display these datas???
Here's my code:
<?php
$collections = get_field('collectionSelector'); /* relationship field */
if( $collections ):
foreach( $collections as $collections_post ):
$piecesList = get_field( 'pieces', $collections_post->ID );
if( $piecesList ):
foreach( $piecesList as $piecesList_post ):
$pieceName = get_field( 'piece_name', $piecesList_post->ID );
echo $pieceName;
endforeach;
endif;
endforeach;
endif;
?>

Related

How to put all product attributes below description without shortcode Woocommerce

currently I've used this code and put shortcode and id in every product post.
add_shortcode("product_attributes", "display_product_attributes");
function display_product_attributes( $atts ) {
// Shortcode attribute (or argument)
$atts = shortcode_atts(
array(
'id' => ''
),
$atts, 'product_attributes'
);
if( ! ( isset($atts['id']) && $atts['id'] > 0 ) ) return;
$product = wc_get_product($atts['id']);
ob_start();
do_action( 'woocommerce_product_additional_information', $product );
return ob_get_clean();
}
Can someone share new code that I don't need to use shortcode and it's will automaticly show additional product information in every below product description post?
Example like picture below
combine description and additional information

How can I echo out an Item's price in Wordpress/ Woocmerce?

lets say I am in the Page section where you put the content in WYSIWYG, my plan is to display one woocomerce item's price using php to pull out the info. I downloaded this Plugin Insert PHP where it allows you to put php code in the WYSIWYG section using a special shortcode. The thing is that I am not an expert on php whatsoever, so how would I even start?
so far I've researched this:
<?php echo $product->get_price_html(); ?>
lets say its the item with an SKU: 6-501
how would you do an php to pull the out the price ONLY of that item?
Use the get_price() method instead:
<?php echo $product->get_price(); ?>
WooCommerce version 2.3 and up has a function wc_get_product_id_by_sku( $sku ).
so you could do something like,
$sku = '6-501';
$_product_id = wc_get_product_id_by_sku( $sku );
$_product = new WC_Product( $_product_id );
echo $_product->get_price_html();
Assuming that you are using Insert PHP plugin, Place the following code between [insert_php] and [/insert_php]:
$sku = '6-501';
global $wpdb;
$id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
if ( $id ){
$product = new WC_Product( $id );
echo $product->get_price();
}
That should work.

How to display a div in a post in your category page - wordpress

I have a website where each post is displayed and each post has a description explaining the post.
How can I get my category page (the page that summarises all posts) to show only the small summary of the post and nothing else. At the moment I am using
the_excerpt();
but this limits the text to 55 words, however some are smaller than that, and so other text is displayed in the category that I don’t want.
For example, this is an example post:
http://thestudentbubble.com/hidden-gems/hidden-gems-restaurants/ganges-indian/
As you can see below the description is additional information which I don't want it to appear on the category page.
this is the category page - http://thestudentbubble.com/hidden-gems/hidden-gems-restaurants/
yet the additional information that I don't want to appear still appears.
The text I want to appear is in:
<div class = "bioText">
How can I make only the text in the div appear in the category page?
Thank you for the help!
this is the category loop :
<div class="comment">
<?php
$cats = get_the_category();
$category_id = $cats[0]->cat_ID;
$args = array( 'posts_per_page' => 1, 'category' => $category_id);
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) :
$this_post_ID = get_the_ID();
$this_post_preview = get_post_meta( $this_post_ID, 'post_preview', true );
echo $this_post_preview;
//the_excerpt();
//the_content();
wp_reset_postdata();
endforeach;
?>
</div>
Ok then, the first thing to do is to create a custom textarea in each post's admin panel. It will appear under the editor. Let's create the .php file that will contain our code, call it post_preview_field.php, and place it in the theme root folder. After doing that, we will need to reference it in our functions.php file. This is done by simply adding the following line:
require_once(get_template_directory().'/post_preview_field.php');
Let's open our post_preview_field.php file and write the code.
<?php
add_action( 'add_meta_boxes', 'post_preview_metabox' );
add_action( 'save_post', 'post_preview_save_postdata' );
function post_preview_metabox() {
$screens = array( 'post' );
foreach ($screens as $screen) {
add_meta_box(
'post_preview_metabox_id',
__( 'Post Preview', 'post_preview_textdomain'),
'post_preview_inner_boxes',
$screen
);
}
}
function post_preview_inner_boxes( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'post_preview_noncename' );
$post_preview_value = get_post_meta( $post->ID, 'post_preview', true );
echo '<label for="post_preview_new_field">';
_e("", 'post_preview_textdomain' );
?>
</label>
<textarea name="post_preview_new_field" id="post_preview_new_field" ><?php echo $post_preview_value; ?></textarea>
<?php }
function post_preview_save_postdata( $post_id ) {
if ( 'post' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) )
return;
} else {}
if ( ! isset( $_POST['post_preview_noncename'] ) || ! wp_verify_nonce( $_POST['post_preview_noncename'], plugin_basename( __FILE__ ) ) )
return;
$post_ID = $_POST['post_ID'];
$post_preview = ( $_POST['post_preview_new_field'] );
add_post_meta($post_ID, 'post_preview', $post_preview, true) or
update_post_meta($post_ID, 'post_preview', $post_preview);
}
?>
Here we go. We just created a textarea field that appears in every post's panel, can contain some text, is saved every time we save\update the post and is stored in the post's metadata.
Now, all we have to do is access this value in the frontend and echo it. How do we do that?
In the loop, we first assign the post ID to a variable:
$this_post_ID = get_the_ID();
and then assign the data we stored to another:
$this_post_preview = get_post_meta( $this_post_ID, 'post_preview', true );
That's it. We can now echo the preview:
echo $this_post_preview;
I managed to solve it by adding
<!--more-->
after each of the descriptions.

Two Queries with WP_Query append second one to end with Pagination

What I am trying to do is create two queries for properties. One will retrieve the regular results based on a normal query. The second query will retrieve properties closely related to the first query. I am able to run both queries and retrieve all results with posts_per_page set to unlimited and no pagination. The problem when adding pagination is that both loops run and display posts on each page.
The page would have 3 from the first loop followed by 3 from the second loop.
I have tried to merge the two queries into one and show them but the same results happen. 3 and 3.
I am thinking that I need to append somehow to be sure that the second loop is getting output after the first. Any thoughts?
Here are my loops(I excluded args because of length)
<?php
$queryOne = new WP_Query($args);
$queryTwo = new WP_Query($args2);
$results = new WP_Query();
$results->posts = array_merge($queryOne->posts, $queryTwo->posts);
?>
<?php foreach($results->posts as $post) : ?>
<?php setup_postdata( $post ); ?>
<?php get_template_part( 'property-listing' ); ?>
<?php endforeach; ?>
as parse_query relies on post_count you'll have to add the two post_counts. In your example post_count is not set. It should work if you populate the post_count. Simply add this at the end:
$results->post_count = $queryOne->post_count + $queryTwo->post_count;
Your complete example:
<?php
$queryOne = new WP_Query($args);
$queryTwo = new WP_Query($args2);
$results = new WP_Query();
$results->posts = array_merge($queryOne->posts, $queryTwo->posts);
$results->post_count = $queryOne->post_count + $queryTwo->post_count;
foreach($results->posts as $post) :
setup_postdata( $post );
get_template_part( 'property-listing' );
endforeach;
?>

WordPress custom taxonomy parent-child navigation menu

I'm trying to create the following menu structure from custom taxonomies:
CAT 1
-SubCat1
-SubCat2
CAT 2
CAT 3
CAT 4
What I want to achieve is when I click on a child category (SubCat1 for example) My current navigation structure should stay the same, and bold the current subcat.
When I click on another PARENT category, it's subcategories should appear and the the rest of the parent cats only (not all the cats with subcats).
My problems are the following:
I managed the create the child navigation menu when clicking on a parent category, but it only shows the CURRENT and the CHILD LEVEL categories in the menu, without the other main categories using this code:
<?php
$taxonomy = $tax;
$orderby = 'name';
$show_count = 1; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
if (get_term_children($term->term_id, $tax) != null) {
$child = $term->term_id;
} else {
$child = '';
}
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'child_of' => $child,
'current_category' => 0
);
?>
<? if (get_term_children($term->term_id, $tax) != null) { ?>
<h3><?php echo $term->name; ?> Templates</h3>
<? } ?>
<?php
wp_list_categories( $args ); ?>
The problem is with the above code that when I click on a child category all my parent/subcategories are displayed again.
I want to be able to stay on the same structure when browsing any of the subcategories from one big category with the addition of the bold font face to the subcategory I'm browsing.
If this makes sense to someone please help.
Thanks,
What I would do is create a custom query to loop through the taxonomy with a parent of 0 then in the loop of displaying them do the get_term_children function. I believe that is the best way to create something like this. This is what I have done in my plugin and it has allowed me to have much more customisation.