admin管理员组

文章数量:1122833

I've created my custom posts. Data from the same is displayed in let's say single-custom-post.php.

Now to clean that file and to remove 100 lines of code, I want to make a new file (let's call it custom-header.php) and then just echo or call that content.

Since I've built custom post as a plugin, ideally would be nice to call it if plugin is active, however I don't know how to finish this because one statement is if myplugin is active but then I would somehow add another statement - function, and here I am lost (below).

I could call it by if function exist, but then i am not sure how do I create that function.

What I did and it does work is this:

  • inside custom-header.php I've added

    if ('custom_header_display') { ?>   
        //my code
    } ?>
    

and then in single-custom-post.php I replaced that 100 lines of code with

include (TEMPLATEPATH . '/inc/custom-header.php' );?>

So my problem is, I don't know is this a good way, and if it's not, what is it?

I've created my custom posts. Data from the same is displayed in let's say single-custom-post.php.

Now to clean that file and to remove 100 lines of code, I want to make a new file (let's call it custom-header.php) and then just echo or call that content.

Since I've built custom post as a plugin, ideally would be nice to call it if plugin is active, however I don't know how to finish this because one statement is if myplugin is active but then I would somehow add another statement - function, and here I am lost (below).

I could call it by if function exist, but then i am not sure how do I create that function.

What I did and it does work is this:

  • inside custom-header.php I've added

    if ('custom_header_display') { ?>   
        //my code
    } ?>
    

and then in single-custom-post.php I replaced that 100 lines of code with

include (TEMPLATEPATH . '/inc/custom-header.php' );?>

So my problem is, I don't know is this a good way, and if it's not, what is it?

Share Improve this question edited Mar 9, 2016 at 4:36 Pieter Goosen 55.4k23 gold badges115 silver badges209 bronze badges asked Mar 9, 2016 at 4:33 MaxMax 1294 silver badges14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You've got an issue with your code. This code will always return true because you're passing a string to it.

if ('custom_header_display') { ?>   
    //my code
} ?>

You can instead use is_plugin_active() to check if the plugin is activated.

Quoting the example from the Codex page, your code should be like:

<?php

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if ( is_plugin_active( 'plugin-directory/plugin-file.php' ) ) {
   // include your file.
} 

本文标签: How to display content If function existcondition true