我有一项有趣的任务。 我为wordpress编写了自定义404处理程序并提取了一个URL。 在做了一些逻辑后,我有一个wordpress帖子ID,我需要显示而不是404页面。
如何显示wordpress帖子页面而不是404页面? 我唯一能想到的就是做
TypeError: Cannot set property 'route' of null
at restangularizeBase (http://localhost:8080/Students/app/lib/restangular.js:638:56)
at restangularizeElem (http://localhost:8080/Students/app/lib/restangular.js:788:35)
at http://localhost:8080/Students/app/lib/restangular.js:882:38
at http://localhost:8080/Students/app/lib/lodash.js:3877:29
at eval (eval at createIterator (http://localhost:8080/Students/app/lib/lodash.js:1778:21), <anonymous>:20:9)
at Function.map (http://localhost:8080/Students/app/lib/lodash.js:3876:9)
at http://localhost:8080/Students/app/lib/restangular.js:878:45
at deferred.promise.then.wrappedCallback (http://localhost:8080/Students/app/lib/angular/angular.js:10689:81)
at deferred.promise.then.wrappedCallback (http://localhost:8080/Students/app/lib/angular/angular.js:10689:81)
at http://localhost:8080/Students/app/lib/angular/angular.js:10775:26
但有没有其他方法可以做到这一点? 感谢
答案 0 :(得分:3)
我无法通过重写模板的404.php来做到这一点。而且我认为那将是模板依赖的。 相反,我设法使用template_redirect操作显示帖子。 插件函数的代码如下所示:
function func_404_redirect($query){
global $wp_query;
$post = get_post(2);
$wp_query->queried_object = $post;
$wp_query->is_single = true;
$wp_query->is_404 = false;
$wp_query->queried_object_id = $post->ID;
$wp_query->post_count = 1;
$wp_query->current_post=-1;
$wp_query->posts = array($post);
}
add_filter('template_redirect','func_404_redirect');
答案 1 :(得分:1)
我在主题中使用的代码:
<?php
global $my_theme;
$content_id = $my_theme->option( OPT_GENERAL, '404_page_id', TRUE );
$my_theme->prepare( $content_id, '404' );
get_header();
?>
<!-- [BEGIN 404] -->
<div class="row">
<?php
get_sidebar( 'left' );
?>
<div id="primary" class="content-area <?php echo $class; ?>">
<main id="main" class="site-main" role="main">
<section class="error-404 not-found">
<?php
// Load the content from the selected page
$content_loaded = FALSE;
if( $content_id > 0 )
{
$query = new WP_Query( array( 'page_id' => $content_id ) );
while( $query->have_posts() )
{
$query->the_post();
get_template_part( 'content', 'page' );
$content_loaded = TRUE;
}
wp_reset_postdata();
}
// Fallback content
if( !$content_loaded )
{
?>
<header class="page-header">
<h4 class="page-title well text-center"><?php _e( 'Page not found', 'my_theme' ); ?></h4>
</header>
<div class="page-content alert alert-danger text-center">
<p><?php _e( 'It looks like nothing was found at this location', 'my_theme' ); ?></p>
</div>
<?php
}
?>
</section>
</main>
</div>
<?php get_sidebar( 'right' ); ?>
</div>
<?php get_footer(); ?>
<!-- [END 404] -->
答案 2 :(得分:0)
您可以在主题(或儿童)中创建404.php页面而不是404处理程序,请参阅reference - 从那里您可以执行任何您喜欢的操作:加载帖子列表,加载单个帖子等等。