![](/img/trans.png)
[英](WordPress/UltimateMember) Hook functions not working in custom plugin / child theme
[英]Wordpress functions not working in custom theme
因此,我是Wordpress的超级新手。 我昨天在MAMP上设置了Wordpress本地服务器,目前正在尝试构建自己的主题。 麻烦的是,每当我尝试执行the loop
,我都不会得到任何内容。 如果仅使用php,我的代码就可以工作,但是问题是自定义的Wordpress函数不可用(即have_posts()
)
其他几篇文章建议要求wp-blog-header
,但这也没有用。 这是我的代码:
<?php
define('WP_USE_THEMES', false);
require('../../../wp-blog-header.php');
if(have_posts()) :
echo 'testing'
else :
echo 'testing'
endif;
?>
我的屏幕上目前没有任何显示。
wp-blog-header.php
位于此处: wordpress -> wp-blog-header.php
我的自定义主题位于: wordpress -> wp-content -> themes -> firstTheme -> index.php
所有提示,不胜感激。
您需要使用适当的WordPress函数来构建自己的自定义主题。 例如,应该将文件放在主题的根目录中,而不是require
然后是x路径深(从WP-Core角度来看也不希望)。 喜欢 :
`get_template_part('name-of-file-to-include-without-php-ending');`
原因是事实,WordPress Codex提议使用子主题功能。
get_template_part('');
函数会进行一些检查,例如是否已安装子主题等。
问题:为什么要定义该常数?
所有常量都应驻留在wp-config(根文件夹)中。
此外,值得一提的是,您不会在帖子中循环浏览。 在撰写本文时,您的代码仅在有帖子的情况下才会显示,然后什么也不做。 您还需要添加while
函数,以浏览并显示帖子。
因此,做(在loop.php或主题中要显示它们的位置):
<?php if(have_posts() ) : ?>
// The while added
<?php while ( have_posts() ) : the_post(); ?>
// Your template tags here: e.g. the_author();
<h2><?php the_author(); ?></h2>
<h3>the_title();</h3>
etc...
<?php endwhile?>
<?php endif; ?>
最后一件事:WordPress有一些约定,哪些文件应驻留在主题文件夹中。 例如,每个主题都应该有一个functions.php
,您functions.php
在其中放置菜单和小部件等内容。
构建主题的最受欢迎标签是:
get_header();
当然,您的主题中需要有一个header.php
文件。 再次,WordPress将精确地查找要包含的这些文件,不允许其他命名。
get_footer();
猜对了-它会在您的主题文件夹中寻找footer.php
。
有关更多信息,请参见WordPress Codex>模板部分等。https://codex.wordpress.org/Theme_Development#Template_Files
我强烈建议您将WordPress核心功能用于模板,而忽略它们并使用“经典” php,例如include / require会导致您一无所获,但不会成功。
看看您使用的模板部分中的一个好插件是“ What the File”。 获取默认主题,安装该插件,然后在管理栏中查看其内容,以了解WordPress的工作方式。
这是我的循环的摘录,主题是使用引导程序。
<?php if(have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if(is_front_page() || is_page() || is_single() ) : ?>
<div class="row row-content">
<div class="content col-xs-12 col-sm-12 col-md-12 col-lg-12">
<?php the_content(); ?>
</div>
</div>
<?php // category.php, archive.php, search.php ?>
<?php elseif(is_category() || is_archive() || is_search() ) : ?>
<div class="row row-excerpt">
<div class="thumbnail-box col-lg-4">
<a href="<?php the_permalink(); ?>" class="preview-image-link">
<?php
// Thumbnail und Post Auszug
if(has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
}
?>
</a>
</div>
<div class="text-details-box col-lg-8">
<div class="row row-excerpt-text">
<div class="excerpt col-lg-12">
<?php the_excerpt(); ?>
</div>
</div>
<div class="row row-tags row-read-more">
<div class="tags col-lg-8">
<?php the_tags('<ul class="tag-list">
</div>
<div class="read-more-boxcol-md-4 col-lg-4">
<a href="<?php the_permalink(); ?>" class="read-more-btn">
<span class="read-more-btn-text">></span>
</a>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
我在建立一些wordpress网站时也发现了这个问题。 安装了PhP插件(Insert PhP)后,我的问题解决了。
安装后,将其更改为[insert_php]和[/ insert_php],我的代码正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.