Skip Navigation

[Gelöst] Get the current Content Template, and find out if the current page is a custom post type Archive using Toolset API

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem: Using PHP, I would like to know if the current page is a custom post type archive, and I would like to be able to determine the name of the Content Template applied to the current page.

Solution:
Use the native WordPress function is_post_type_archive() to determine if the current page is a post type archive:

if ( is_post_type_archive( 'my-custom-post-type-slug' ) ) {
  // do something
}

Use the wpv_filter_content_template_output filter to determine the assigned Content Template, and assign that value to a global variable. Then access the title of the Content Template post using get_the_title() since Content Templates are technically posts:

add_filter( 'wpv_filter_content_template_output', 'get_content_template_id', 99, 4 );
function get_content_template_id( $content, $template_selected, $id, $kind ) {
    global $current_archive_template_id;   
    $current_archive_template_id = $template_selected; // $template_selected = current Content Template ID
    return $content;
}
// later...
$content_template_title = get_the_title($current_archive_template_id);

Relevant Documentation: https://codex.wordpress.org/Function_Reference/is_post_type_archive
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_content_template_output

This support ticket is created vor 7 Jahren, 1 Monat. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Dieses Thema enthält 3 Antworten, hat 3 Stimmen.

Zuletzt aktualisiert von chrisH-10 vor 7 Jahren, 1 Monat.

Assistiert von: Christian Cox.

Author
Artikel
#582487
2017-10-26_21-19-03.jpg

I need the following functions:
Function to take the title of the archive page of a new CPT.
Function to know if I am on the archive page of a particular new CPT.
Look at the screenshot.
Thanks for your help
Best regards,
Francisco R.

#582525

Function to know if I am on the archive page of a particular new CPT.
WordPress has a function called is_post_type_archive that does this:

if ( is_post_type_archive( 'my-custom-post-type-slug' ) ) {
  // do something
}

Documentation for that:
https://codex.wordpress.org/Function_Reference/is_post_type_archive

Function to take the title of the archive page of a new CPT.
The only way I am aware of to determine which Content Template is being applied to the current page is by using the filter wpv_filter_content_template_output. This will give you access to the template ID. You could use this value to set a global variable and access it elsewhere:

add_filter( 'wpv_filter_content_template_output', 'get_content_template_id', 99, 4 );
function get_content_template_id( $content, $template_selected, $id, $kind ) {
    global $current_archive_template_id;   
    $current_archive_template_id = $template_selected; // $template_selected = current Content Template ID
    return $content;
}

Documentation for this filter:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_content_template_output

Technically Content Templates are posts, so you can get the name of the Content Template using the standard WordPress method get_the_title() after the global variable is defined:

$content_template_title = get_the_title($current_archive_template_id);
#582625

Ok. Thanks.

#1536441

This is useful but I had the need to get the template name in header.php. The hook called above is only invoked once the page gets to the template. To get the template info earlier I used the following which I lifted from \plugins\wp-views\embedded\inc\views-templates\wpv-template.class.php:

$template_selected = get_post_meta( get_the_ID(), '_views_template', true );
$template_slug = get_post_field( 'post_name', $template_selected );    // The template's slug
$template_class_title = preg_replace("/[^A-Za-z_ ]/",'', get_the_title( $template_selected ) );    // The line used the full title
$template_class = 'views-template-' . strtolower( str_replace( ' ', '-', $template_class_title ) );    // The template class as it appears on the BODY