我正在尝试使用get\\u post\\u permalink()获取自定义帖子类型的permalink。我需要使用get\\u post\\u permalink(),因为我使用post\\u type\\u链接过滤器将特定post类型的permalink更改为其他类型。下面是我正在尝试运行的代码:
class Get_Department_Class {
/**
* The department\'s post ID.
*
* @since 1.0
* @access public
* @var int $ID Holds the department\'s post ID
*/
public $ID;
/**
* Will hold the department\'s post data.
*
* @since 1.0
* @access public
* @var array $post Holds the department\'s post data
*/
public $post;
/**
* Will hold the department\'s permalink.
*
* @since 1.0
* @access protected
* @var string $permalink Holds the department\'s permalink
*/
protected $permalink;
/**
* Build/construct the department.
*
* @since 1.0
* @param $post_id - allows you to pass the department\'s post ID or post title
*/
public function __construct( $post_id ) {
global $wpdb, $blog_id;
// If not main site, change $wpdb blog ID for queries
// Will change back when we\'re done
$old_blog_id = ( 1 != $blog_id ) ? $wpdb->set_blog_id( 1 ) : NULL;
// Check against an ID
if ( is_numeric( $post_id ) ) {
$this->post = get_post( $post_id );
}
// Check against a post title
if ( ! $this->post ) {
$this->post = get_page_by_title( $post_id, OBJECT, \'departments\' );
}
// Reset the blog ID
if ( isset( $old_blog_id ) )
$wpdb->set_blog_id( $old_blog_id );
// No point if there\'s no post ID
if ( ! ( $this->ID = isset( $this->post ) && isset( $this->post->ID ) ? $this->post->ID : NULL ) ) {
$this->post = NULL;
return;
}
}
/**
* Get the department\'s permalink.
*
* @since 1.0
*/
public function get_permalink() {
// Make sure we have the post ID
if ( ! $this->ID )
return false;
// See if the permalink is already set
if ( isset( $this->permalink ) && ! empty( $this->permalink ) )
return $this->permalink;
// Get the permalink
if ( $permalink = get_post_permalink( $this->ID ) ) {
// Store the permalink
$this->permalink = $permalink;
return $this->permalink;
}
return false;
}
}
返回的错误是“注意:在第266行的/Applications/MAMP/htdocs/wp includes/link-template.php中尝试获取非对象的属性。”在wordpress核心中进行一些测试之后,在链接模板中的第264行。php文件返回NULL。
第264行:
$post_type = get_post_type_object($post->post_type);
然后在第266行,核心代码尝试访问$post\\u类型对象的属性,该属性为NULL,因此抛出错误。
我不想修改核心代码。有什么办法可以解决这个问题吗?
仅供参考:我正在运行WP 4.5.9。。。