admin管理员组

文章数量:1173626

I have custom post type named: easytask, and the following code is shortcode from plugin to retrieve data from the custom post above ( easytask ).

function easy_shortcode( $attr ) {

    // Shortcode parameter
    extract( shortcode_atts( array(
        'med' => -1,
        ), $attr ) );

    $easy_id = explode( ",", $med );

    // Get data from custom post type
    $easy_arg = array(
        'post__in' => $easy_id, 
        'post_type' => 'easytask',
    );

    $easy_query = new WP_Query( $easy_arg );

    if ( $easy_query->have_posts() ):

        while ( $easy_query->have_posts() ) : $easy_query->the_post();

            // Content from our custom Post goes here...

        endwhile;
        wp_reset_postdata();

    else:

        // If empty

    endif;

}

add_shortcode( 'easy-task', 'easy_shortcode' );

But when I put the shortcode into the blog list, the theme function that uses get_permalink return the wrong URL, in this case, the URL should be the return to the post URL itself, but my issue is the URL return the custom post URL.

NOTE: All posts in blog list have the correct link except the post that containing the easy-task shortcode.

The following code is the theme function I found in theme functions.php that conflict with the easy-task shortcode.

function adventure_posted_on() {

            printf( __( '<span class="%1$s">Last Updated:</span> %2$s <span class="meta-sep">by</span> %3$s', 'organic-adventure' ),
                'meta-prep meta-prep-author',
                sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
                    esc_url( get_permalink() ),
                    esc_attr( get_the_modified_time() ),
                    get_the_modified_date()
                ),
                sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
                    esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
                    sprintf( esc_attr__( 'View all posts by %s', 'organic-adventure' ), get_the_author() ),
                    get_the_author()
                )
            );
}

Can anyone help me with this?

I have custom post type named: easytask, and the following code is shortcode from plugin to retrieve data from the custom post above ( easytask ).

function easy_shortcode( $attr ) {

    // Shortcode parameter
    extract( shortcode_atts( array(
        'med' => -1,
        ), $attr ) );

    $easy_id = explode( ",", $med );

    // Get data from custom post type
    $easy_arg = array(
        'post__in' => $easy_id, 
        'post_type' => 'easytask',
    );

    $easy_query = new WP_Query( $easy_arg );

    if ( $easy_query->have_posts() ):

        while ( $easy_query->have_posts() ) : $easy_query->the_post();

            // Content from our custom Post goes here...

        endwhile;
        wp_reset_postdata();

    else:

        // If empty

    endif;

}

add_shortcode( 'easy-task', 'easy_shortcode' );

But when I put the shortcode into the blog list, the theme function that uses get_permalink return the wrong URL, in this case, the URL should be the return to the post URL itself, but my issue is the URL return the custom post URL.

NOTE: All posts in blog list have the correct link except the post that containing the easy-task shortcode.

The following code is the theme function I found in theme functions.php that conflict with the easy-task shortcode.

function adventure_posted_on() {

            printf( __( '<span class="%1$s">Last Updated:</span> %2$s <span class="meta-sep">by</span> %3$s', 'organic-adventure' ),
                'meta-prep meta-prep-author',
                sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
                    esc_url( get_permalink() ),
                    esc_attr( get_the_modified_time() ),
                    get_the_modified_date()
                ),
                sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
                    esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
                    sprintf( esc_attr__( 'View all posts by %s', 'organic-adventure' ), get_the_author() ),
                    get_the_author()
                )
            );
}

Can anyone help me with this?

Share Improve this question edited May 2, 2017 at 5:44 Faysal Mahamud 1,0402 gold badges8 silver badges21 bronze badges asked May 2, 2017 at 4:53 Peter ChimpPeter Chimp 13 bronze badges 3
  • please post the full code of the shortcode function. where in that function do you return the output? – Michael Commented May 2, 2017 at 5:46
  • @Michael : Even if I didn't return any data from the custom post still get the wrong permalink in the link that generated from theme function above – Peter Chimp Commented May 2, 2017 at 6:24
  • try and move the wp_reset_postdata() to after the endif. – Michael Commented May 3, 2017 at 0:27
Add a comment  | 

1 Answer 1

Reset to default 0
  1. IMO It is rarely a good idea to evaluate shortcodes outside of the specific post page as too many times you need the context for either the evaluation itself or styling.

  2. Your problem is most likely related to the use of the_post and wp_reset_postdata. What is exactly the post being reset to in that case? A thumb rule, as long as you can avoid it, use explicit post ID parameters in function calls and try to avoid any assumption about which globals are being set to what. Unfortunately, some core api just do not work without the globals being set, and if you need to use them than you need need to find out what wp_reset_postdata does and maybe setup the "original" post data in a different way.

本文标签: pluginsWP getpermalink Return Wrong URL