Hi,
I need to return the post type archive link for a given post, and wrap it in an anchor. In PHP, I'd do:
echo '<a class="post-type" href="' . get_post_type_archive_link( get_post_type() ) . '">' . $post_type->labels->singular_name . '</a>';
In Toolset, there seems no way to do this. The nearest I've been aboe to put together looking at the docs and in the forum is this:
<a class="post-type" href="[wpv-bloginfo show="url"]/[wpv-post-type]">[wpv-post-type show="single"]</a>
However, this breaks whenever the singular name differs from the slug. For instance, I have a site where the default post type of 'Post' needs to be called 'News'. It's referred to as News in the admin UI, its archive slug is '/news', its category base is '/news', etc.
The PHP above would point to /news - which is correct. The only way I can work out to do this with Toolset (above) returns /post - which is wrong.
I cannot work out how to return the archive link for the current post type with Toolset. Am I missing something obvious?
Thanks in advance!
Hi,
Thank you for contacting us and I'd be happy to assist.
As you noted, there is no direct Toolset shortcode available to get the post type's archive link, but you can register a custom shortcode for this:
https://toolset.com/documentation/adding-custom-code/how-to-create-a-custom-shortcode/
Example:
add_shortcode( 'get_post_type_archive_link', 'get_post_type_archive_link_func');
function get_post_type_archive_link_func($atts) {
$a = shortcode_atts( array(
'type' => ''
), $atts );
$type_link = get_post_type_archive_link( $a['type'] );
return $type_link;
}
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.
This new shortcode will return the post type archive link based on post type's slug passed in the attribute "type" and to the get that slug you can use "wpv-post-type" shortcode:
https://toolset.com/documentation/user-guides/views/views-shortcodes/#wpv-post-type
[get_post_type_archive_link type="[wpv-post-type]"]
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Ah, okay - that'll work for now. Thanks Waqar.
Hi Waqar,
Something odd is going on when this gets parsed:
This shortcode:
[get_post_type_archive_link type="[wpv-post-type]"]
correctly returns (for instance, on a post type of 'post' whose archive slug is 'news'): "hidden link{redacted domain}/news/"
However, when I incorporate this into an href:
<a class="post-type" href="[get_post_type_archive_link type="[wpv-post-type]"]">[wpv-post-type show="single"]</a>
it returns: "hidden link{redacted domain}/[get_post_type_archive_link%20type="
breaking the anchor and returning the syntax from the function.
To clarify: I need the archive slug URL to be returned as the href, but I need to display the link text for the archive single name.
Any idea what's happening here?
Thanks!
Hi,
Thanks for writing back.
Since you're using double-quotes in your link's code ( a tag ), you'll need to replace the inner shortcode's double-quotes with single-quotes, so that they're properly escaped and recognized:
<a class="post-type" href="[get_post_type_archive_link type='[wpv-post-type]']">[wpv-post-type show="single"]</a>
This should do the trick.
regards,
Waqar
Oh, of course I do - thanks Waqar, I really wasn't awake when looking at this.