使用functions.php中的add_filter将类添加到Wordpress post_class

时间:2015-08-26 18:31:42

标签: php wordpress custom-post-type add-filter

我正在尝试为某个类型的所有帖子添加一个额外的类 - 我将这个添加到functions.php后,我正在获得一个空白页

我只需要在$ classes包含字符串“toefl_post”的地方添加类“post”。

这是我的代码:

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*

public class GameClass extends JPanel
        implements ActionListener, KeyListener {
    private static final long serialVersionUID = -2508183917768834794L;

    private Image image;

    // Added this, because you did not include it.
    private class Main {
        static final int WW = 256;
        static final int WH = 256;
    }

    public GameClass() {
        Timer time = new Timer(15, this);
        time.start();
        this.addKeyListener(this);
        this.setFocusable(true);

        this.loadImages();
    }

    // Load all required images into instance variables.
    public void loadImages() {
        image = loadImage("spaceship.png");
    }

    // Draw the image to the panel.
    public void paintComponent(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, Main.WW, Main.WH);

        // Dimensions of spaceship
        int imgW = image.getWidth(null);
        int imgH = image.getHeight(null);

        // Dimensions of panel
        int pnlW = this.getWidth();
        int pnlH = this.getHeight();

        // Draw the spaceship in the center of the window.
        g.drawImage(image, pnlW/2 - imgW/2, pnlH/2 - imgH/2, null);
    }

    public void actionPerformed(ActionEvent e) {
        repaint();
    }

    public void keyPressed(KeyEvent e) { }

    public void keyReleased(KeyEvent e) { }

    public void keyTyped(KeyEvent e) { }

    // Static image loader method which utilizes the `ClassLoader`.
    public static Image loadImage(String filename) {
        try {
            return ImageIO.read(GameClass.class.getClassLoader().getResource("resources/" + filename));
        } catch (IOException e) {
            System.out.println("Error loading image: " + filename);
        }

        return null;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                Container panel = new GameClass();

                frame.setSize(Main.WW, Main.WH);
                frame.setTitle("Spaceship Game");
                frame.setContentPane(panel);
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

我对PHP很陌生,所以我猜测代码有问题 - 非常感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

是的,那里有很多语法问题。

1.- $ classes是一个数组,而不是一个字符串

2.-您不能使用+来连接字符串,您应该使用.

3.-要将键添加到数组中,您应该使用array_push

试试这个:

function add_post_class($classes) {
    $additional = 'post';
    foreach ($classes as $class) {
        if( $class == 'toefl_post'){
            array_push($classes , $additional);
            break;
        }
    }
    return $classes;
}
add_filter('post_class', 'add_post_class');

答案 1 :(得分:1)

您的问题在$additional = 'post'

您错过了;

还有其他语法错误,如Enrique Chavez所说。

答案 2 :(得分:1)

答案有效,但这个更简单:

add_filter('post_class','my_class');
function my_class( $classes ) {
  $classes[] = 'my-class-name';
  return $classes;
}