Support » Themes and Templates » php require_once() function

  • I’m trying to figure out how to eliminate redundant code by using includes. For example, a theme that I’m working on has the following meta info for each post:

    <p class="postmeta"><?php the_time('M j, Y'); ?> | <?php the_author(); ?> | <?php the_category(', ') ?> | <a href="<?php comments_link(); ?>" class="comments">Comments (<?php comments_number('0','1','%'); ?>)</a></p>

    This code is located in several theme files so if I want to rearrange items (e.g., place the author before the date), I would have to edit the same code in multiple places. The ability to use includes would make updates such as this much easier. Please help…

Viewing 8 replies - 1 through 8 (of 8 total)
  • esmi


    Forum Moderator

    You could try creating a function that echoes out your code and placing it in functions.php within your theme. Then replace the code in the individual template files with a call to your new function. You don’t have to include any call to functions.php first. WordPress does that by default.

    You could wrap that inside a function and even add the ability to switch the display order with some minor arguments…

    Simply put a function just runs a set of code….

    <?php function my_example() {
    echo 'this is my example';

    then the call..
    <?php my_example(); ?>

    Would output…

    this is my example

    Esmi it’s not a simple matter of echo’ing the content unfortunately because not all WP functions operate on that basis.. some return, some echo…etc…

    I can give some examples if you want to put it into a function…..



    Forum Moderator

    True – though I’m pretty sure most of those particular native WP functions actually echo (e.g. the_category or the_author). So, if you’re not used to writing theme functions, it may be a lot easier to echo them out than try to return them. Otherwise you end up with the WP function outputs appearing before your customised markup – which then means finding similar functions that ‘return’ instead of ‘echo’. All of which can be a bit overwhelming if it’s your first function. 🙂

    I usually just print, echo each one and see what i get, then dig around in the files if i’m still confused….

    Not that i’m sure it makes a difference, i end up confused half the time anyway…. :-s …

    Thanks for the info. Prior to posting this topic, I tried creating a function in functions.php with a single statement echo "test"; and it totally broke wordpress when I saved. I couldn’t even get in the admin panel (good thing I was working on a development server).

    I should also add that I’m very comfortable with php/mysql but relatively new to wordpress.

    Theme functions aren’t called into the admin – unless told to do so, so i can’t see how a basic function caused a problem like that…

    It was proberly a minor typo or something silly…

    I usually test my fuctions directly in the template then move it to the functions file once it works.

    It’s the same proceedure you’d use for a normal PHP function….

    It works now…I had to disable a couple of my security plugins.

    Thanks for the help!

    im require_once(wp_config.php) and like you said it echoes out stuff automatically. Is there any way to get rid of this without locating the source of the echo. Like deleting straight after the call whatever it is echoing? I don’t know php that well…


Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘php require_once() function’ is closed to new replies.