Forums

[resolved] Can You Put PHP Code In Custom Field? (21 posts)

  1. soleman
    Member
    Posted 2 years ago #

    I have a simple line I want to put in a custom field and it just won't show up. It works when I place the code in single.php - it just doesn't work when I put the code in through a custom field.

    Here is what I have on my single.php page:

    <?php echo get_post_meta($post->ID, "ko_text", $single = true); ?>

    And in my custom field named ko_text I have: <?php echo ddpl_list(1); ?>

    I've been searching this site and the net for hours trying to find out if there is a way to do this.... !

    Thanks for your help.

  2. Benjamin_SP
    Member
    Posted 2 years ago #

    Maybye the ExecPHP plugin may work. Install and activate it and see if the php code in the custom field displays.

  3. soleman
    Member
    Posted 2 years ago #

    ^ thanks for the heads up. That is a great plugin but I can't use plugins where I am putting this - it has to be in the single.php page outside a sidebar.

  4. crayoncrisis
    Member
    Posted 2 years ago #

    You're probably thinking of widgets, plugins are fine outside a sidebar.

  5. Benjamin_SP
    Member
    Posted 2 years ago #

    When you activate the plugin it will affect all corners of your site - not just the sidebar.

  6. soleman
    Member
    Posted 2 years ago #

    ^ how do I call this plugin? I looked at the plugin page and it didn't give any code to use in the single page or any other page.

    Usually they have the code you can manually put into the page.

    Thanks!

  7. soleman
    Member
    Posted 2 years ago #

    I am figuring it out some - but here is my problem -

    I'm trying to have editors insert this line of code <?php echo ddpl_list(1); ?> only on certain posts - so I thought the custom tags was the ticket. I don't see any plugin that will do this either. I see ones that go into sidebars and I can put those on the pages as well - but I don't wan them on all pages.

  8. Benjamin_SP
    Member
    Posted 2 years ago #

    I used to use ExecPHP a long time ago so I don't remember exactly but I think it is one of those plugins that you just activate and let it take effect (no need to call). To test if it is working insert a php code in a sample post or page. For example try this:

    <?=++$my_post_count . '.';?>

    If this produces "1." <-- this number than ExecPHP is working but if you see just the code itself then it isn't working.

  9. soleman
    Member
    Posted 2 years ago #

    ^ It came up as a "1."! So it is working.

    Any idea how I can get this: <?php echo ddpl_list(#); ?>

    into select posts using this?

  10. Benjamin_SP
    Member
    Posted 2 years ago #

    I'm not sure why you need to use php code as your custom fields value. What is ko_text exactly - I know it's the name of your custom field but what are you trying to do with it? Display an image, text, what?

    If you want to use a custom fields called "ko_text" then place this in your template:

    <? echo get_post_meta($post->ID, 'ko_text', true) ?>

    And in your posts write the value for ko_text. Like "These are some Words"

    and in the area that you designated in your template, the value for ko_text will appear (only for the post/page for which your added the custom field).

  11. soleman
    Member
    Posted 2 years ago #

    Sorry that I'm not being clear - here are the two main reasons I need to put the code in the custom fields (unless someone can think of a better way and then I could say "I could have had a V-8".

    I have 100+ posts that I want people to have access to through a drop down menu (this is the code that activates the drop down menu: <?php echo ddpl_list(Cat#); ?> ). But I don't want this on every page - and - the pages I do want it on it has to have a specific category #.

    The second reason I need to put the code in the custom fields is that I want a specific text file access via php get file. There are multiple posts that need show the contents of different files. 100 posts need to present X file wile 50 posts need to present Y file.

  12. iridiax
    Member
    Posted 2 years ago #

    But I don't want this on every page - and - the pages I do want it on it has to have a specific category #.

    What about conditional code display with in_category?

    http://codex.wordpress.org/Template_Tags/in_category
    http://codex.wordpress.org/Conditional_Tags#A_Category_Page

  13. soleman
    Member
    Posted 2 years ago #

    ^ Dude- ! That worked great - !

    Thanks man -

  14. Benjamin_SP
    Member
    Posted 2 years ago #

    Iridiax is right, conditional tags would be the far better option. Instead of putting custom field values one by one for 100+ posts you can just place this in your template:

    <?php if (is_category('???')) { ?>

    <?php echo ddpl_list(Cat#); ?>

    <?php } ?>

    Instead of "???" you would put your category's id, slug or simply the name. If you want to put multiple categories there, separate them with commas.

    I still don't get the second part of your intention. But the above should cover the first part.

  15. soleman
    Member
    Posted 2 years ago #

    Got it working perfectly. Thanks for everyone's hep - I really appreciate it.

  16. soleman
    Member
    Posted 2 years ago #

    One last item - how would I put an else statement in there? I tried it and it didn't work... and it sure seems easy. Just want the else to be this:

    <p class="post_meta"><span class="date">Posted on <?php the_time('F jS, Y') ?></span></p>

  17. iridiax
    Member
    Posted 2 years ago #

    how would I put an else statement in there?

    Here's the format I use since you can use regular, unaltered html and php with it:

    <?php if (in_category('5')) { ?>
    <!-- code here -->
    
    <?php } else { ?>
    <!-- code here -->
    
    <?php } ?>
  18. soleman
    Member
    Posted 2 years ago #

    Again - sickness! Thanks!

  19. fitzpatrick512
    Member
    Posted 2 years ago #

    hmm.... I actually could use some PHP code in my Custom Fields!

  20. Vin Thomas
    Member
    Posted 2 years ago #

    I could really use PHP in my custom fields as well. I tried EXEC-PHP but it didn't work in a custom field.

    What I am trying to do is display all sibling pages in the sidebar on only a few pages. There is no formula for the pages that I want to use it on otherwise I would use a conditional statement.

    Any help would be greatly appreciated.

  21. vtxyzzy
    Member
    Posted 2 years ago #

    @soleman, I had a similar situation. I wanted to use a different template for certain categories of posts, so I renamed single.php to single-default.php and created a new single.php to test the categories. If it is one of the special categories, use single-in-category.php, otherwise, use single-default.php.

    I originally hard-coded the categories in the new single.php, but as I added new ones, I had to edit the code. My solution was to use http://planetozh.com/blog/my-projects/wordpress-theme-toolkit-admin-menu/.

    This toolkit was easy to integrate with my theme and made it simple to add an option called single_in_category. This contains a comma separated list of the category slugs or IDs. Now I can add or remove a category in the Admin page.

    Here is my new single.php:

    <?php
    // single.php is now in single-default.php
    // We want to use single-in-category for
    // all categories listed in the single_in_category
    // option which is set in the Appearance->Miata Club Options panel.
    // Uses global $mclub created by themetoolkit in functions.php
    global $mclub;
    $post = $wp_query->post;
    $mycats = array();
    if ($mycatlist = $mclub->option['single_in_category']) {
      $mycats = explode(',',$mycatlist);
    }
    
    if ( in_category($mycats)) {
      include(TEMPLATEPATH . '/single-in-category.php'); }
    else {
      include(TEMPLATEPATH . '/single-default.php');
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic