【问题标题】:How to call up a page or a post in WordPress?如何在 WordPress 中调用页面或帖子?
【发布时间】:2020-03-26 23:02:17
【问题描述】:

如果类别中没有帖子,我希望类别页面显示特定页面(或帖子)。怎么做?

到目前为止,我发现它取决于 index.php 文件。我在主题子模板下创建了一个新的 index.php 文件。我可以让它显示一条纯文本消息,例如“当前该类别中没有帖子”,但不是那样,我希望它呈现特定页面(或帖子)。

我应该在那个 PHP 文件中做什么?它看起来如下:

<?php get_header(); ?>
<?php if( is_home() && ! is_paged() ) : ?>
    <?php if( pinboard_get_option( 'slider' ) ) : ?>
        <?php get_template_part( 'slider' ); ?>
    <?php endif; ?>
    <?php get_sidebar( 'wide' ); ?>
    <?php get_sidebar( 'boxes' ); ?>
<?php elseif( ( is_home() && is_paged() ) || ( ! is_home() && pinboard_get_option( 'location' ) ) ) : ?>
    <?php pinboard_current_location(); ?>
<?php endif; ?>
<div id="container">
    <section id="content" <?php pinboard_content_class(); ?>>
        <?php if( is_category( pinboard_get_option( 'portfolio_cat' ) ) || ( is_category() && cat_is_ancestor_of( pinboard_get_option( 'portfolio_cat' ), get_queried_object() ) ) ) : ?>
            <?php pinboard_category_filter( pinboard_get_option( 'portfolio_cat' ) ); ?>
        <?php endif; ?>
        <?php if( have_posts() ) : ?>
            <div class="entries">
                <?php while( have_posts() ) : the_post(); ?>
                    <?php get_template_part( 'content', get_post_format() ); ?>
                <?php endwhile; ?>
            </div><!-- .entries -->
            <?php pinboard_posts_nav(); ?>
        <?php else : ?>
        <?php get_post ("page-id-2364"); ?> //this doesn't work!
        <?php endif; ?>
    </section><!-- #content -->
    <?php if( 'no-sidebars' != pinboard_get_option( 'layout' ) && 'full-width' != pinboard_get_option( 'layout' ) && ! is_category( pinboard_get_option( 'portfolio_cat' ) ) && ! ( is_category() && cat_is_ancestor_of( pinboard_get_option( 'portfolio_cat' ), get_queried_object() ) ) ) : ?>
        <?php get_sidebar(); ?>
    <?php endif; ?>
    <div class="clear"></div>
</div><!-- #container -->

根据https://developer.wordpress.org/reference/classes/wp_query/的文章对代码做了一些修改,但没有成功。 :( 我肯定在这里错过了一些非常基本的东西,但是什么?

<?php get_header(); ?>
<?php if( is_home() && ! is_paged() ) : ?>
    <?php if( pinboard_get_option( 'slider' ) ) : ?>
        <?php get_template_part( 'slider' ); ?>
    <?php endif; ?>
    <?php get_sidebar( 'wide' ); ?>
    <?php get_sidebar( 'boxes' ); ?>
<?php elseif( ( is_home() && is_paged() ) || ( ! is_home() && pinboard_get_option( 'location' ) ) ) : ?>
    <?php pinboard_current_location(); ?>
<?php endif; ?>
<div id="container">
    <section id="content" <?php pinboard_content_class(); ?>>
        <?php if( is_category( pinboard_get_option( 'portfolio_cat' ) ) || ( is_category() && cat_is_ancestor_of( pinboard_get_option( 'portfolio_cat' ), get_queried_object() ) ) ) : ?>
            <?php pinboard_category_filter( pinboard_get_option( 'portfolio_cat' ) ); ?>
        <?php endif; ?>
        <?php if( have_posts() ) : ?>
            <div class="entries">
                <?php while( have_posts() ) : the_post(); ?>
                    <?php get_template_part( 'content', get_post_format() ); ?>
                <?php endwhile; ?>
            </div><!-- .entries -->
            <?php pinboard_posts_nav(); ?>
        <?php else : ?>
        <?php $query = new WP_Query( array( 'page_id' => 2364 ) ); ?> //still doesn't work
        <?php endif; ?>
    </section><!-- #content -->
    <?php if( 'no-sidebars' != pinboard_get_option( 'layout' ) && 'full-width' != pinboard_get_option( 'layout' ) && ! is_category( pinboard_get_option( 'portfolio_cat' ) ) && ! ( is_category() && cat_is_ancestor_of( pinboard_get_option( 'portfolio_cat' ), get_queried_object() ) ) ) : ?>
        <?php get_sidebar(); ?>
    <?php endif; ?>
    <div class="clear"></div>
</div><!-- #container -->

【问题讨论】:

  • 谁能告诉它为什么不起作用?
    2361)); _e('$查询'); ?>

标签: php wordpress


【解决方案1】:

类别页面应从category.php 文件或更多指定文件中显示(参见:https://developer.wordpress.org/files/2014/10/wp-hierarchy.png)。

您可以使用require() 函数在您的category.php 文件中显示一些文件内容。

【讨论】:

  • 感谢您的快速回答!但是,这是什么意思?我不太熟悉 WordPress 和 PHP。我应该在上面的 index.php 文件中更改或放入什么?我知道它适用于我的类别页面,因为我只用一个文本行而不是那个引用无效页面 ID 的字符串来尝试它。或者我应该复制一个 category.php 并将其粘贴到子主题,如果是,我应该在那里更改或添加什么?
  • instead of that, I want it to render a specific page (or a post) - 特定页面是来自 WordPress 的某个页面还是您的自定义 php 页面?
  • 这是一个普通的WP页面。
  • 好的,现在很清楚了。您需要使用WP_Query 来获取指定的页面详细信息(标题、内容、摘录、图像等)。在此处查看更多信息:developer.wordpress.org/reference/classes/wp_query
  • 感谢您的参考。我会研究一下,看看我是否能找到可行的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-05
  • 2015-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多