【问题标题】:Wordpress the_content / post is emptyWordpress the_content / post为空
【发布时间】:2016-12-04 00:53:30
【问题描述】:

我正在为 Wordpress 构建自己的模板,但在显示帖子内容时遇到了问题。

我已经为主页建立了一个页面模板,它工作正常。循环输出我想要显示的内容。现在我正在构建模板以显示一篇文章,但循环没有返回任何内容。

这是页面模板的代码:

<?php
/*
Template Name: PAGE
*/
define( 'WP_USE_THEMES', false );
get_header(); 
?>

<div class="wrapper">
    <div class="sidebar">
    <?PHP get_sidebar(); ?>
    </div>
    <div class="main">
    <div class="section group">
        <div class="col col12-12">
            <span>
                <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                <?php endwhile; else : ?>
                    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
                <?php endif; ?>
            </span>
        </div>
    </div>
</div>
</div>  
<?php get_footer(); ?>

当我单击主页上的某个链接时,这会导致显示“抱歉,没有符合您的条件的帖子”消息。奇怪的是该页面确实存在(它的 ID=26,如下所示):

<?php
$post = get_post(26); 
$title = $post->post_title;
echo $title;
?> 

这有效并显示预期的标题。我试过 get_the_ID();获取帖子 ID,但它返回一个空变量。

我的模板中可能缺少一些东西,但我不知道是什么。

有什么想法吗?

谢谢

【问题讨论】:

标签: wordpress


【解决方案1】:

我发现了这个问题,它与模板本身无关。 我通过使用一个标准的 Wordpress 主题(25 个)发现了问题所在,即使我从管理 UI 中点击了一个帖子,每个帖子都会导致我出现 404。我将永久链接结构换回了 ?p=123 选项,一切正常。绝对是永久链接结构问题。

问题来自 polylang 插件。我正在使用一个 wordpress 站点网络,为此我提供了这个插件。我不需要这个特定站点的插件,但无论如何它都需要处于活动状态。所以我激活了 polylang 并将其配置为只有一种语言,现在它可以工作了。

在这个过程中得到了新的白发,但如果这可以帮助任何人......

感谢您的帮助!

劳伦特

【讨论】:

    【解决方案2】:

    我不认为,这个模板可以输出任何这样的帖子。对于WordPress

    /*
    Template Name: PAGE
    */
    

    表示:这是一个页面模板,您可以将其分配给 page 的内容,但不能分配给 post

    当您想在此类页面上显示特定帖子时,您必须:

    1. 将模板分配给 WP-backend 中的页面
    2. 向模板添加新查询以显示帖子

    喜欢:

    <?php
    /*
    Template Name: PAGE
    */
    define( 'WP_USE_THEMES', false );
    get_header(); 
    ?> 
    
    // The new Query
    $args = array (
    'p'  => '26',
    );
    // The Query
    $query = new WP_Query( $args );
    
       <div class="wrapper">
        <div class="sidebar">
        <?PHP get_sidebar(); ?>
        </div>
        <div class="main">
        <div class="section group">
            <div class="col col12-12">
                <span>
                    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                    <?php endwhile; else : ?>
                        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
                    <?php endif; ?>
                </span>
            </div>
        </div>
    </div>
    </div>  
    <?php get_footer(); ?>
    

    但由于这非常复杂,我建议:

    1. 从模板中删除/* Template Name: PAGE */
    2. 另存为single.php或更具体为single-26.php
    3. 链接到您的 wp 菜单中所需的帖子

    【讨论】:

      猜你喜欢
      • 2012-03-14
      • 1970-01-01
      • 2016-03-04
      • 2019-07-06
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2015-06-14
      相关资源
      最近更新 更多