Trying to call dynamic content as a shortcode attribute

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

Topic shortcode wordpress.com-hosting Wordpress

Category Web


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


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!


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.