簡體   English   中英

WordPress功能無法在自定義主題中使用

[英]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

所有提示,不勝感激。

好吧,您正在編寫PHP,並且您的代碼包含

if(have_posts()) :
    echo 'testing'
else : 
    echo 'testing'
endif;

我不確定這在PHP中如何有效。 我希望

if(have_posts())
    echo 'testing';
else
    echo 'testing';

另請參見PHP中的ifelse語法。

您需要使用適當的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM