php 用于创建图库postype和分类的插件。以及创建一些模板文件以在主题中显示图库帖子类型页面

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 用于创建图库postype和分类的插件。以及创建一些模板文件以在主题中显示图库帖子类型页面相关的知识,希望对你有一定的参考价值。

<?php
  /*
  Plugin Name: Sweet Experience Gallery Post Type
  Plugin URI: anguloroberto.com.ve
  Description: Create custom gallery post type and taxonomies
  Version: 0.1
  Author: Roberto Angulo
  Text Domain : sx-gallery-postype
  Author URI: anguloroberto.com.ve
  */

  /**
   * Copyright (c) Roberto Angulo 2017 (email: robertoeat91@gmail.com) . All rights reserved.
   *
   * Released under the GPL license
   * http://www.opensource.org/licenses/gpl-license.php
   *
   * This is an add-on for WordPress
   * http://wordpress.org/
   *
   * **********************************************************************
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   * **********************************************************************
   */

   // Disable access from outside wordpress
    if ( ! defined( 'ABSPATH' ) ) {
      exit;
    }

   /**
    * Class to create a gallery custom post type
    * and add some functions to act on the backend
    */
   class Sx_Gallery_Postype
   {
     /**
      * Property where we store an instance of the class
      *
      * @var null $instance
      */
     private static $instance = null;

     private function __construct()
     {
       # code...
     }

     /**
      * We set an instance of the class
      *
      * @return self::$instance object
      */
      public static function sx_gallery_postype_get_instance() {

        if ( is_null( self::$instance ) ) {
           self::$instance = new self;
        }
        return self::$instance;
      }

      /**
       * Hooks
       */
      public function sx_gallery_postype_setup()
      {
         add_action( 'init', array( $this, 'sx_gallery_postype_create' ) );
         add_action( 'do_meta_boxes', array( $this, 'sx_gallery_postype_reassign_post_feature_metabox_position' ) );
      }

     // function that handle the flush rewrite rules,
     // so a new permalinks structure config make effect immediately
      public function sx_gallery_postype_flush_rewrites() {
       	$this->sx_gallery_postype_create();
       	flush_rewrite_rules();
     }


      // create galleries page dinamically, when the plugins is activated
     public function sx_gallery_postype_create_page_dinamically()
     {
       /**
        * Object with theme data
        *
        * @var Object $current_theme
        */
       $current_theme = wp_get_theme();

       /**
        * Path to the theme folder
        *
        * @var string $theme_path
        */
       $theme_path = get_theme_root() . "/" . $current_theme->stylesheet;

       /**
        * Complete uri for the gallery template
        *
        * @var string $template_file
        */
       $template_file = $theme_path . '/' . 'gallery-template.php';

       if ( ! file_exists( $template_file ) ) { // if the gallery template doesn't exists we create it

           // Gallery template
           $content = '<?php
             /**
             * Template Name: My Gallery Template
             */
             get_header();

             /**
              * Taxonomy name
              *
              * @var string $taxonomy' .
              '*/
               $taxonomy' . "= 'category_gallery';

               /**
                * Custom post type name
                *
                * @var string" . '$post_type
                */
               $post_type' . "= 'sx_gallery_postype';

               /* Display custom post type content */

               echo do_shortcode( '[sx_gallery_postype_page_content_shortcode]'," . '$taxonomy, $post_type );
             get_footer();';

           $file_handle = fopen( $template_file, "wb" );
           fwrite( $file_handle, $content );
           fclose( $file_handle );

       }

       /**
        * Complete uri for the taxonomy category gallery
        *
        * @var string $template_file
        */

       $template_file = $theme_path . '/' . 'taxonomy-category_gallery.php';

      if ( ! file_exists( $template_file ) ) { // if the taxonomy template doesn't exists we create it

          $content = "<?php
           /**
           * Template for the 'category_gallery' taxonomy
           */
           get_header();
           ?>" .
              '<div class="container container-margin-top">
                <?php
                   /* Display layout, photo cards, term links and pagination */' .

                  "
                   echo do_shortcode( '[sx_gallery_pages_layout_shortcode]' );
                ?>
             </div>
           <?php get_footer();";

           $file_handle = fopen( $template_file, "wb" );
           fwrite( $file_handle, $content );
           fclose( $file_handle );
      }

      /**
       * Complete uri for the taxonomy tag gallery
       *
       * @var string $template_file
       */
      $template_file = $theme_path . '/' . 'taxonomy-tag_gallery.php';

      if ( ! file_exists( $template_file ) ) { // if the taxonomy template doesn't exists we create it

        $content = "<?php
        /**
        * Template for the 'tag_gallery' taxonomy
         */
        get_header();
        ?>" .
          '<div class="container container-margin-top">
            <?php
                /* Display layout, photo cards, term links and pagination  */' .

                "
                echo do_shortcode( '[sx_gallery_pages_layout_shortcode]' );
            ?>
          </div>
        <?php
        get_footer();";

         $file_handle = fopen( $template_file, "wb" );
         fwrite( $file_handle, $content );
         fclose( $file_handle );

      }

      /**
       * Complete uri for the archive gallery page
       *
       * @var string $template_file
       */
      $template_file = $theme_path . '/' . 'archive-sx_gallery_postype.php';

      if ( ! file_exists( $template_file ) ) { // if the archive gallery page doesn't exists we create it

        $content = '<?php
          /**
          * Archive template for the gallery custom post type
          */
          get_header();
        ?>
        <div class="container container-margin-top">
          <section class="row">
            <h1 class="text-center">
              <?php the_archive_title(); ?>
            </h1>
            <?php
                if( have_posts() ) {
                  while ( have_posts() ) {
                    the_post();

                      ?>

                        <div class="col-sm-6 col-sm-offset-3 col-md-3 col-md-offset-1 post-card post-card--margin-left post-card--margin-top post-card--border-customizer margin-bottom">
                            <div class="row">
                                <figure>
                                    <?php
                                        $id = get_the_ID();

                                        $src_large = get_the_post_thumbnail_url( $id, ' . "'large' );
                                     ?>" .
                                      '<a href="<?php echo esc_url( $src_large ); ?>" class="pretty-photo">
                                        <?php
                                            $args_thumbnail = array(' .
                                              "'class' => 'width-100 height-100 margin-bottom'
                                            );
                                            echo get_the_post_thumbnail(" . ' $id, ' . "'sx-gallery-custom-thumbnail'," . ' $args_thumbnail );
                                        ?>
                                      </a>
                                  </figure>
                              </div>
                               <p class="text-center">
                                <?php echo get_the_title(); ?>
                              </p>
                              <?php

                                $category_terms = get_the_terms( $id,' . "'category_gallery' ); ?>" .

                                '<div class="section-separator--thiner primary-color-customize"></div>
                                <?php

                                  echo apply_filters(' . "'sx_filter_custom_taxonomy_terms'," . ' $category_terms );

                              ?>
                          </div>
                      <?php
                  }
                } else {
                  ?>

                  <h3 class="text-center"><?php _e( "Sorry, we have not found any post", $current_theme->get( "TextDomain" ) ); ?></h3>

                  <?php
                }
             ?>
          </section>
        </div>
        <?php
          get_footer();';

         $file_handle = fopen( $template_file, "wb" );
         fwrite( $file_handle, $content );
         fclose( $file_handle );

      }


        $gallery_page_option = get_option( 'sx_gallery_page_id' );

         if ( ! $gallery_page_option ) {

           // Arguments for the wp_insert_post function
             $gallery_page_args =
                 array(
                   'post_title' => 'Galleries',
                   'post_name' => 'my-galleries',
                   'post_content' => '',
                   'post_status' => 'publish',
                   'post_type' => 'page',
                   'page_template' => 'gallery-template.php',
                 );

            /**
             * Gallery page post ID
             *
             * @var string $inserted_gallery_page
             */
             $inserted_gallery_page = wp_insert_post( $gallery_page_args, true );

             update_option( 'sx_gallery_page_id', $inserted_gallery_page );
         }
     }

     // Reassign a position for the post feature metabox
     public function sx_gallery_postype_reassign_post_feature_metabox_position()
     {
       remove_meta_box(
         'postimagediv',
         'sx_gallery_postype',
         'side'
       );
       add_meta_box(
         'postimagediv',
          esc_html__( 'Featured Image', 'sx-gallery-postype' ),
         'post_thumbnail_meta_box',
         'sx_gallery_postype',
         'normal',
         'high'
        );
     }

     /**
      * We register a new post type
      */
     public function sx_gallery_postype_create()
     {
       /**
        * Labels collection for the 'labels' argument
        *
        * @var array $labels
        */
       $labels = array(
           'name' => 'Galleries',
           'singular_name' => __( 'Gallery', 'sx-gallery-postype' ),
           'add_new' => __( 'Add new gallery', 'sx-gallery-postype' ),
           'add_new_item' => __( 'Add new gallery', 'sx-gallery-postype' ),
           'edit' => __( 'Edit', 'sx-gallery-postype' ),
           'edit_item' => __( 'Edit gallery', 'sx-gallery-postype' ),
           'new_item' => __( 'New gallery', 'sx-gallery-postype' ),
           'view' => __( 'View gallery', 'sx-gallery-postype' ),
           'view_item' => __( 'View gallery', 'sx-gallery-postype' ),
           'search items' => __( 'Search galleries', 'sx-gallery-postype' ),
           'not_found' => __( 'No galleries found', 'sx-gallery-postype' ),
           'not_found_in_trash' => __( 'No galleries in the trash', 'sx-gallery-postype' ),
       );

       /**
        * Arguments for the 'register_post_type' function
        *
        * @var array $args
        */
       $args = array(
           'labels' => $labels,
           'hierarchical' => false,
           'description' => __( 'Share image galleries of your own trips' , 'sx-gallery-postype' ),
           'public' => true,
           'menu_position' => 20,
           'menu_icon' => 'dashicons-format-gallery',
           'has_archive' => 'galleries',
           'rewrite_rules' => array( 'slug' => 'galleries', 'with_front' => false ),
           'supports' => array( 'title', 'thumbnail' ),
       );

       register_post_type( 'sx_gallery_postype', $args );
     }
   }

   add_action( 'plugins_loaded', array( Sx_Gallery_Postype::sx_gallery_postype_get_instance(), 'sx_gallery_postype_setup' ) );

  // reflush rewrite rules
  register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
  register_activation_hook( __FILE__, array( Sx_Gallery_Postype::sx_gallery_postype_get_instance(), 'sx_gallery_postype_flush_rewrites' )  );

  // Create pages and template files dinamically on activation plugin
  register_activation_hook( __FILE__, array( Sx_Gallery_Postype::sx_gallery_postype_get_instance(), 'sx_gallery_postype_create_page_dinamically' )  );

  // We call the class where we registered the custom taxonomies
  require_once( __DIR__ . '\includes\class-gallery-custom-taxonomies.php' );

  // We call the class where we registered the shortcodes
  require_once( __DIR__ . '\includes\class-gallery-frontend.php' );
<?php
  /**
   * Methods to display visual content on galleries pages, on the frontend
   *
   * @package Sweet Experience Gallery Post Type
   * @since Sweet Experience Gallery Post Type 1.0
   */
  class Sx_Gallery_Postype_Frontend
  {

    /**
     * We will use it to initiate an instance of the class
     *
     * @var null $instance
     */
    private static $instance = null;

    /**
     * We will use it to store a message when no content is found in the loop
     *
     * @var string $no_content
     */
    public $no_content = '';

    private function __construct()
    {
      // 'No content' message
      $this->no_content = '<h3 class="text-center">' . __( 'Sorry, no posts were found', 'sx-gallery-postype' ) . '</h3>';
    }

    // Get instance of the class
    public static function sx_gallery_postype_frontend_get_instance()
    {

      if ( is_null( self::$instance ) ) {
        self::$instance = new self;
      }
      return self::$instance;
    }

    // Hooks
    public function sx_gallery_postype_frontend_setup()
    {
      // Register shortcode
      add_action( 'init', array( $this, 'sx_gallery_postype_frontend_register_shortcodes' ) );

      // Display pagination
      add_action( 'sx_gallery_postype_frontend_pagination', array( $this, 'sx_gallery_postype_frontend_pagination' ) );

      // Filter links taxonomy terms
      add_action( 'sx_gallery_postype_frontend_filter_taxonomy_terms', array( $this, 'sx_gallery_postype_frontend_filter_taxonomy_terms' ) );

      // Handle data to display content on taxonomy pages conditionally
      add_action( 'sx_gallery_postype_frontend_loop_taxonomy_terms', array( $this, 'sx_gallery_postype_frontend_loop_taxonomy_terms' ), 10, 3 );

      // Display gallery post type archive links
      add_action( 'sx_gallery_postype_frontpage_monthly_archive_links', array( $this, 'sx_gallery_postype_frontpage_monthly_archive_links' ) );

      // Enqueue styles
      add_action( 'wp_enqueue_scripts', array( $this, 'sx_gallery_postype_enqueue_scripts' ) );

      // Operations to do, during the init action
      add_action( 'init', array( $this, 'sx_gallery_postype_init_actions' ) );
    }

    // Operations to do, during the init action
    public function sx_gallery_postype_init_actions()
    {
      add_image_size( 'sx-gallery-custom-thumbnail', 250, 250, array( 'left', 'center' ) );
    }

    // Enqueue styles
    public function sx_gallery_postype_enqueue_scripts()
    {
      // Bootstrap
      wp_register_style( 'sx_gallery_postype_bootstrap_styles', plugins_url( 'css/bootstrap.min.css', __DIR__ ) );

      // Iconmoon icons
      wp_register_style( 'sx_gallery_postype_icons', plugins_url( 'icons/Font/demo-files/demo.css', __DIR__ ), array( 'sx_gallery_postype_bootstrap_styles' ) );

      // Plugin styles
      wp_enqueue_style( 'sx_gallery_postype_custom_styles', plugins_url( 'css/sx-gallery_postype_styles.css', __DIR__ ), array( 'sx_gallery_postype_icons' ) );

    }

    // Display monthly archive links
    function sx_gallery_postype_frontpage_monthly_archive_links( $args = array() )
    {
      ?>
      <h4 class="text-center margin-bottom">
        <?php _e( 'Posts classified by month', 'sx-gallery-postype' ); ?>
      </h4>

        <ul class="text-center list-inline margin-bottom">
          <?php
            wp_get_archives( $args );
          ?>
        </ul>
      <?php
    }

    // Register shorcodes
    public function sx_gallery_postype_frontend_register_shortcodes()
    {

      add_shortcode( 'sx_gallery_postype_page_content_shortcode', array( $this, 'sx_gallery_postype_page_content_shortcode' ) );


      add_shortcode( 'sx_gallery_pages_layout_shortcode', array( $this, 'sx_gallery_pages_layout_shortcode' ) );
    }

    // Shortcode callback
    public function sx_gallery_pages_layout_shortcode( $atts, $content = null )
    {

      /**
       * Current taxonomy page to show content
       *
       * @var string $taxonomy
       */
      $taxonomy = is_tax( 'category_gallery' ) ? 'category_gallery' : 'tag_gallery';

      /**
       * This is to show links to the other taxonomy page, that means that, if
       * we are on 'category_gallery', show links to 'tag_gallery', and viceversa
       *
       * @var string $taxonomy_2
       */
      $taxonomy_2 = is_tax( 'category_gallery' ) ? 'tag_gallery' : 'category_gallery';

      $attributes = shortcode_atts(
          array(
            'taxonomy' => $taxonomy,
            'taxonomy_2' => $taxonomy_2,
          ), $atts
      );

      // Previus defined shortcode attributes, we are going to use it as
      // parameters of sx_gallery_pages_layout() function, so we can retrieve
      // the taxonomy pages content

      $taxonomy = $attributes['taxonomy'];

      $taxonomy_2 = $attributes['taxonomy_2'];

      return $this->sx_gallery_pages_layout( $taxonomy, $taxonomy_2 );
    }

    /**
     * Custom action callback to handle object terms and display gallery posts conditionally
     *
     * @param array $obj_terms Array objects of taxonomy terms
     * @param string $taxonomy Taxonomy name
     * @param string $postype Registered postype name
     */
       public function sx_gallery_postype_frontend_loop_taxonomy_terms( $obj_terms, $taxonomy, $postype )
      {
        global $post;
        $terms_slug = array();

        foreach ( $obj_terms as $term_key => $term_value ) {
            $term_value = ( array ) $term_value; // convert to an array each object so we can loop throught it
            foreach ( $term_value as $key => $value ) {
                $terms_slug[] = $term_value['slug']; // we store on the variable that we defined earlier, an array of term slugs
            }
        }
        $terms_slug = array_unique( $terms_slug ); // then we make sure that any value is repeated

        if( have_posts() ) {
          while ( have_posts() ) {
            the_post();

                foreach ( $terms_slug as $terms_slug_key => $terms_slug_value ) {

                  $my_posts = get_posts(
                        array(
                          'post_type' => $postype,
                          $taxonomy => $terms_slug_value, // pass in the array slug as a parameter to get the posts
                          'numberposts' => 1,
                        )
                    );

                    /**
                     * Object from each term of the current taxonomy
                     *
                     * @var object $obj_term
                     */
                     $obj_term = get_term_by( 'slug', $terms_slug_value, $taxonomy );

                      foreach ( $my_posts as $key => $my_post ) {

                        /**
                         * Gallery page id
                         *
                         * @var integer $gallery_page_id
                         */
                        $gallery_page_id = absint( get_option( 'sx_gallery_page_id' ) );

                        /**
                         * Url of the taxonomy term
                         *
                         * @var string $term_link
                         */
                        $term_link = get_term_link( $obj_term->term_id );

                        if ( $post->ID === $gallery_page_id ) { // check if we are on the gallery page, so we return the conciern layout

                          // post card image
                          $markup_gallery = '<div class="col-sm-6 col-sm-offset-3 col-md-3 col-md-offset-1 post-card post-card--filter-sature-hover post-card--margin-left post-card--margin-top post-card--border-customizer margin-bottom">' .

                                    '<a href="' . esc_url( $term_link ) . '" class="">' .

                                        '<div class="row">' .

                                          get_the_post_thumbnail( $my_post->ID, 'sx-gallery-custom-thumbnail', array( 'class' => 'center-block margin-bottom width-100 height-100', ) ) .

                                        '</div>' .

                                      '</a>' .

                                      '<p>' .
                                        '<a href="' . esc_url( $term_link ) . '">' .
                                        // translators: '%s' : Custom category name
                                        sprintf( esc_html__( '%s', 'sx-gallery-postype' ), $obj_term->name ) .
                                        '</a>' .
                                      '</p>' .

                                    '</div>';

                              echo $markup_gallery;

                          }
                        }
                     }
                   }
                }
          }

      /**
       * Content layout of the main sx_gallery_postype page
       *
       * @param string $taxonomy Taxonomy name
       * @param string $postype Custom post type name
       */
      public function sx_gallery_postype_page_content( $taxonomy, $postype )
      {
        ?>
        <div class="container container-margin-top margin-bottom">
            <div class="row">
                <div class="col-xs-12">
                    <?php
                      if( have_posts() ) {
                        while ( have_posts() ) {
                          the_post();
                        ?>
                        <h1 class="text-center">
                          <?php echo get_the_title(); ?>
                        </h1>
                        <?php
                          the_content();
                        }
                      } else {
                          echo $this->no_content;
                      }
                    ?>
                </div>
            </div>
        </div>

        <div class="container">
          <div class="row">
              <?php
                  /**
                   * Array of objects terms from $taxonomy
                   *
                   * @var array $terms_array
                   */
                  $terms_array = get_terms( array( 'taxonomy' => $taxonomy ) );

                  if ( ! empty( $terms_array ) && ! is_wp_error( $terms_array ) ) {

                    // Displaying gallery post type content layout
                    do_action( 'sx_gallery_postype_frontend_loop_taxonomy_terms', $terms_array, $taxonomy, $postype );
                  }
              ?>
          </div>
        </div>

        <div class="container container-margin-top">
          <hr>
          <div class="row">
            <?php
                $args = array(
                  'post_type' => $postype,
                );
                //  display monthly archive links
                do_action( 'sx_gallery_postype_frontpage_monthly_archive_links', $args );
            ?>
          </div>
        </div>
        <?php
      }

      // Function shortcode callback
      public function sx_gallery_postype_page_content_shortcode( $atts, $content = null )
      {
        /**
         * Array of attributes for the shortcode
         *
         * @var array $attrs_shortcode
         */
        $attrs_shortcode = shortcode_atts(
                  array(
                    'taxonomy' => 'category_gallery',
                    'post_type' => 'sx_gallery_postype',
                    ), $atts );

        /**
         * Taxonomy name
         *
         * @var string $taxonomy
         */
        $taxonomy = $attrs_shortcode['taxonomy'];

        /**
         * Post type name
         *
         * @var string $postype
         */
        $postype = $attrs_shortcode['post_type'];

        return $this->sx_gallery_postype_page_content( $taxonomy, $postype );
      }

      /**
      * Display design content for gallery pages
      *
      * @param $taxonomy Taxonomy name
      * @param $taxonomy_2 Taxonomy name
      */
      public function sx_gallery_pages_layout( $taxonomy, $taxonomy_2 )
      {
        /**
        * Current term title
        *
        * @var string $category_name
        */
       $category_name = single_term_title( '', false );

        // Display term title
       // translators: '%s' : Post category name
       echo '<h1 class="text-center">' . sprintf( esc_html__( '%s' , 'sx-gallery-postype' ), $category_name ) . '</h1>' . '<hr>';

       $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

      $args = array(
        $taxonomy => $category_name,
        'paged' => $paged,
      );
      $my_query = new WP_Query( $args );
        ?>
      <div class="row">
        <?php
        if( $my_query->have_posts() ) {

          while ( $my_query->have_posts() ) {
            $my_query->the_post();

              /**
               * Post title
               *
               * @var string $title
               */
              $title = get_the_title();

              /**
               * Current post id
               *
               * @var integer $id
               */
              $id = get_the_ID();

              /**
               * custom thumbnail size
               *
               * @var string $thumb
               */
              $thumb = 'sx-gallery-custom-thumbnail';

              /**
               * large thumbnail size
               *
               * @var string $large
               */
              $large = 'large';

              /**
               * Post thumbnail url source
               *
               * @var string $thumbnail_url
               */
              $thumbnail_url = get_the_post_thumbnail_url( $id, $large );

            ?>
                <div class="col-sm-6 col-sm-offset-3 col-md-3 col-md-offset-1 post-card post-card--filter-sature-hover post-card--margin-left post-card--margin-top post-card--border-customizer margin-bottom">

                            <!-- post card -->
                            <a href="<?php echo $thumbnail_url ? esc_url( $thumbnail_url ) : ''; ?>" class="pretty-photo margin-top">
                              <div class="row">
                                <?php
                                  $thumbnail_attrs =
                                  array(
                                    'class' => 'img-responsive width-100 height-100'
                                  );

                                   echo get_the_post_thumbnail( $id, $thumb, $thumbnail_attrs );
                                 ?>
                              </div>
                            </a>

                              <!-- Display title post -->
                              <p class="margin-top text-center">
                                <?php
                                  $title = wordwrap( $title, 27, '<br />' );

                                  echo $title;
                                ?>
                              </p>

                             <div class="section-separator--thiner primary-color-customize"></div>

                         <?php
                             /**
                              * Array taxonomy terms
                              *
                              * @var array $array_terms
                              */
                             $array_terms = get_the_terms( $id, $taxonomy_2 );

                             if ( $array_terms && ! is_wp_error( $array_terms ) ) { // check that we have a valid array terms

                               // Output term links separated by commas
                               echo apply_filters( 'sx_gallery_postype_frontend_filter_taxonomy_terms', $array_terms );
                             }
                         ?>
                      </div>
                  <?php
                }

                 if ( $my_query->max_num_pages > 1 ): ?>
                  <div class="col-md-6 col-md-offset-3 margin-top">
                    <!-- Pagination -->
                      <h4 class="text-center margin-top">
                        <?php
                            do_action( 'sx_gallery_postype_frontend_pagination', $my_query->max_num_pages );
                        ?>
                      </h4>
                  </div>
                <?php endif; ?>

                <?php
              } else {
                      echo $this->no_content;
              }
              wp_reset_postdata();  ?>
              </div>
            <?php
      }

      /**
       * Callback filter, we loop thorught the terms, then we build links markup
       *
       * @param array $terms Array of taxonomy terms
       * @return $markup_terms string
       */
       public function sx_gallery_postype_frontend_filter_taxonomy_terms( $terms )
      {
          $array_links = array();

          foreach ( $terms as $key => $obj_term )
          {
              /**
               * url taxonomy term
               *
               * @var string $url_term
               */
              $url_term = get_term_link( $obj_term->term_id );

              /**
               * Array with the markup term links
               *
               * @var array $array_links
               */
              $array_links[] = '<a href="' . esc_url( $url_term ) . '">' .

              // translators: '%s' : Tag/category name
              sprintf( esc_html__( '%s', 'sx-gallery-postype' ), $obj_term->name ) .

              '</a>';

          }

          /**
           * The term links separated by a commas
           *
           * @var string $term_links
           */
          $term_links = implode( ', ', $array_links );

          return $markup_terms = '<p class="text-center margin-top">' . $term_links . '</p>';
      }

      /**
       * Pagination gallery posts pages
       *
       * @param integer $total_pags Max number of pages from a query
       */
      public function sx_gallery_postype_frontend_pagination( $total_pags )
     {

         $big = 999999999; // need an unlikely integer

         echo paginate_links( array(
           'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
           'current' => max( 1, get_query_var('paged') ),
           'prev_text'          => '<span class="icon icon-arrow-left2"></span>' . __( ' Previous' ),
           'next_text'          => __( 'Next ' ) . '<span class="icon icon-arrow-right2"></span>',
           'total' => $total_pags
         ) );
     }

  }

  add_action( 'plugins_loaded', array( Sx_Gallery_Postype_Frontend::sx_gallery_postype_frontend_get_instance(), 'sx_gallery_postype_frontend_setup' ) );
<?php
 /**
 * Register custom taxonomies for the gallery post type
 *
 * @package Sweet Experience Gallery Post Type
 * @since Sweet Experience Gallery Post Type 1.0
 */

   class Sx_Gallery_Postype_Taxonomies
   {

     /**
      * Property where we store an instance of the class
      *
      * @var null $instance
      */
     private static $instance = null;


     function __construct()
     {
     }

     public function sx_gallery_postype_taxonomies_setup()
     {
       // Register custom tag taxonomy
       add_action( 'init', array( $this, 'sx_gallery_postype_taxonomies_tag_register' ) );

       // Register custom category taxonomy
       add_action( 'init', array( $this, 'sx_gallery_postype_taxonomies_category_register' ) );
     }

     /**
      * We set an instance of the class
      *
      * @return self::$instance object
      */
      public static function sx_gallery_postype_taxonomies_get_instance() {

        if ( is_null( self::$instance ) ) {
           self::$instance = new self;
        }
        return self::$instance;
      }

     //  We register custom taxonomies
      public function sx_gallery_postype_taxonomies_tag_register()
      {
          /**
           * Custom labels for our new 'tag_gallery' taxonomy
           *
           * @var array $labels_tag_taxonomy
           */
          $labels_tag_taxonomy = array(
            'name' => _X( 'Tags gallery', 'taxonomy general name', 'sx-gallery-postype' ),
            'singular_name' => _X( 'Tag gallery', 'taxonomy singular name', 'sx-gallery-postype' ),
            'search_items' => __( 'Search tags galleries', 'sx-gallery-postype' ),
            'popular_items' => __( 'Popular tags galleries', 'sx-gallery-postype' ),
            'all_items' => __( 'All tags galleries', 'sx-gallery-postype' ),
            'edit_item' => __( 'Edit tag gallery', 'sx-gallery-postype' ),
            'update_item' => __( 'Update tag gallery', 'sx-gallery-postype' ),
            'not_found' => __( 'No tag gallery found', 'sx-gallery-postype' ),
            'not_found_in_trash' => __( 'No tag gallery found in trash', 'sx-gallery-postype' ),
            'add_new_item' => __( 'Add new tag gallery', 'sx-gallery-postype' ),
            'new_item_name' => __( 'New tag gallery name', 'sx-gallery-postype' ),
            'menu_name' => __( 'Tags galleries', 'sx-gallery-postype' ),
          );

           /**
            * Array arguments for our new 'tag_gallery' taxonomy
            *
            * @var array $args_taxonomies
            */
           $args_taxonomies =  array(
                'labels' => $labels_tag_taxonomy,
                'hierarchical' => false,
                'description' => __( 'Clasify your photos by tags, so you can have an organize gallery', 'sx-gallery-postype' ),
                'rewrite' => array( 'slug' => 'tags-gallery', 'with_front' => false ),
                'update_count_callback' => '_update_post_term_count'
            );


          register_taxonomy( 'tag_gallery', 'sx_gallery_postype', $args_taxonomies );

          // Connect custom taxonomy with object type.
          // Codex recommendation ( https://codex.wordpress.org/Function_Reference/register_taxonomy_for_object_type )
          register_taxonomy_for_object_type( 'tag_gallery', 'sx_gallery_postype' );
      }

      public function sx_gallery_postype_taxonomies_category_register()
      {
        /**
         * Custom labels for our new 'category_gallery' taxonomy
         *
         * @var array $labels_cat_taxonomy
         */
         $labels_cat_taxonomy = array(
           'name' => _X( 'Categories gallery', 'taxonomy general name', 'sx-gallery-postype' ),
           'singular_name' => _X( 'Category gallery', 'taxonomy singular name', 'sx-gallery-postype' ),
           'search_items' => __( 'Search categories galleries', 'sx-gallery-postype' ),
           'popular_items' => __( 'Popular categories galleries', 'sx-gallery-postype' ),
           'all_items' => __( 'All categories galleries', 'sx-gallery-postype' ),
           'edit_item' => __( 'Edit category gallery', 'sx-gallery-postype' ),
           'update_item' => __( 'Update category gallery', 'sx-gallery-postype' ),
           'not_found' => __( 'No category gallery found', 'sx-gallery-postype' ),
           'not_found_in_trash' => __( 'No category gallery found in trash', 'sx-gallery-postype' ),
           'add_new_item' => __( 'Add new category gallery', 'sx-gallery-postype' ),
           'new_item_name' => __( 'New category gallery name', 'sx-gallery-postype' ),
           'menu_name' => __( 'Categories galleries', 'sx-gallery-postype' ),
         );

         /**
          * Array arguments for our new 'category_gallery' taxonomy
          *
          * @var array $cat_args
          */
         $cat_args = array(
           'labels' => $labels_cat_taxonomy,
           'hierarchical' => true,
           'description' => __( 'Clasify your photos by categories', 'sx-gallery-postype' ),
           'rewrite' => array( 'slug' => 'categories-gallery', 'with_front' => false ),
         );

         // register taxonomy
         register_taxonomy( 'category_gallery', 'sx_gallery_postype', $cat_args );
         register_taxonomy_for_object_type( 'category_gallery', 'sx_gallery_postype' );
      }

   }

   add_action( 'plugins_loaded', array( Sx_Gallery_Postype_Taxonomies::sx_gallery_postype_taxonomies_get_instance(), 'sx_gallery_postype_taxonomies_setup' ) );

以上是关于php 用于创建图库postype和分类的插件。以及创建一些模板文件以在主题中显示图库帖子类型页面的主要内容,如果未能解决你的问题,请参考以下文章

PHP 带有自定义帖子类型和自定义分类的WordPress图库页面

php 创建自定义wordpress图库输出

php 创建自定义wordpress图库输出

php 创建自定义wordpress图库输出

文件上传选项以从相机拍摄图像或从图库中选择不适用于 Mozilla Firefox 中的 Web 应用程序

PHP的93个WordPress插件有后门