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 |1 Answer
Reset to default 0IMO 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.
Your problem is most likely related to the use of
the_post
andwp_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 whatwp_reset_postdata
does and maybe setup the "original" post data in a different way.
本文标签: pluginsWP getpermalink Return Wrong URL
版权声明:本文标题:plugins - WP get_permalink Return Wrong URL 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737584370a1997632.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp_reset_postdata()
to after theendif
. – Michael Commented May 3, 2017 at 0:27