1

404 エラー ページのコンテンツ(つまり、ヘッダーまたはフッターではない)を動的に置き換えるために、Wordpress プラグインで使用できるアクションまたはフィルターは何ですか?

the_content 基本的に、 filterに相当する 404 エラー ページを探しています。これは、既存のページのコンテンツをフィルター処理します。

お時間をいただきありがとうございます。

注:現在のテーマの 404 エラー ページを手動で変更できることはわかっていますが、それは私が達成しようとしている効果ではありません。

4

3 に答える 3

1

条件付きセクションでthe_contentフィルターを追加できる場合があります。is_404

function content_404($content) {
  if (is_404()) {
    // do some stuff with $content
  }
  // no matter what,
  return $content;
} 

add_filter( 'the_content', 'content_404' );

これは、404.phpページ テンプレートにthe_contenttemplate タグが配置されていることを前提としていることに注意してください。

于 2013-01-26T19:14:41.810 に答える
1

解決策は、404.php ファイルの内容によって異なります。このファイルに次のような静的テキストが含まれている場合

_e( 'It seems we can’t find what you’re looking for...', 'twentyeleven' );

独自のフィルターを追加できます

apply_filters( 'my_404_content', 'Default 404 message' );

および functions.php (またはプラグイン)

add_filter( 'my_404_content', 'replace_404_message' );
function replace_404_message($message) {
    return 'Error 404 - '.$message;
}

404.php がビルトイン WP 関数を使用してページ コンテンツを表示する場合、それらがサポートされているフィルターを確認する必要があります。

于 2013-01-26T19:09:49.620 に答える
1

このWordPressの回答から:テーマを変更せずにカスタム投稿タイプの出力を制御する方法は?

プラグイン ファイル:

<?php
/*
Plugin Name: Plugin 404 Page
Plugin URI: http://stackoverflow.com/questions/14539884
Description: Use the plugin's template file to render a custom 404.php
Author: brasofilo
Author URI: https://wordpress.stackexchange.com/users/12615/brasofilo
Version: 2013.26.01
License: GPLv2
*/
class Universal_Template
{
    public function __construct()
    {       
        $this->url = plugins_url( '', __FILE__ );   
        $this->path = plugin_dir_path( __FILE__ );
        add_action( 'init', array( $this, 'init' ) );
   }

    public function init() 
    {
        add_filter( 'template_include', array( $this, 'template_404' ) );
    }

    public function template_404( $template ) 
    {
        if ( is_404() )
            $template = $this->path . '/404.php';

        return $template;
    }
}

$so_14539884 = new Universal_Template();

また、プラグイン フォルダーには、次の名前のファイルがあります404.php

<?php
/**
 * The template for displaying 404 pages (Not Found).
 *
 * @package WordPress
 * @subpackage Twenty_Twelve
 * @since Twenty Twelve 1.0
 */

get_header(); ?>

    <div id="primary" class="site-content">
        MY 404!
    </div><!-- #primary -->

<?php get_footer(); ?>
于 2013-01-26T19:31:06.477 に答える