Community Forums › Forums › Archived Forums › Design Tips and Tricks › Shortlink at the bottom of content on a tag post
Tagged: wp_get_shortlink
- This topic has 16 replies, 3 voices, and was last updated 5 years, 8 months ago by bluenavy.
-
AuthorPosts
-
January 30, 2019 at 12:29 am #489130bluenavyParticipant
Is it possible to add a shortlink to the bottom of content on a tag post based on its permalink.
I have a plugin that adds a link onto the bottom of content on a post page based on its permalink, but for the life of me, I have no idea what to tweak to make it work on a tag post.
Is it possible to do this on a tag post ?
The plugin i am using has the following code :
<?php /* Plugin Name: Add Shortlink to Posts Description: Adds a link to the shortlink for each post below the content. /* * Localise the plugin */ load_plugin_textdomain('astp'); /* * Adds the shortlink on the end of posts */ function astp_add_shortlink($content){ // Don't add shortlink to excerpts if ( ! is_single() ) return $content; // Generate the shortlink $shortlink = wp_get_shortlink(); $shortlink = ( ! empty( $shortlink ) ? $shortlink : get_permalink() ); // Generate the html $html = sprintf( '<p class="the_shortlink">%s %s</p>', __(" ", 'astp'), $shortlink, get_the_title(), $shortlink ); // Add to the content return $content . $html; } add_filter( "the_content", "astp_add_shortlink", 11 ); ?>
January 30, 2019 at 12:47 am #489131Victor FontModeratorI have no idea what you mean when you say tag post. A post is a just a post regardless if it has tags or not.
Maybe understanding how wp_get_shortlink() works will help you: https://codex.wordpress.org/Function_Reference/wp_get_shortlink
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?January 30, 2019 at 12:47 am #489132Brad DaltonParticipantJanuary 30, 2019 at 1:26 am #489133bluenavyParticipantits local dev site - its just a normal tag post.
January 30, 2019 at 1:38 am #489135bluenavyParticipant@VictorFont
Hi Victor - I have a post and have added 5 tags to the post.
This has generated 5 tag posts that have their own individual permalinks.
I have used the case i posted to get wordpress to put a shortlink under the content but it has just worked on the 1 post and not for the tag posts.
I presume the code to get it to work on the post is not directed at tag posts and I just wanted to know what I would target to get a shorter to be outputted to all the tag posts ?
The code currently reads
add_filter( "the_content", "astp_add_shortlink", 11 );
would the below suffice ?
add_filter( "genesis_after_post_content", "astp_add_shortlink", 11 );
or is their a filter that would enable me to enable a shortlink to be displayed at the bottom of all tags, posts or pages or any other post taxonomy ?
Thanks
January 30, 2019 at 1:41 am #489137bluenavyParticipant@VictorFont
Hi Victor - I have created a post and have added 5 tags to the post.
This has generated 5 tag posts that have their own individual permalinks.
I have used the case i posted to get wordpress to put a shortlink under the content but it has just worked on the 1 post and not for the tag posts.
I presume the code to get it to work on the post is not directed at tag posts and I just wanted to know what I would target to get a shorter to be outputted to all the tag posts ?
The code currently reads
add_filter( "the_content", "astp_add_shortlink", 11 );
would the below suffice ?
add_filter( "genesis_after_post_content", "astp_add_shortlink", 11 );
or is their a filter that would enable me to enable a shortlink to be displayed at the bottom of all tags, posts or pages or any other post taxonomy ?
Thanks
January 30, 2019 at 1:49 am #489138Brad DaltonParticipantJanuary 30, 2019 at 1:51 am #489139bluenavyParticipantyes sorry for my bad terminology ::embarrassed::
Ideally i would like to add something to that code so it all outputs the same
Sorry for the confusion
January 30, 2019 at 4:34 am #489140bluenavyParticipantThat works a treat just two little niggles which may be an easy one for you to assist.
I am using the below code from your site - https://wpsites.net/web-design/add-shortlink-anywhere-in-genesis/
Great resource and bookmarked !
add_action( 'genesis_after_post_content', 'add_wp_get_shortlink' ); function add_wp_get_shortlink() { printf( '<a href="%s">' . __( 'Your link text here' ) . '</a>', __( wp_get_shortlink() ) ); }
It outputs the shortlink at the bottom of the page as needed ( Thankyou !! )
Is their a way I can tweak the ( 'Your link text here' ) code to change the outputted text to the shortlink ie https://myshortlinkonthesceenatthebottom.com/wootwoot
Secondly the shortlink is being generated to the post permalink as opposed to the permalink of the actual tag archive page.
Is their a way to tweak that so the shortlink generated is strictly based on the tag archive page permalink ?
If it helps my permalink structure is based on /%category%/%postname%/ and my tag base is 'aortic aneurysms'
Thankyou so much in advance and I apologise for my incorrect terminology or if I am not as fluent in the code language.
Thanks
January 30, 2019 at 11:23 pm #489163Brad DaltonParticipantUse a different function to get the tag archive permalink and different hook.
You can modify the default text, simple as that.
January 30, 2019 at 11:42 pm #489166bluenavyParticipantThanks for the response.
What I meant was that I was trying to make the shortlink just replicate the shortlink url, the issue i am facing is that if I have 2, 3, 5, 11, 20 or 30 pages, if i just put a generic text in ( 'Your link text here' ) then it will be the same for all pages and I wanted the individual shortlink to be replicated in text.
With regards to using a different function, for the lack of trying I did try to glean some info online but was a tad overwhelmed with the jargon.
Do you have nay ideas on a simple way to get the tag archive permalink ?
The hook ( I hope that is the correct phrase ) is correct and the shortlink displays where it should be.
Is this the bit which defines the post type
if ( is_tag() ) {
I could live with just putting a generic ( 'Aortic Deviation' ) in the that field and its just the shortlink being based on the tag archive permalink that is really throwing me ;(
January 31, 2019 at 12:25 am #489168Brad DaltonParticipantJanuary 31, 2019 at 12:49 am #489170bluenavyParticipantThanks for the link, but have absolutely no idea what to do with that LOL
function get_term_link( $term, $taxonomy = '' ) { global $wp_rewrite; if ( !is_object($term) ) { if ( is_int( $term ) ) { $term = get_term( $term, $taxonomy ); } else { $term = get_term_by( 'slug', $term, $taxonomy ); } } if ( !is_object($term) ) $term = new WP_Error( 'invalid_term', __( 'Empty Term.' ) ); if ( is_wp_error( $term ) ) return $term; $taxonomy = $term->taxonomy; $termlink = $wp_rewrite->get_extra_permastruct($taxonomy); /** * Filters the permalink structure for a terms before token replacement occurs. * * @since 4.9.0 * * @param string $termlink The permalink structure for the term's taxonomy. * @param WP_Term $term The term object. */ $termlink = apply_filters( 'pre_term_link', $termlink, $term ); $slug = $term->slug; $t = get_taxonomy($taxonomy); if ( empty($termlink) ) { if ( 'category' == $taxonomy ) $termlink = '?cat=' . $term->term_id; elseif ( $t->query_var ) $termlink = "?$t->query_var=$slug"; else $termlink = "?taxonomy=$taxonomy&term=$slug"; $termlink = home_url($termlink); } else { if ( $t->rewrite['hierarchical'] ) { $hierarchical_slugs = array(); $ancestors = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' ); foreach ( (array)$ancestors as $ancestor ) { $ancestor_term = get_term($ancestor, $taxonomy); $hierarchical_slugs[] = $ancestor_term->slug; } $hierarchical_slugs = array_reverse($hierarchical_slugs); $hierarchical_slugs[] = $slug; $termlink = str_replace("%$taxonomy%", implode('/', $hierarchical_slugs), $termlink); } else { $termlink = str_replace("%$taxonomy%", $slug, $termlink); } $termlink = home_url( user_trailingslashit($termlink, 'category') ); } // Back Compat filters. if ( 'post_tag' == $taxonomy ) { /** * Filters the tag link. * * @since 2.3.0 * @deprecated 2.5.0 Use 'term_link' instead. * * @param string $termlink Tag link URL. * @param int $term_id Term ID. */ $termlink = apply_filters( 'tag_link', $termlink, $term->term_id ); } elseif ( 'category' == $taxonomy ) { /** * Filters the category link. * * @since 1.5.0 * @deprecated 2.5.0 Use 'term_link' instead. * * @param string $termlink Category link URL. * @param int $term_id Term ID. */ $termlink = apply_filters( 'category_link', $termlink, $term->term_id ); } /** * Filters the term link. * * @since 2.5.0 * * @param string $termlink Term link URL. * @param object $term Term object. * @param string $taxonomy Taxonomy slug. */ return apply_filters( 'term_link', $termlink, $term, $taxonomy ); }
January 31, 2019 at 1:35 am #489171Brad DaltonParticipantThere is no short link for the tag archive permalink, only single post permalink ( the code of which i have already provided ).
If you want to get the permalink for the tag archive, you can use get_tag_link as another option to get_term_link.
The full code for what you're wanting would need to be written and tested otherwise you may find an existing tutorial which contains code you can use as is or which you can modify.
January 31, 2019 at 1:45 am #489172bluenavyParticipantThankyou for your response.
Is this something you could code on a developer basis ( for a fee of course )
If so, please let me know and length of time to create.
Many Thanks
January 31, 2019 at 1:49 am #489173Brad DaltonParticipantJanuary 31, 2019 at 1:57 am #489174bluenavyParticipantdone
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.