【问题标题】:Customizr WP Theme remove links from post meta tagsCustomizr WP Theme 从帖子元标记中删除链接
【发布时间】:2017-11-07 00:13:08
【问题描述】:

我搜索并没有发现这个问题之前已经发布过。

在每个帖子的顶部显示元数据,包括作者、条目日期、更新日期和分类。其中每一个还包括一个 url。我想显示元数据但删除链接。我找到了 php 文件并成功修改它以达到预期的效果。我需要帮助的是如何在我的子主题中进行这些修改,以便在主题更新后不会覆盖这些更改。

我一直试图在他们的 github 帐户上从主题发布者那里获得一些指导,但到目前为止没有任何回应。

Customizr 免费版 4.0.11

详情如下:

我能够找到 php 文件:core/front/models/content/post-metas/class-model-post_metas.php

我能够找到并修改将 url 链接插入 author-meta 元素的函数部分。注意,下面的示例仅从作者元标记中删除链接。对其他元数据进行了类似的修改。这是一个私有函数,所以我认为我不能从 functions.php 调用它。

我对名为 czr_fn_get_meta_author() 的函数进行了更改

private function czr_fn_get_meta_author() {
    $author_id = null;

    if ( is_single() )
      if ( ! in_the_loop() ) {
        global $post;
        $author_id = $post->post_author;
      }
    return apply_filters(
    'tc_author_meta',
    sprintf( '<span class="author vcard author_name"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>' ,
        esc_url( get_author_posts_url( get_the_author_meta( 'ID', $author_id ) ) ),
        esc_attr( sprintf( __( 'View all posts by %s' , 'customizr' ), get_the_author_meta( 'display_name', $author_id ) ) ),
        get_the_author_meta( 'display_name', $author_id )
    )
);//end filter
  }

这是修改后的版本:

  private function czr_fn_get_meta_author() {
    $author_id = null;

    if ( is_single() )
      if ( ! in_the_loop() ) {
        global $post;
        $author_id = $post->post_author;
      }
    return apply_filters(
        'tc_author_meta',
        sprintf( '<span class="author vcard author_name">%3$s</span>' ,
            esc_url( get_author_posts_url( get_the_author_meta( 'ID', $author_id ) ) ),
            esc_attr( sprintf( __( 'View all posts by %s' , 'customizr' ), get_the_author_meta( 'display_name', $author_id ) ) ),
            get_the_author_meta( 'display_name', $author_id )
        )
    );//end filter
  }

所以我可以弄清楚如何对 entry-meta 和其他人做同样的事情。现在我只需要知道如何在我的子主题中进行这项工作,这样它就不会被更新删除。

请,谢谢。

