• I’m currently getting a ‘Cannot Redeclare get_header() error’ (see below) when I try pulling posts onto an external page using the following code:

    <?php
    /* Short and sweet */
    define('WP_USE_THEMES', false);
    require('./lsutv/wp-blog-header.php');
    ?>
    
    <?php
    global $post;
    $args = array( 'posts_per_page' => 4 );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" class="video_title" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a><br />
    <?php the_excerpt(); ?><hr/>
    <?php endforeach; ?></div>

    Here’s the error:

    PHP Fatal error: Cannot redeclare get_header() (previously declared in main\media\label\core\init.php:51) in main\media\lsutv\wp-includes\general-template.php on line 36

    When the code is used as a stand alone php file, it pulls the posts perfectly, but as soon as I add it to the page I need the posts to show on (a page ‘above’ the WP site). There anything I can try / do?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Change this:

    require('./lsutv/wp-blog-header.php');

    to this:

    require_once('./lsutv/wp-blog-header.php');

    I have a feeling that when you’re trying to run that inside the WordPress system, you’re trying to include the wp-blog-header.php file twice as it’s already included by WordPress before it gets to your file, and that’s what’s giving you that error.

    Thread Starter danleedham

    (@danleedham)

    Thanks for the feedback although when I do change it to require once, it still gives the error. Having a little poke around, it appears a college has use the function get_header & get_footer before:

    function get_header($title) {
    
    	include("header.php");

    I’ve renamed these functions to get_AREANAME_header, and now it all works just fine. Makes me wonder what I’d do if I had various WP sites within the big site and I wanted to pull all their headers – would I have to use a multisite site?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Cannot redeclare get_header() on External Page post call’ is closed to new replies.