Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在WordPress中进行函数调用的正确方法_Php_Wordpress_Custom Wordpress Pages - Fatal编程技术网

Php 在WordPress中进行函数调用的正确方法

Php 在WordPress中进行函数调用的正确方法,php,wordpress,custom-wordpress-pages,Php,Wordpress,Custom Wordpress Pages,在wordpress页面上进行函数调用的最佳实践是什么 例如,如果您想调用my_special_function()中,函数调用的正确位置(即home page.php,/template parts/content page.php等) 仅供参考,我使用下划线主题 我看到了一些关于短代码的评论。与只调用该页面上的函数相比,下面插入了短代码的WP页面模板是一种最佳实践吗 因此,如果我想在下面的页面模板中调用函数,我只需要在页面模板中的任何地方插入短代码,或者只是调用函数就足够了,或者是最佳实

在wordpress页面上进行函数调用的最佳实践是什么

例如,如果您想调用
my_special_function()中,函数调用的正确位置(即
home page.php
/template parts/content page.php
等)


仅供参考,我使用下划线主题

我看到了一些关于短代码的评论。与只调用该页面上的函数相比,下面插入了短代码的WP页面模板是一种最佳实践吗

因此,如果我想在下面的页面模板中调用函数,我只需要在页面模板中的任何地方插入短代码,或者只是调用函数就足够了,或者是最佳实践

<?php
/**
 * Template Name: Home Page
 *
 * The template for displaying the home page.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package Rollins_Ridge
 */

get_header(); ?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">

                // insert Shortcode
                <?php echo do_shortcode('[special_function_shortcode]') ;?>

               // or just call the function
               my_special_function();

               // or something else im not aware of
               code here;

            <?php
            while ( have_posts() ) : the_post();

                get_template_part( 'template-parts/content', 'page' );

                // If comments are open or we have at least one comment, load up the comment template.
                if ( comments_open() || get_comments_number() ) :
                    comments_template();
                endif;

            endwhile; // End of the loop ?>
        </main><!-- #main -->
    </div><!-- #primary -->
<?php
get_footer();

//插入短代码
//或者直接调用函数
我的特殊函数();
//或者其他我不知道的事情
代码在这里;

不要编辑主题核心文件。 正确的方法是在子主题目录中使用functions.php文件

您可以在此文件中添加函数,并使用wordpress钩子创建shortocde 像

并在现场任何地方使用以下shortocde

in wordpress page use [special_function_shortcode]
and in php use
<?php echo do_shortcode('[special_function_shortcode]') ;?>
在wordpress页面中使用[special_function_shortcode]
以及在php中的使用

取决于主题的实现。一些主题仅随
index.php
提供,其他主题随
home page.php
提供。主题根目录中的php文件是什么?此外现代主题随页面生成器而来,这更难说明它可以在哪里运行函数。另一种想法是,如果您不熟悉运行函数的正确位置,请创建自己的短代码,然后您可以将短代码放在内容中而不是主题中。@MerianosNikos感谢您的回复。我使用的是下划线初学者主题,到目前为止,我只是把函数调用放在我想调用的任何页面的“content area”div中。我只是想知道这是实现这一目标的最佳方式还是有更合适的方式。我对shortcode不是很熟悉,但我会看一下文档。你很好。WordPress it真的很简单,因此你应该走捷径,不要再考虑更复杂的解决方案;)@MerianosNikos lol好吧,我想我会继续做我正在做的事情,只是想确保将来不会有任何问题:)你可以直接在php中调用函数,无需使用do_shortcode函数。@kunruh我通常只在页面模板中直接调用该函数,但我一直在寻找一种最佳实践方法来实现它。@MoxDev如果要在自己制作的php模板中使用该函数,最佳实践是直接调用该函数。
my_special_function(){

//Your content goes here.

echo "This is proper way";

}
add_shortcode('special_function_shortcode','my_special_function');
in wordpress page use [special_function_shortcode]
and in php use
<?php echo do_shortcode('[special_function_shortcode]') ;?>