WordPress.org

Ready to get started?Download WordPress

Forums

php require_once() function (9 posts)

  1. imanerd
    Member
    Posted 5 years ago #

    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...

  2. esmi
    Theme Diva & Forum Moderator
    Posted 5 years ago #

    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.

  3. t31os
    Member
    Posted 5 years ago #

    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.....

  4. esmi
    Theme Diva & Forum Moderator
    Posted 5 years ago #

    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. :-)

  5. t31os
    Member
    Posted 5 years ago #

    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 ...

  6. imanerd
    Member
    Posted 5 years ago #

    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.

  7. t31os
    Member
    Posted 5 years ago #

    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....

  8. imanerd
    Member
    Posted 5 years ago #

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

    Thanks for the help!

  9. chriseriksson
    Member
    Posted 4 years ago #

    esmi,
    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...

    Thanks,
    Chris

Topic Closed

This topic has been closed to new replies.

About this Topic