plugins - archive_template override when no posts exist

admin2025-06-06  3

I have a plugin which contains a single PHP template for both single_template and archive_template for multiple post types. I am loading a client side application in this template, which is why I only need a single template for all four variants.

The code I have is:

add_filter( 'single_template', 'react_template' );
add_filter( 'archive_template', 'react_template' );

function react_template( $template ) {

    global $post;
    if ( $post->post_type == 'tutorial' || $post->post_type == 'dashboard' ) {
        if ( file_exists( PLUGIN_DIR . 'templates/learning.php' ) ) {
            return PLUGIN_DIR . 'templates/learning.php';
        }
    }

    return $template;
}

This is working perfectly for my needs. However, if no posts exist for either tutorial or dashboard types I get an error because $post is null.

It's not a big deal as in production there will always be numerous posts in the system, but it bugs me. I do not want to have a named template for each type. Is there a way to have my archive template work when no posts exist?

I have a plugin which contains a single PHP template for both single_template and archive_template for multiple post types. I am loading a client side application in this template, which is why I only need a single template for all four variants.

The code I have is:

add_filter( 'single_template', 'react_template' );
add_filter( 'archive_template', 'react_template' );

function react_template( $template ) {

    global $post;
    if ( $post->post_type == 'tutorial' || $post->post_type == 'dashboard' ) {
        if ( file_exists( PLUGIN_DIR . 'templates/learning.php' ) ) {
            return PLUGIN_DIR . 'templates/learning.php';
        }
    }

    return $template;
}

This is working perfectly for my needs. However, if no posts exist for either tutorial or dashboard types I get an error because $post is null.

It's not a big deal as in production there will always be numerous posts in the system, but it bugs me. I do not want to have a named template for each type. Is there a way to have my archive template work when no posts exist?

Share Improve this question asked Nov 28, 2018 at 0:10 hsimahhsimah 1054 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

One way to accomplish this may be to use the pre_get_posts to conditionally add the single_template and archive_template filters.

namespace StackExchange\WordPress;

/**
 * Add filters for `single_template`, and `archive_template` for the tutorial and 
 * dashboard archives.
 */
function pre_get_posts( $query )
{
  if( is_singular() )
  {
    $post_type = get_post_type();
    if ( 'tutorial' === $post_type  || 'dashboard' === $post_type) )
    {
      add_filter( 'single_template', __NAMESPACE__ . '\react_template' );
      return $query;
    }
  }

  if ( ! is_post_type_archive( [ 'tutorial', 'dashboard' ] ) )
  {
    return $query;
  }
  if( ! isset( $query->query[ 'post_type' ] )
  {
    return $query;
  }

  $post_type = $query->query['post_type'];
  if ( 'tutorial' === $post_type  || 'dashboard' === $post_type) )
  {
    add_filter( 'archive_template', __NAMESPACE__ . '\react_template' );
  }
  return $query;
}
add_action( 'pre_get_posts', __NAMESPACE__ . '\pre_get_posts' );

/**
 * Return the React Template, if it exists
 */
function react_template( $template )
{
  if ( file_exists( PLUGIN_DIR . 'templates/learning.php' ) )
  {
    return PLUGIN_DIR . 'templates/learning.php';
  }
  return $template;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749146959a316765.html

最新回复(0)