wordpress.com hosting - Trying to call dynamic content as a shortcode attribute

admin2025-04-19  1

I'm trying to add a dynamic attribute to an existing (3rd party) shortcode. The shortcode appears several times on the same page. When I do this (in my functions.php), I can get the first shortcode to output correctly but the others don't.

function testOne(){
    return do_shortcode('[abc type="contact" orderby="menu_order" order="asc" style="list-post" logic="and" abc_tax_student="student1" abc_tax_teacher="'.basename(get_permalink()).'"]');
}
add_shortcode('shortcode1', 'testOne');

function testTwo(){
    return do_shortcode('[abc type="contact" orderby="menu_order" order="asc" style="list-post" logic="and" abc_tax_student="student2" abc_tax_teacher="'.basename(get_permalink()).'"]');
}
add_shortcode('shortcode2', 'testTwo');

function testThree(){
    return do_shortcode('[abc type="contact" orderby="menu_order" order="asc" style="list-post" logic="and" abc_tax_student="student3" abc_tax_teacher="'.basename(get_permalink()).'"]');
}
add_shortcode('shortcode3', 'testThree');

So as you can see, each shortcode is pulling the slug of the page it resides on (that's what I'm trying to achieve anyway). They're using "and" logic, and in each case, the student is different.

Why is only the first instance working? I wish I knew more about all this stuff!

Thanks in advance folks, Mike

I'm trying to add a dynamic attribute to an existing (3rd party) shortcode. The shortcode appears several times on the same page. When I do this (in my functions.php), I can get the first shortcode to output correctly but the others don't.

function testOne(){
    return do_shortcode('[abc type="contact" orderby="menu_order" order="asc" style="list-post" logic="and" abc_tax_student="student1" abc_tax_teacher="'.basename(get_permalink()).'"]');
}
add_shortcode('shortcode1', 'testOne');

function testTwo(){
    return do_shortcode('[abc type="contact" orderby="menu_order" order="asc" style="list-post" logic="and" abc_tax_student="student2" abc_tax_teacher="'.basename(get_permalink()).'"]');
}
add_shortcode('shortcode2', 'testTwo');

function testThree(){
    return do_shortcode('[abc type="contact" orderby="menu_order" order="asc" style="list-post" logic="and" abc_tax_student="student3" abc_tax_teacher="'.basename(get_permalink()).'"]');
}
add_shortcode('shortcode3', 'testThree');

So as you can see, each shortcode is pulling the slug of the page it resides on (that's what I'm trying to achieve anyway). They're using "and" logic, and in each case, the student is different.

Why is only the first instance working? I wish I knew more about all this stuff!

Thanks in advance folks, Mike

Share Improve this question edited Nov 15, 2019 at 7:12 bueltge 17.1k7 gold badges62 silver badges97 bronze badges asked Jun 4, 2017 at 23:49 macmike78macmike78 12 bronze badges 6
  • Hi, Welcome to WPSE. Why don't you try and call the nested shortcode's function directly inside the first shortcode? – Johansson Commented Jun 4, 2017 at 23:54
  • Thanks Jack! This is where I start to get lost. Can you provide me an example of what you mean? Come to think of it, my subject line is probably misleading since I originally started to try and pull this off with 2 shortcodes (created my own get_the_title and permalink shortcodes and use them inside this one) but couldn't get it to work. Now I'm just trying to do whatever it takes to get it to work! Lol – macmike78 Commented Jun 5, 2017 at 1:15
  • how are you using the shortcodes in your content? what is this 3rd party shortcode, i.e. what is the code of the [abc] shortcode? – Michael Commented Jun 5, 2017 at 1:23
  • @Michael it's Themify's Post Type Builder. I'm pretty sure I've got the right file that handles the shortcode but am not 100% sure what I'm looking for in it. Am I allowed to post all of it in here or is that a no-no? Thanks. – macmike78 Commented Jun 5, 2017 at 1:39
  • 1 Gotcha @jackjohansson! Will try that today and keep you posted. Thnx – macmike78 Commented Jun 5, 2017 at 9:36
 |  Show 1 more comment

3 Answers 3

Reset to default 2

I think you are having syntax errors in the shortcode like missing single quotes.Please check the code properly.

I think you're just missing some single quotes to escape out to php:

function testOne(){
    return do_shortcode('[abc abc_tax_teacher="' . basename(get_permalink()) . '"]');
}

otherwise the value the abc shortcode function sees is literally .basename(get_permalink()).

get_permalink() relies on the global $post to get the current post ID. If this doesn't output the expected permalink, it's likely because $post has been polluted by some other query. In that case you can try get_permalink( get_queried_object_id() ) to explicitly supply the main query's post ID.

get_permalink() just wasn't cutting it. Here's what I ended up doing in case anyone tries to do the same thing someday...

// Thank you Stack Exchange, Themify & Tuts+ (a little piece of each of you is in here). function managercontacts_post_lists_sc( $atts, $content = null, $tag ) { $current_page = sanitize_post( $GLOBALS['wp_the_query']->get_queried_object() ); $slug = $current_page->post_name; extract( shortcode_atts( array( 'type' => 'contact', 'orderby' => 'menu_order', 'order' => 'asc', 'style' => 'list-post', 'logic' => 'and' ), $atts ) ); $args = ''; switch( $tag ) { case "bearings": $args = 'ptb_tax_vertical="bearings-sealing-products-power-transmission"'; break; case "conveyor_belt": $args = 'ptb_tax_vertical="conveyor-belt-hose-rigging-hydraulics-service"'; break; case "electrical": $args = 'ptb_tax_vertical="electrical-products"'; break; case "engineered_products": $args = 'ptb_tax_vertical="engineered-products-services"'; break; case "not_specified": $args = 'ptb_tax_vertical="not-specified"'; break; case "specialty_valves": $args = 'ptb_tax_vertical="specialty-valves-actuators-instrumentation-service"'; break; case "welding": $args = 'ptb_tax_vertical="welding-products-services"'; break; } return do_shortcode('[ptb type="'.$type.'" orderby="'.$orderby.'" order="'.$order.'" style="'.$style.'" logic="'.$logic.'" '.$args.' ptb_tax_manager="'.$slug.'"]'); wp_reset_query(); } add_shortcode( 'bearings', 'managercontacts_post_lists_sc' ); add_shortcode( 'conveyor_belt', 'managercontacts_post_lists_sc' ); add_shortcode( 'electrical', 'managercontacts_post_lists_sc' ); add_shortcode( 'engineered_products', 'managercontacts_post_lists_sc' ); add_shortcode( 'not_specified', 'managercontacts_lists_sc' ); add_shortcode( 'specialty_valves', 'managercontacts_post_lists_sc' ); add_shortcode( 'welding', 'managercontacts_post_lists_sc' );

Thanks everyone who chimed in!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745002479a279313.html

最新回复(0)