Skip Navigation

[Resolved] Post Type Archive Link

This support ticket is created 4 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 0.74 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by logicelf 4 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#1409889

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!

#1411123

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

#1411709

Ah, okay - that'll work for now. Thanks Waqar.

#1415843

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!

#1415853

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

#1416069

Oh, of course I do - thanks Waqar, I really wasn't awake when looking at this.