【问题讨论】:

    标签: php wordpress url meta-tags


    【解决方案1】:

    我有这个问题的潜在解决方案。我能够获取上面列出的“class-model-post-metas.php”文件的副本,并使用相同的目录结构将其放置在我的子主题中。然后我修改了文件以检查帖子“作者”的用户级别是否设置为订阅者。如果是这样,我去掉了href。否则,执行正常行为。

    这样做的原因是,当添加一个名为 CM Answers Pro 的用户论坛插件时,用户可以使用社交登录 (oAuth) 登录和注册。我默认订阅者级别的所有新注册。所以我所有的论坛参与者都在订阅者级别举行。这就是最初造成这个问题的原因。每次向论坛添加新帖子时,用户都会被标记为帖子的“作者”。即使他们没有作者级别的权限。

    当显示来自问答论坛的单个帖子时,Customizr 会为“作者”姓名和进入日期设置帖子元数据,包括链接。这些链接会创建 404,因为它们是在假设问答论坛中的每个帖子都是博客帖子的情况下构建的。

    当用户设置为订阅者级别时,修改下面的代码可以正确地从帖子元数据中删除链接。对以下两个函数进行了修改:czr_fn_get_meta_date() 和 czr_fn_get_meta_author()。您可以将其与 Customizr GitHub 存储库中提供的原始源代码进行比较。 https://github.com/presscustomizr/customizr

    <?php
    class CZR_post_metas_model_class extends CZR_Model {
    
    
      /* PUBLIC GETTERS */
      public function czr_fn_get_cat_list( $limit = false, $sep = '' ) {
        return 0 != esc_attr( czr_fn_opt( 'tc_show_post_metas_categories' ) ) ? $this -> czr_fn_get_meta( 'categories', $limit, $sep ) : '';
      }
    
      public function czr_fn_get_tag_list( $limit = false, $sep = '' ) {
        return 0 != esc_attr( czr_fn_opt( 'tc_show_post_metas_tags' ) ) ? $this -> czr_fn_get_meta( 'tags', $limit, $sep ) : '';
      }
    
      public function czr_fn_get_author( $before = null ) {
        return 0 != esc_attr( czr_fn_opt( 'tc_show_post_metas_author' ) ) ? $this -> czr_fn_get_meta( 'author', array( $before ) ) : '';
      }
    
      public function czr_fn_get_publication_date( $permalink = false, $before = null ) {
        return 0 != esc_attr( czr_fn_opt( 'tc_show_post_metas_publication_date' ) ) ? $this -> czr_fn_get_meta( 'pub_date', array(
            '',
            $permalink,
            $before = null ) ) : '';
      }
    
      public function czr_fn_get_update_date( $permalink = false, $before = null ) {
        return 0 != esc_attr( czr_fn_opt( 'tc_show_post_metas_update_date' ) ) &&
               false !== czr_fn_post_has_update() ?
                    $this -> czr_fn_get_meta( 'up_date', array( '', $permalink ) ) : '';
      }
      /* END PUBLIC GETTERS */
    
    
      /* HELPERS */
      protected function czr_fn_get_meta( $meta, $params = array(), $separator = '' ) {
    
        $params = is_array( $params ) ? $params : array( $params );
        return czr_fn_stringify_array( call_user_func_array( array( $this, "czr_fn_meta_generate_{$meta}" ), $params ), $separator );
    
      }
    
    
      private function czr_fn_meta_generate_categories( $limit = false ) {
        return $this -> czr_fn_meta_generate_tax_list( $hierarchical = true, $limit );
      }
    
      private function czr_fn_meta_generate_tags( $limit = false ) {
        return $this -> czr_fn_meta_generate_tax_list( $hierarchical = false, $limit );
      }
    
      private function czr_fn_meta_generate_author( $before ) {
        $author = $this -> czr_fn_get_meta_author();
        $before = is_null($before) ? __( 'by&nbsp;', 'customizr-child' ) :'';
        return '<span class="author-meta">' . $before . $author . '</span>';
      }
    
      private function czr_fn_meta_generate_pub_date( $format = '', $permalink = false, $before = null ) {
        $date   = $this -> czr_fn_get_meta_date( 'publication', $format, $permalink );
        $before = is_null($before) ? __( 'Published&nbsp;', 'customizr' ) :'';
    
        return $before . $date;
      }
    
      private function czr_fn_meta_generate_up_date( $format = '', $permalink = false, $before = null ) {
        $date   = $this -> czr_fn_get_meta_date( 'update', $format, $permalink );
        $before = is_null($before) ? __( 'Updated&nbsp;', 'customizr' ) :'';
    
        return $before . $date;
      }
    
    
      protected function czr_fn_get_term_css_class( $_is_hierarchical ) {
        $_classes = array();
    
        if ( $_is_hierarchical )
          array_push( $_classes , 'tax__link' );
        else
          array_push( $_classes , 'tag__link btn btn-skin-dark-oh inverted' );
    
        return $_classes;
      }
    
      /**
      * Helper
      * Return the date post metas
      *
      * @package Customizr
      * @since Customizr 3.2.6
      */
      protected function czr_fn_get_meta_date( $pub_or_update = 'publication', $_format = '', $permalink = false ) {
        if ( 'short' == $_format )
          $_format = 'j M, Y';
        $_format = apply_filters( 'czr_meta_date_format' , $_format );
        $_use_post_mod_date = apply_filters( 'czr_use_the_post_modified_date' , 'publication' != $pub_or_update );
        // get user level to be used to turn meta links on or off
        $subscriber_level = get_the_author_meta('user_level');
        if ($subscriber_level == 0) {
            // user is a subscriber
            return apply_filters(
                'tc_date_meta',
                 sprintf( '<span><time class="entry-date %3$s" datetime="%4$s">%5$s</time></span>' ,
                   $permalink ? esc_url( get_the_permalink() ) : esc_url( get_day_link( get_the_time( 'Y' ), get_the_time( 'm' ), get_the_time( 'd' ) ) ),
                   $permalink ? esc_attr( the_title_attribute( array( 'before' => __('Permalink to:&nbsp;', 'customizr'), 'echo' => false ) ) ) : esc_attr( get_the_time() ),
                   'publication' == $pub_or_update ? 'published updated' : 'updated',
                   $_use_post_mod_date ? esc_attr( get_the_modified_date('c') ) : esc_attr( get_the_date( 'c' ) ),
                   $_use_post_mod_date ? esc_html( get_the_modified_date( $_format ) ) : esc_html( get_the_date( $_format ) )
                 ),
                 $_use_post_mod_date,
                 $_format
            );//end filter
        } else {
            // user is not a subscriber
            return apply_filters(
              'tc_date_meta',
                sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date %3$s" datetime="%4$s">%5$s</time></a>' ,
                  $permalink ? esc_url( get_the_permalink() ) : esc_url( get_day_link( get_the_time( 'Y' ), get_the_time( 'm' ), get_the_time( 'd' ) ) ),
                  $permalink ? esc_attr( the_title_attribute( array( 'before' => __('Permalink to:&nbsp;', 'customizr'), 'echo' => false ) ) ) : esc_attr( get_the_time() ),
                  'publication' == $pub_or_update ? 'published updated' : 'updated',
                  $_use_post_mod_date ? esc_attr( get_the_modified_date('c') ) : esc_attr( get_the_date( 'c' ) ),
                  $_use_post_mod_date ? esc_html( get_the_modified_date( $_format ) ) : esc_html( get_the_date( $_format ) )
                ),
                $_use_post_mod_date,
                $_format
            );//end filter
        }
      }
    
      /**
      * Helper
      * Return the post author metas
      *
      * @package Customizr
      * @since Customizr 3.2.6
      */
    
      // LET'S SEE IF WE CAN UPDATE THIS FUNCTION TO CHECK IF 'AUTHOR' IS ACTUALLY AN AUTHOR
      // BECUASE POSTS FROM THE CM ANSWERS PLUGIN ARE SUBSCRIBERS
    
      private function czr_fn_get_meta_author() {
        $author_id = null;
        if ( is_single() )
            $subscriber_level = 0;
            if ( ! in_the_loop() ) {
                global $post;
                $author_id = $post->post_author;
            }
            $subscriber_level = get_the_author_meta('user_level');
            // if ($subscriber_level == 0) {
            //     print_r("Subscriber ");
            // } else {
            //     print_r("Not Subscriber ");
            // }
            if($subscriber_level == 0) {
                // user role is subscriber
                return apply_filters(
                    'tc_author_meta',
                    sprintf( '<span class="author vcard author_name">%3$s</span>' ,
                        esc_url( get_author_posts_url( get_the_author_meta( 'ID', $author_id ) ) ),
                        esc_attr( sprintf( __( 'View all posts by %s' , 'customizr' ), get_the_author_meta( 'display_name', $author_id ) ) ),
                        get_the_author_meta( 'display_name', $author_id )
                    )
                );//end filter
            } else {
                // user role is higher than subscriber level
                return apply_filters(
                  'tc_author_meta',
                  sprintf( '<span class="author vcard author_name"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>' ,
                      esc_url( get_author_posts_url( get_the_author_meta( 'ID', $author_id ) ) ),
                      esc_attr( sprintf( __( 'View all posts by %s' , 'customizr' ), get_the_author_meta( 'display_name', $author_id ) ) ),
                      get_the_author_meta( 'display_name', $author_id )
                  )
                );//end filter
            }
    
      }
    
    
      /**
      * Helper
      * @return string of all the taxonomy terms (including the category list for posts)
      * @param  hierarchical tax boolean => true = categories like, false = tags like
      *
      * @package Customizr
      * @since Customizr 3.0
      */
      private function czr_fn_meta_generate_tax_list( $hierarchical, $limit = false ) {
        $post_terms = $this -> czr_fn_get_term_of_tax_type( $hierarchical, $limit );
        if ( ! $post_terms )
          return;
        $_terms_html_array  = array_map( array( $this , 'czr_fn_meta_term_view' ), $post_terms );
        return $_terms_html_array;
      }
    
    
      /**
      * Helper
      * @return string of the single term view
      * @param  $term object
      *
      * @package Customizr
      * @since Customizr 3.3.2
      */
      private function czr_fn_meta_term_view( $term ) {
        $_is_hierarchical  =  is_taxonomy_hierarchical( $term -> taxonomy );
    
        $_classes      = czr_fn_stringify_array( apply_filters( 'czr_meta_tax_class', $this -> czr_fn_get_term_css_class( $_is_hierarchical ), $_is_hierarchical, $term ) );
    
    
        // (Rocco's PR Comment) : following to this https://wordpress.org/support/topic/empty-articles-when-upgrading-to-customizr-version-332
        // I found that at least wp 3.6.1  get_term_link($term->term_id, $term->taxonomy) returns a WP_Error
        // Looking at the codex, looks like we can just use get_term_link($term), when $term is a term object.
        // Just this change avoids the issue with 3.6.1, but I thought should be better make a check anyway on the return type of that function.
        $_term_link    = is_wp_error( get_term_link( $term ) ) ? '' : get_term_link( $term );
        $_to_return    = $_term_link ? '<a %1$s href="%2$s" title="%3$s"> <span>%4$s</span> </a>' :  '<span %1$s> %4$s </span>';
        $_to_return    = $_is_hierarchical ? $_to_return : '<li>' . $_to_return . '</li>';
        return apply_filters( 'czr_meta_term_view' , sprintf($_to_return,
            $_classes ? 'class="'. $_classes .'"' : '',
            $_term_link,
            esc_attr( sprintf( __( "View all posts in %s", 'customizr' ), $term -> name ) ),
            $term -> name
          )
        );
      }
    
    
      /**
      * Helper to return the current post terms of specified taxonomy type : hierarchical or not
      *
      * @return boolean (false) or array
      * @param  boolean : hierarchical or not
      * @package Customizr
      * @since Customizr 3.1.20
      *
      */
      private function czr_fn_get_term_of_tax_type( $hierarchical = true, $limit = false ) {
        //var declaration
        $post_type              = get_post_type( czr_fn_get_id() );
        $tax_list               = get_object_taxonomies( $post_type, 'object' );
        $_tax_type_list         = array();
        $_tax_type_terms_list   = array();
    
        if ( empty($tax_list) )
          return false;
    
        //filter the post taxonomies
        while ( $_tax_object = current($tax_list) ) {
          // cast $_tax_object stdClass object in an array to access its property 'public'
          // fix for PHP version < 5.3 (?)
          $_tax_object = (array) $_tax_object;
          //Is the object well defined ?
          if ( ! isset($_tax_object['name']) ) {
            next($tax_list);
            continue;
          }
          $_tax_name = $_tax_object['name'];
          //skip the post format taxinomy
          if ( ! $this -> czr_fn_is_tax_authorized( $_tax_object, $post_type ) ) {
            next($tax_list);
            continue;
          }
          if ( (bool) $hierarchical === (bool) $_tax_object['hierarchical'] )
            $_tax_type_list[$_tax_name] = $_tax_object;
    
          next($tax_list);
        }
        if ( empty($_tax_type_list) )
          return false;
    
        $found = 0;
    
        //fill the post terms array
        foreach ($_tax_type_list as $tax_name => $data ) {
          $_current_tax_terms = get_the_terms( czr_fn_get_id() , $tax_name );
          //If current post support this tax but no terms has been assigned yet = continue
          if ( ! $_current_tax_terms )
            continue;
          while( $term = current($_current_tax_terms) ) {
            $_tax_type_terms_list[$term -> term_id] = $term;
            if ( $limit > 0 && ++$found == $limit )
              break 2;
            next($_current_tax_terms);
          }
        }
    
        /*if ( ! empty($_tax_type_terms_list) && $limit > 0 )
          $_tax_type_terms_list = array_slice( $_tax_type_terms_list, 0, $limit );
    */
        return empty($_tax_type_terms_list) ? false : apply_filters( "czr_tax_meta_list" , $_tax_type_terms_list , $hierarchical );
      }
    
      /**
      * Helper : check if a given tax is allowed in the post metas or not
      * A tax is authorized if :
      * 1) not in the exclude list
      * 2) AND not private
      *
      * @return boolean (false)
      * @param  $post_type, $_tax_object
      * @package Customizr
      * @since Customizr 3.3+
      *
      */
      private function czr_fn_is_tax_authorized( $_tax_object , $post_type ) {
        $_in_exclude_list = in_array(
          $_tax_object['name'],
          apply_filters_ref_array ( 'czr_exclude_taxonomies_from_metas' , array( array('post_format') , $post_type , czr_fn_get_id() ) )
        );
        $_is_private = false === (bool) $_tax_object['public'] && apply_filters_ref_array( 'czr_exclude_private_taxonomies', array( true, $_tax_object['public'], czr_fn_get_id() ) );
        return ! $_in_exclude_list && ! $_is_private;
      }
    
    
      /* Customizer: allow dynamic visibility in the preview */
      function czr_fn_body_class( $_classes/*array*/ ) {
        if ( ! czr_fn_is_customizing() )
          return $_classes;
    
        if ( 0 == esc_attr( czr_fn_opt( 'tc_show_post_metas' ) ) )
           $_classes[] = 'hide-all-post-metas';
    
        if (
            ( is_singular() && ! is_page() && ! czr_fn_is_real_home() && 0 == esc_attr( czr_fn_opt( 'tc_show_post_metas_single_post' ) ) ) ||
            ( ! is_singular() && ! czr_fn_is_real_home() && ! is_page() && 0 == esc_attr( czr_fn_opt( 'tc_show_post_metas_post_lists' ) ) ) ||
            ( czr_fn_is_real_home() ) && 0 == esc_attr( czr_fn_opt( 'tc_show_post_metas_home' ) )
        )
          $_classes[] = 'hide-post-metas';
    
        return $_classes;
      }
    }//end of class
    

    【讨论】:

      猜你喜欢
      • 2018-10-23
      • 2019-06-16
      • 2013-09-21
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      • 2013-10-21
      相关资源
      最近更新 更多