• Hi there,

    I have found a code for display author-x.php in author.php if the file exists and using the current author ID.

    if(! file_exists('author-'.$curauth->ID.'.php'))
    {
        include('author-'.$curauth->ID.'.php');
    }
    else
    {
        //Default author page
    }

    But that is for author.php file. I´m looking for a way to the same but in single.php pages and for categories, not only authors. Something like:

    if(! file_exists('extra-'.author_ID.'.php'))
    {
        include('extra-'.author_ID.'.php');
    }
    else
    {
        //anything else...
    }

    I tried everything, just don´t know how to input the author ID or category ID into include code, so the filenames are generated. Can´t find anything about just simple include(‘file.php’).

    Thanks for any help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • MichaelH

    (@michaelh)

    In theory, for a single post, the author id would be available with $posts[0]->post_author.

    Thread Starter Diana K. Cury

    (@dianakc)

    Im gonna try that! Thank you!

    Thread Starter Diana K. Cury

    (@dianakc)

    Hello again!

    I tried that in single.php:

    <?php if (! file_exists('aut-'.$post[0]->post-author.'.php')) { ?>
    <?php include('aut-'.$post[0]->post-author.'.php'); ?>
    <?php } ?>

    And got that:

    Fatal error: Cannot use object of type stdClass as array in … on line 16

    Oh!

    Thread Starter Diana K. Cury

    (@dianakc)

    That is so hard! Any tip anyone?!

    MichaelH

    (@michaelh)

    I was suggesting $posts[0]->post_author not $post[0]->post_author

    Thread Starter Diana K. Cury

    (@dianakc)

    I got it!

    I read the Author_templates.php and there it was: $authordata->ID

    But now, I don´t know how to use the conditional, isn´t working:

    <?php if (! file_exists('aut-'.$authordata->ID.'.php')) { ?>
    <?php include('aut-'.$authordata->ID.'.php'); ?>
    <?php } ?>

    I want include a file aut-{user-ID}.php only if exists, if not, then include a default.php file.

    MichaelH

    (@michaelh)

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Include files only if exists’ is closed to new replies.