获取插件类方法中的POST内容

时间:2019-03-09 作者:ZWPDev

我正在编写一个简单的SEO类,它将向wordpress的标题添加各种元标记。我对wordpress api有两个小问题。首先,我需要从标记列表中删除最后一个逗号,我已经尝试了rtrim 但没有成功。第二个问题是,我无法加载帖子或页面的内容来创建描述元标记的内容。如何解决这两个问题?

<?php

/*
* Plugin Name: Custom Meta Tag
*/

class WP_Custom_Meta_Tag{

  private $post_tags;
  private $key;
  private $keyword;
  private $description;

  public function init(){
    add_action(\'wp_head\', array($this, \'add_meta_description\'));
    add_action(\'wp_head\', array($this, \'add_meta_keywords\'));
  }

  /*
  * metatag keywords
  */
  public function add_meta_keywords(){
    if( is_single() ){
      $post_tags = get_the_tags( $post->ID );
      $key = \'\';
      foreach( $post_tags as $keyword ){
        $key .= $keyword->name.\', \';
      }
      echo \'<meta name="keywords" content="\'.$key.\'">\';
    }

  }

  /*
  * metatag description
  */
  public function add_meta_description(){
    if( is_single() ){

      #$description = strip_tags( $post->post_content );
      $d = get_the_content( $post->ID );
      #$description = strip_shortcodes( $post->post_content );
      #$description = str_replace( array("\\n", "\\r", "\\t"), \' \', $description );
      #$description = substr( $description, 0, 125 );

      #echo \'<meta name="description" content="\'. $description .\'">\';
      var_dump($d);
    }
  }

}

$meta = new WP_Custom_Meta_Tag;
$meta->init();
?> 
EDIT

我已经解决了这个问题,现在一切似乎都正常了。如果有人建议更好地删减描述文字,请分享答案。我读过wp_trim_words, 但这行不通。以下是更新的代码:

class WP_Custom_Meta_Tag{

  private $post_tags;
  private $key;
  private $keyword;
  private $description;

  public function init(){
    add_action(\'wp_head\', array($this, \'add_metatags\'), 1);
  }

  /*
  * metatag keywords
  */
  public function add_metatags(){
    global $post;
    if( is_single() || is_page() ){
      $post_tags = get_the_tags( $post->ID );
      $key = [];
      foreach( $post_tags as $keyword ){
        $key[] = $keyword->name;
      }
      // this part needs to be improved
      $description = strip_tags( $post->post_content );
      $description = strip_shortcodes( $post->post_content );
      $description = str_replace( array("\\n", "\\r", "\\t"), \' \', $description );
      $description = substr( $description, 0, 140 );

      echo \'<meta name="description" content="\'. $description .\'" />\';
      echo \'<meta name="keywords" content="\'.implode(\',\' ,$key).\'" />\';
      echo \'<meta property="og:title" content="\'. get_the_title( $post->ID ) .\'" />\';
      echo \'<meta property="og:description" content="\'. $description .\'"/>\';
      #echo \'<meta property="og:type" content="" />\';
      echo \'<meta property="og:url" content="\'. get_the_permalink( $post->ID ) .\'" />\';
      if( has_post_thumbnail() ){
        echo \'<meta property="og:image" content="\'. get_the_post_thumbnail_url( $post->ID , \'full\') .\'" />\';
      }
    }
  }

}

$meta = new WP_Custom_Meta_Tag;
$meta->init();
?>

1 个回复
SO网友:Qaisar Feroz

我需要从标记列表中删除最后一个逗号

全部放置$keyword->name 放入数组并使用implode() 以获得结果。

我无法加载帖子或页面的内容

对象$post 在您的函数中不可用。使用global $post; 访问$post->ID$post->post_content

所以

public function add_meta_keywords(){
    if( is_single() ){
      $post_tags = get_the_tags( $post->ID );
      $key = \'\';
      foreach( $post_tags as $keyword ){
        $key .= $keyword->name.\', \';
      }
      echo \'<meta name="keywords" content="\'.$key.\'">\';
    }

  }
应该是这样的

public function add_meta_keywords(){
    if( is_single() ){

      global $post;

      $post_tags = get_the_tags( $post->ID );
      $key = array();
      foreach( $post_tags as $keyword ){
        $key []= $keyword->name;
      }
      echo \'<meta name="keywords" content="\'.implode(\', \',$key).\'">\';
    }

  }
以及

public function add_meta_description(){
    if( is_single() ){

      #$description = strip_tags( $post->post_content );
      $d = get_the_content( $post->ID );
      #$description = strip_shortcodes( $post->post_content );
      #$description = str_replace( array("\\n", "\\r", "\\t"), \' \', $description );
      #$description = substr( $description, 0, 125 );

      #echo \'<meta name="description" content="\'. $description .\'">\';
      var_dump($d);
    }
  }
应该是

public function add_meta_description(){

    global $post;

    if( is_single() ){
      global $post;

      #$description = strip_tags( $post->post_content );
      $d = get_the_content( $post->ID );
      #$description = strip_shortcodes( $post->post_content );
      #$description = str_replace( array("\\n", "\\r", "\\t"), \' \', $description );
      #$description = substr( $description, 0, 125 );

      #echo \'<meta name="description" content="\'. $description .\'">\';
      var_dump($d);
    }
  }
EDIT:

更好的解决方案是将描述缩短到140个字符。

试试这个

// Checks the content\'s length, if more 140 chars...
// Output  140 chars, but don\'t break a word

if ( strlen($description) > 140){

    $description = substr($description, 0, strpos($description, \' \', 140));
}

echo \'<meta name="description" content="\'. $description .\'">\';
我希望这有帮助。

相关推荐

如何清理wp-content/plugins/wordpress-seo中不必要的文件?

我目前维护Wordpress网站7年。磁盘使用率最近已满,因此我需要定期检查自动备份并保存这些备份,然后将其从服务器中删除。当我检查时,插件文件夹中的wordpress seo文件夹(这是Yoast seo默认插件文件夹)占用了太多空间。据我所知,它充满了各种版本的插件文件。确切地说,它位于wordpress seo插件的js/dist文件夹中。它包含几种类型的小型javascript,每个名称的末尾都有某种序列或版本号,总容量约为250-300 MB。我可以清理这些文件吗?如果是,是否安全?如果我从以前