Php WP自定义主题
我在几周前添加了这段代码,但昨晚它突然停止工作,这怎么可能呢?它可以在本地服务器上正常工作,但不能在共享主机上正常工作。Local运行的是PHP5.5.3,host运行的是5.4,但我刚刚请求将该特定子域升级到5.5,以防出现问题 使用子主题。父主题创建一个名为Portfolio的自定义帖子类型,其中所有帖子的slug为/Portfolio item/。在我的孩子主题中,我增加了两件事:1。一个新的自定义帖子类型叫做Hidden Pages,我使用了一个我在网上找到的代码来删除slug,所以它就是domain.com/page-name。2.我添加了更多关于公文包类型的代码,这也删除了slug 一切都很完美,但突然之间,所有标准页面404和404都隐藏在后端,但不在数据库中,这不是数据库问题!,而公文包帖子会神奇地移动到博客的常规标准帖子中。如果注释掉第132行的custom_pre_get_posts,问题就解决了。但是客户端需要URL是干净的,没有slug。所以我不想让它保持原样 下面是子主题的functions.php:Php WP自定义主题,php,wordpress,custom-post-type,slug,Php,Wordpress,Custom Post Type,Slug,我在几周前添加了这段代码,但昨晚它突然停止工作,这怎么可能呢?它可以在本地服务器上正常工作,但不能在共享主机上正常工作。Local运行的是PHP5.5.3,host运行的是5.4,但我刚刚请求将该特定子域升级到5.5,以防出现问题 使用子主题。父主题创建一个名为Portfolio的自定义帖子类型,其中所有帖子的slug为/Portfolio item/。在我的孩子主题中,我增加了两件事:1。一个新的自定义帖子类型叫做Hidden Pages,我使用了一个我在网上找到的代码来删除slug,所以它就
<?php
function evol_child_scripts() {
wp_enqueue_style( 'style', get_stylesheet_directory_uri() . '/stylesheets/css/style.css' );
}
add_action( 'wp_enqueue_scripts', 'evol_child_scripts' );
/*function codex_custom_init() {
$args = array(
'public' => true,
'label' => 'Hidden Pages'
);
register_post_type( 'hidden', $args );
}
add_action( 'init', 'codex_custom_init' );
*/
/**
* Register a custom post type but don't do anything fancy
*/
register_post_type( 'hidden', array( 'label' => 'Hidden Pages', 'public' => true, 'capability_type' => 'post', 'show_ui' => true, 'query_var' => true, 'supports' => array( 'title', 'editor', 'thumbnail' ) ) );
/**
* Remove the slug from published post permalinks. Only affect our CPT though.
*/
function vipx_remove_cpt_slug( $post_link, $post, $leavename ) {
if ( ! in_array( $post->post_type, array( 'hidden' ) )
|| 'publish' != $post->post_status )
return $post_link;
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'vipx_remove_cpt_slug', 10, 3 );
/**
* Some hackery to have WordPress match postname to any of our public
* post types. All of our public post types can have /post-name/ as
* the slug, so they better be unique across all posts. Typically core
* only accounts for posts and pages where the slug is /post-name/
*/
function vipx_parse_request_tricksy( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() )
return;
// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query )
|| ! isset( $query->query['page'] ) )
return;
// 'name' will be set if post permalinks are just post_name,
// otherwise the page rule will match
if ( ! empty( $query->query['name'] ) )
$query->set( 'post_type', array( 'post', 'hidden', 'page' ) );
}
add_action( 'pre_get_posts', 'vipx_parse_request_tricksy' );
function remove_search_filter () {
remove_filter( 'pre_get_posts', 'tr_search_filter' );
}
add_action( 'init', 'remove_search_filter');
function remove_icons() {
wp_dequeue_style( 'icons' );
}
add_action( 'wp_enqueue_scripts', 'remove_icons' );
function tr_widgets_init_2() {
if ( ot_get_option( 'tr_sidebars' ) ) :
$tr_sidebars = ot_get_option( 'tr_sidebars' );
foreach ( $tr_sidebars as $tr_sidebar ) {
register_sidebar( array(
'id' => $tr_sidebar["id"],
'name' => $tr_sidebar["title"],
'before_widget' => '<div class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h6 class="widget-title">',
'after_title' => '</h6>',
));
}
endif;
};
add_action( 'widgets_init', 'tr_widgets_init_2' );
add_filter('post_type_link','custom_post_type_link', 10, 3);
function custom_post_type_link($permalink, $post, $leavename) {
$url_components = parse_url($permalink);
$post_path = $url_components['path'];
$post_name = end((explode('/', trim($post_path, '/'))));
if(!empty($post_name)) {
switch($post->post_type) {
case 'portfolio':
$permalink = str_replace($post_path, '/' . $post_name . '/', $permalink);
break;
}
}
return $permalink;
}
function custom_pre_get_posts($query) {
global $wpdb;
if(!$query->is_main_query()) {
return;
}
$post_name = $query->get('name');
$post_type = $wpdb->get_var( $wpdb->prepare( 'SELECT post_type FROM ' . $wpdb->posts . ' WHERE post_name = %s LIMIT 1', $post_name ) );
switch($post_type) {
case 'portfolio':
$query->set('portfolio', $post_name);
$query->set('post_type', $post_type);
$query->is_single = true;
$query->is_page = false;
break;
}
return $query;
}
add_action('pre_get_posts','custom_pre_get_posts');
function fb_add_search_box ( $items, $args ) {
// only on primary menu
if( 'primary' === $args -> theme_location )
$items .= '<li class="menu-item menu-item-search">' . get_search_form( FALSE ) . '</li>';
return $items;
}
add_filter( 'wp_nav_menu_items', 'fb_add_search_box', 10, 2 );
// Callback function to insert 'styleselect' into the $buttons array
function my_mce_buttons_2( $buttons ) {
array_unshift( $buttons, 'styleselect' );
return $buttons;
}
// Register our callback to the appropriate filter
add_filter('mce_buttons_2', 'my_mce_buttons_2');
// Callback function to filter the MCE settings
function my_mce_before_init_insert_formats( $init_array ) {
// Define the style_formats array
$style_formats = array(
// Each array child is a format with it's own settings
array(
'title' => 'Image Text',
'block' => 'p',
'classes' => 'image-text',
),
array(
'title' => 'Image Text no Overlay',
'block' => 'p',
'classes' => 'image-text-2',
),
array(
'title' => 'w/ gray background',
'inline' => 'span',
'exact' => true,
'classes' => 'gray-background',
),
array(
'title' => 'w/ white background',
'inline' => 'span',
'exact' => true,
'classes' => 'white-background',
),
array(
'title' => 'Image Text no Indent',
'block' => 'p',
'classes' => 'image-text-3',
),
array(
'title' => 'Button',
'inline' => 'a',
'exact' => true,
'classes' => 'button small',
),
array(
'title' => 'Blog Image Caption',
'block' => 'p',
'classes' => 'caption',
),
array(
'title' => 'Signature',
'inline' => 'span',
'exact' => true,
'classes' => 'sign',
),
);
// Insert the array, JSON ENCODED, into 'style_formats'
$init_array['style_formats'] = json_encode( $style_formats );
return $init_array;
}
// Attach callback to 'tiny_mce_before_init'
add_filter( 'tiny_mce_before_init', 'my_mce_before_init_insert_formats' );
这是指这一行:
$post_name = end(explode('/', trim($post_path, '/')));
这仍然在错误日志中,但更改后,它不在管理端,这是否意味着它已修复
子主题代码来自此处:和
所以,如果有人知道如何修改这个文件,使一切恢复到完美工作状态,那就太好了。实际上,我们离发射还有几天。Eep。谢谢大家! 问题不在于Wordpress,正如错误所述,这都是纯PHP造成的。您应该对带括号的换行分解进行一些更改:
考虑到我的PHP水平,并且不想重写同时涉及父主题和子主题函数的代码,我找不到解决这个问题的方法。我最终删除了所有试图删除slug的代码。我祈祷并安装了这个过时的插件,它成功了。因此,对于将来参考的任何人,我想您可以避免使用代码,而只使用此代码
嗨,雷沃!非常感谢,这确实消除了错误,所以现在我们没有错误了。但不幸的是,它没有解决问题。仍然在乱七八糟的页面上搜索。下一步怎么办?我是应该更新上面的代码以反映这一变化,还是应该保留它?@Alicia同时设置WP_DEBUG和WP_DEBUG_LOG。但是,您已经启用了WP_调试,您需要在之后添加这一行:定义'WP_调试日志',true;。然后转到您的网站以获取要创建的日志,并检查/wp content/目录中的debug.log日志文件。最好也在你的帖子中添加一个更新。好的,如果你是指错误日志,这里是错误:PHP致命错误:无法重新声明vipx\u remove\u cpt\u slug,之前在evol child/functions.PHP的第294行声明。PHP:26在evol/functions.PHP中,这是指我猜我一定已经将该部分添加到父主题的函数中,我也是。@Alicia就是这样!您已经在父模板中声明了该函数,不需要重新声明。好的问题是我只是查看并。。。它不在那里。让我添加parent functions.php。
Strict Standards: Only variables should be passed by reference in
[path redacted]/functions.php on line 97
$post_name = end(explode('/', trim($post_path, '/')));
$post_name = end((explode('/', trim($post_path, '/'))));