【问题标题】:Wordpress - Getting the queried type (Category, archive, author etc)Wordpress - 获取查询类型(类别、存档、作者等)
【发布时间】:2016-01-28 12:20:17
【问题描述】:

例如 1.当有人点击“策展人”类别时,将其带到策展人类别页面,并以自己的特色图片显示“策展人”类别的帖子。 或者 2. 当有人点击作者“john”时,它会将他们带到 john 页面,其中包含他的所有帖子并显示“john”特色图片。

问题是我想在不同页面显示特色图像时如何检索此“策展人”或“约翰”。说提取“约翰”去文件夹找到 john.jpg 并显示他的存档页面和他的标题图像。或者进入“explorers”分类页面,显示带有explorers特色图片的分类页面。

我知道了,但它只适用于类别:

<? $cat = get_query_var('cat');
$yourcat = get_category ($cat);
?>
#headerimg {
background-image: url(<?php echo       ithaka_directory_uri() .'/images/'. $yourcat-    >slug .'.jpg'?>);
}

做了 pgk 告诉我的:get_queried_object();但不工作。

<?php $term = $wp_query->get_queried_object(); ?>

#headerimg {
background-image: url(<?php echo ithaka_directory_uri() .'/images/'. $term .'.jpg'?>);
}

它返回给我“致命错误:在 /home/departur/public_html/wp-content/themes/ithaka/ 中的非对象上调用成员函数 get_queried_object() inc/custom-header.php97
"

【问题讨论】:

  • 不是$wp_query-&gt;get_queried_object(),只有get_queried_object(),因为您没有$wp_query 的实例。如果你想这样使用它,你必须这样做global $wp_query

标签: php sql wordpress


【解决方案1】:

你可以使用get_queried_object()函数link

来自法典

检索当前查询的对象。例如:

如果您在单个帖子上,它将返回帖子对象

如果你在一个页面上,它将返回页面对象

如果您在存档页面上,它将返回帖子类型对象

如果您在类别存档中,它将返回类别对象

如果您在作者存档中,它将返回作者对象

等等

【讨论】:

  • 谢谢,但它似乎不起作用。页面出错。
【解决方案2】:

好的,我设法从这个网站修复它: http://stanhub.com/display-wordpress-taxonomy-term-name-in-taxonomy-page/

<?php if (is_archive()) { 
$taxonomy = get_queried_object();
?>

#headerimg {
background-image: url(<?php echo ithaka_directory_uri() .'/images/'. $taxonomy->slug .'.jpg'?>);
}

【讨论】:

    【解决方案3】:

    我的朋友帮我解决了这些问题。如果有文件存在,它也会检查。如果没有,它将输出默认图像。

    <?php
        if (is_archive()) { 
            $taxonomy = get_queried_object();
            if (is_a($taxonomy, 'WP_User'))
                $dataused = $taxonomy->user_nicename;
            elseif (is_a($taxonomy, 'WP_Term'))
                $dataused = $taxonomy->category_nicename;
    
            if (file_exists($_SERVER['DOCUMENT_ROOT'] .'/wp-content/themes/ithaka/images/'. $dataused .'.jpg'))
                $dataused = ithaka_directory_uri() .'/images/'. $dataused .'.jpg';
            else
                $dataused = $header_image;
        } else
            $dataused = $header_image;
        ?>
            #headerimg {
            background-image: url(<?php echo $dataused; ?>);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      相关资源
      最近更新 更多