【问题标题】:Wordpress category.php not working in /?catWordpress category.php 在 /?cat 中不起作用
【发布时间】:2014-01-03 18:01:33
【问题描述】:

更新后(或者可能没有),但在之前的一段时间,当我要去类别列表页面时 mysite.com/?cat=10 我得到不是 category.php 模板 - 它打开 single.php 为什么?

我正在尝试制作 category-10.php (仅用于测试),但仍会使用 single.php。 我应该在哪里查看我的问题? 问题出在我的 ajax 插件上,他的代码是:

<?php
/**
 * Plugin Name: Ajax content loader
 * Description: Load content througt ajax.
 * Version: 0.1
 * 
 */

 /**
  * Initialization. Add our script if needed on this page.
  */
 function ajax_content_init() {
    global $wp_query;

    // Add code to index pages.
    if( !is_singular() ) {  
        // Queue JS
        wp_enqueue_script(
            'load-posts',
            plugin_dir_url( __FILE__ ) . 'js/load-posts.js',
            array('jquery'),
            '1.0',
            true
        );

        $wp_query->is_single = true;
    }
 }
 add_action('template_redirect', 'ajax_content_init');

 ?>

有什么想法吗? 是手机版的,手机怎么激活?不适合桌面?

【问题讨论】:

  • 您有示例代码可以提供吗?
  • 你怎么知道显示的是single.php,还要尝试禁用所有插件并检查。
  • category.php 或 single.php 的代码?但是如果我使用带有页脚和页眉功能的空白页,它也不会显示。
  • 我正在通过 html 代码搜索,我得到了。
  • 同样的 HTML 可能在你的 index.php 中

标签: php wordpress


【解决方案1】:

下载WordPress Debug Bar Plugin。它会在你的 wp-admin 栏中添加一个按钮,上面写着“调试”。按下时,此按钮将显示调试面板。在此调试面板中找到显示“WP Query”的选项卡,它应该会显示查询变量和正在使用的模板文件。

这应该可以帮助您确定正在注册哪些查询变量,以及真正加载了哪个模板文件。

【讨论】:

    【解决方案2】:

    在尝试进行实验之前,您应该阅读 template_redirect 的 wordpress codex。

    这个动作钩子在 WordPress 确定哪一个之前执行 要加载的模板页面。

    你实际尝试的是:

    if( !is_singular() ) {
    

    如果页面不是单数,则表示不是模板page.phpsingle.php 它应该执行代码。

    所以当您在category.php 时,它会执行代码并将全局$wp_query 图层对象属性single 设置为true。因此,您当时查看的任何模板都会将该页面设置为single

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2015-03-23
      • 2012-01-03
      • 2015-07-13
      • 1970-01-01
      • 2018-11-06
      • 2016-07-02
      • 1970-01-01
      • 2015-07-18
      • 1970-01-01
      相关资源
      最近更新 更多