multipe targets per post
-
i’m using a repeater field for multiple recipes in a post on this food blog – see example here: http://veggiemagnifique.com/magnifique-lebanese-platter/
the issue is that the print-me button targets the id #recipe as follows: [print-me target=”#recipe” title=”| Printer-friendly version”] – this is fine when only one recipe is present but when several are present the first is always targeted regardless of which print-me button you hit.
i’ve been trying to organise a way on incorporating some kind of nth-child style condition to make each button target the relevant recipe but so far no luck. any ideas, anyone?
thanks.
-
it can be done using the %ID% Placeholder
you will need to adjust the name from simply ‘recipe’ to a name that includes the unique post ID of the recipe.
thanks baden,
i’m having a good look at this but the issue is that i’m trying to use several print-o-matic buttons within a single post, so presumably the post ID won’t be different for each row.
forgive me if i’ve just failed to understand something here – anything else you can tell me about how to do this please?
how are your #recipe elements being created? Manually, or via a shortcode or theme function or template loop? If you would like us to help you suss this issue out, please consider upgrading to print-pro-matic as it comes with a higher level of support that can be offered here. Regardless, we’ll do what we can to help you get this resolved
that’s very much appreciated, thanks.
the setup is as follows: within the post there is a custom repeater field which allows for the inclusion of one or more recipes in the post. within the repeater field is a field for the print-me shortcode.
the php call in the theme assigns the id #recipe to the content of that field but i guess what i actually need to do is find some way to assign a different id to each entry in the loop or somehow use an nth-child like rule so as to only target the instance of the repeater which contains the print button.
does that make sense? here’s the code:
<?php if( have_rows('recipe') ): ?> <?php while ( have_rows('recipe') ) : the_row(); ?> <div id="recipe"> <h4><?php the_sub_field('title'); ?></h4> <?php if(get_sub_field('intro_text') != "") { ?> <div class="intro-text"> <p><?php the_sub_field('intro_text'); ?></p> </div> <?php } else { ;?><?php } ;?> <?php if( have_rows('info_tabs') ): ?> <div class="recipe-tabs"> <?php while ( have_rows('info_tabs') ) : the_row(); ?> <?php if(get_sub_field('prep_time') != "") { ?> <div class="info-tab"> <h6>Prep time</h6> <p><?php the_sub_field('prep_time'); ?></p> </div> <?php } else { ;?><?php } ;?> <?php if(get_sub_field('cook_time') != "") { ?> <div class="info-tab"> <h6>Cook time</h6> <p><?php the_sub_field('cook_time'); ?></p> </div> <?php } else { ;?><?php } ;?> <?php if(get_sub_field('total_time') != "") { ?> <div class="info-tab"> <h6>Total time</h6> <p><?php the_sub_field('total_time'); ?></p> </div> <?php } else { ;?><?php } ;?> <?php endwhile; ?> </div> <?php else : endif; ?> <div class="ingredients"> <h5>Ingredients</h5> <?php the_sub_field('ingredients'); ?> </div> <div class="instructions"> <h5>Instructions</h5> <?php the_sub_field('instructions'); ?> </div> <?php if(get_sub_field('notes') != "") { ?> <div class="notes"> <h5>Notes & Tips</h5> <?php the_sub_field('notes'); ?> </div> <?php } else { ;?><?php } ;?> </div> <div class="print"> <?php the_sub_field('print_button'); ?> </div> <?php endwhile; ?> <?php else : endif; ?>thank you so much.
this should be quite painless:
Step 1
Change this:<?php while ( have_rows('recipe') ) : the_row(); ?> <div id="recipe">To this:
<?php while ( have_rows('recipe') ) : the_row(); ?> <div id="recipe_<?php the_ID(); ?>">Fun fact about the ID attribute:
The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document).
http://www.w3schools.com/tags/att_global_id.asp
Finally
Where exactly are you placing the print-me shortcode?
Regardless, you will need to change:[print-me target="#recipe"/]to:
[print-me target="#recipe_%ID%"/]Let us know if we can be of further assistance or if you get this resolved.
thanks baden,
i’ve done what you suggest in the code and changed the shortcodes at this post: http://veggiemagnifique.com/magnifique-lebanese-platter/ – the print buttons are now rendering empty pages.
i haven’t understood where the separate IDs are generated though. these recipes are not separate posts but repeater fields within one post. the shortcode is being placed in a WYSIWYG editor within the repeater field itself, as per the code i sent above:
<div class="print"> <?php the_sub_field('print_button'); ?> </div>the sub field “print_button” is where code is placed by site users. adding
target="#recipe_%ID%"/to what is there already gives me
[print-me target="#recipe_%ID%"/ title="| Printer-friendly version"]but as you’ll see when you hit the print buttons at the above URL they’re not then giving us the recipes. do i need somehow to generate an ID for each row in the repeater?
(and yeah – i should have used classes!)
thanks again.
p.s. i also removed #recipe from the default target field in the print-o-matic global settings, to ensure that wasn’t over-ruling the targeting within the shortcode.
AH OK! so you are correct… that idea will not work, as each recipe is now using the post ID of the page, not the ID of the repeater field.
So I think you are looking at a much more advanced use case. You first need to find out if there is any kind of unique ID for each of the repeater fields. Normally these are saved as meta-value array, so you might be able to access the array key position for each item.
Regardless, you might want to consider upgrading Print-Pro-Matic as it comes with a much higher level of personal support than we can offer you here.
By the way, is this repeater field part of your theme or a plugin? We’ll try and look into another solution. Fret not, we won’t leave you hanging on this.
i do appreciate this baden – i’ve also suggested the upgrade to my client so if they agree to that we’ll flip them over.
the repeater field has been created with the advanced custom fields plugin. perhaps there’d be some indication of IDs created for repeaters in their documentation, or even a little firebugging might give me some more info. i’ll check these two options to see if i can identify an ID system there.
any ideas of course please keep me posted. thanks for putting in the time.
The topic ‘multipe targets per post’ is closed to new replies.