首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress Post格式下拉列表为空

Wordpress Post格式下拉列表为空
EN

Stack Overflow用户
提问于 2022-02-03 20:00:05
回答 1查看 114关注 0票数 0

我在研究wordpress的主题。该主题基于用户配置的复选框启用默认支持的post格式的子集,如下所示:

代码语言:javascript
复制
function mytheme_enable_theme_support(){
  $options = get_option('post_formats');
  $formats = array('aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat');
  $output = array();
  foreach($formats as $format){
    if (isset($options[$format])){
      $output[] = $format;
    }
  }
  if(!empty($options)){
    add_theme_support('post-formats', $output);
  }
}
add_action('after_setup_theme', 'mytheme_enable_theme_support');

检索到的选项的GUI设置页面工作得很好,我已经在DB级别验证了我希望看到的post格式在那里。

我在日志中没有看到错误。当添加一个新的帖子时,你应该能够选择一个帖子格式,但是下拉列表中没有选项。

有人知道是怎么回事吗?

我使用PHP8.0在NGINX上运行Wordpress 5.8.1。

更新:

我已经用我编写的debug()调试例程修改了上面的代码,它将变量的内容打印到文件中,如下所示:

代码语言:javascript
复制
  if(!empty($options)){
    debug(print_r($output,true)."\n");
    debug(print_r($options,true)."\n");
    add_theme_support('post-formats', $output);
  }

上面两个debug()调用的输出如下:

代码语言:javascript
复制
Array ($output)
(
    [0] => gallery
    [1] => video
    [2] => audio
)

Array ($options)
(
    [gallery] => 1
    [video] => 1
    [audio] => 1
)

在我看来,不管代码上面发生了什么,对add_theme_support('post-formats', $output);的调用都是正确的。所以我想我的问题是..。为什么我在post格式下拉列表中没有看到这些选项?我还能去哪里看看会发生什么呢?

更新2:

以下是我为那些感兴趣的人精心评论的运行代码的版本。

代码语言:javascript
复制
//enable the post formats chosen in the theme settings
function mytheme_enable_theme_support(){
  $options = get_option('post_formats'); 
  /* if the option 'post_format' is found in the DB, creates an arrray of what it contains
     in my case, that array looks like this:

     Array (
         [gallery] => 1
         [video] => 1
         [audio] => 1
     )     

     if the option is not found, $options will contain NULL.
  */
  
  $formats = array('aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat');
  /* here we initialize a temporary variable with all the supported post formats. This is so that we 
     can use it below to build an array of just the ones configured. There are likely better ways to 
     do this but this works.

  */
  
  $output = array(); //initialize $output as an empty array
  
  foreach($formats as $format){
    /* loop through the $formats array defined above. This loop will execute 9 times.
       each time it executes $format will contain the next string in the sequence 
       ('aside', 'gallery', 'link', etc.)
    */
    if (isset($options[$format])){
      /* this if statement checks to see if the current $format string exists as a key in the $options
      array. 
        if (isset($options['aside']))
        if (isset($options['gallery']))
        if (isset($options['link']))
        if (isset($options['image']))
        etc.
      */  
      
      $output[] = $format;
      /* if it does, this assignment pushes the current format string onto the $output array. 
      $output[] = $format; is equivalent to array_push($output, $format);
      See https://www.php.net/manual/en/function.array-push.php for details.
      */
    }
    // $output[] = ( @$options[$format] == 1 ? $format : ''); // '@' is shorthand for 'isset()'
  }
  if(!empty($options)){
    // when we're all done, if we have any elements in the $options array... 

    debug(print_r($output,true)."\n");
    debug(print_r($options,true)."\n");
    // configure the theme to support them
    add_theme_support('post-formats', $output);
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-23 18:02:26

我发现了问题。这和我的代码无关。上面所有的讨论都是彻头彻尾的“红鲱鱼”。

主题根目录中缺少index.php模板文件,会以某种方式破坏post编辑器中的post格式下拉菜单。我对WordPress的内部结构不够精通,无法推测可能发生的事情。你们中的一些人可能理解这一点,如果你理解了,请为记录添加一个评论或另一个答复。

根据这里文档中的模板层次结构,如果存在以下模板,则不需要用于呈现所有页面的默认模板index.php:

  • archive.php
  • single.php
  • page.php
  • home.php
  • 404.php
  • search.php

但情况显然并非如此。如果无论其他模板是否存在,index.php都会丢失,那么就没有功能后格式下拉菜单了。

为了记录在案,我的新index.php页面是我的home.php的一个副本,包含以下代码:

代码语言:javascript
复制
<?php get_header();
?>
  <div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
        <?php
          if(have_posts()):

            while(have_posts()): the_post();

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

            endwhile;

          endif;
         ?>
    </main>
  </div> <!-- #primary -->

<?php get_footer(); ?>

正如您所看到的,至少在代码中没有什么奇怪的事情。我使用PHP7.4运行WordPress 5.8.1。在DevKinsta版本2.4.1 (2.4.1.3185)上。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70977537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档