- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我目前在我的 CMS 中有一个选项,可以将标签添加到我的自定义帖子类型单页。
现在,我想将此标签显示为“特色”项目。因此,在我的分类法“文件名”中,我使用以下代码收集标签并将它们显示在分类法页面中:
<?php
$args = array(
'tag_slug__and' => array('sector1'),
'post_type' => array( 'sectors' )
);
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<a href="<?php echo get_permalink(); ?>">
<?php echo "<div class='col-md-6' style='margin-bottom:20px;'>"; ?>
<div class="row mobilemargin">
<div class="categorytiletextsector1">
<div class="col-md-6 col-sm-6 col-xs-12 nopr"><?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'sector1img hovereffect')); ?> </div>
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="testdiv">
<h5><?php the_title(); ?></h5>
<p><?php the_excerpt(); ?></p>
</div>
</div>
</div>
</div>
<?php echo "</div>"; ?>
</a>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
现在,我的问题是,这将在每个类别页面上显示所选标签,因为它是在分类页面上设置的。
我怎样才能使它只在当前类别上设置。
那么如果我的商品属于“类别 A”,只有“A”的类别页面会使用商品类别显示它?
任何帮助都会很棒
编辑。使用了这段代码,希望它能工作,但运气不佳
$args = array(
'tag_slug__and' => array( 'sector1' ),
'post_type' => array( 'sectors' ),
'tax_query' => array(
array(
'taxonomy' => 'sectors',
'terms' => get_queried_object_id(),
),
),
);
最佳答案
您的问题是您的自定义查询。这里有一个非常重要的注意事项是,永远不要在任何类型的存档页面或主页上用自定义查询替换主查询。我已经在 this post 中详细解释了所有内容最近。请务必阅读它和所有链接的帖子,因为这会让你受益匪浅
您的解决方案是删除您的自定义查询并将其替换为我们都知道的默认循环
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
// Your template tags and html mark up
}
}
如果您需要更改主查询中的任何内容,请使用 pre_get_posts
进行更改
你最好的想法是使用完整的 tax_query
来显示所选分类术语和标签中的帖子
您可以尝试这样的操作:(至少需要 PHP 5.4+。另外,这个未经测试)
$q = get_queried_object();
$args = [
'post_type' => 'sectors',
'tax_query' => [
[
'taxonomy' => $q->taxonomy,
'terms' => $q->term_id,
'include_children' => false // Exclude child terms
],
[
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'sector1', //I believe this is the slug
],
],
];
对于较旧的 PHP 版本,请使用以下内容
$q = get_queried_object();
$args = array(
'post_type' => 'sectors',
'tax_query' => array(
array(
'taxonomy' => $q->taxonomy,
'terms' => $q->term_id,
'include_children' => false // Exclude child terms
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'sector1', //I believe this is the slug
),
),
);
要排除 sector1
标签和任何其他 sectorX
标签中的帖子,您可以执行以下操作
您可以尝试这样的操作:(至少需要 PHP 5.4+。另外,这个未经测试)
$q = get_queried_object();
$args = [
'post_type' => 'sectors',
'tax_query' => [
[
'taxonomy' => $q->taxonomy,
'terms' => $q->term_id,
'include_children' => false // Exclude child terms
],
[
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'sector1', //I believe this is the slug
'operator' => 'NOT_IN'
],
],
];
对于较旧的 PHP 版本,请使用以下内容
$q = get_queried_object();
$args = array(
'post_type' => 'sectors',
'tax_query' => array(
array(
'taxonomy' => $q->taxonomy,
'terms' => $q->term_id,
'include_children' => false // Exclude child terms
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'sector1', //I believe this is the slug
'operator' => 'NOT_IN'
),
),
);
请注意,您可以像这样将一组标签传递给 terms
参数
'terms' => array( 'sector1', 'sector2', 'etc' ),
或短数组语法
'terms' => ['sector1', 'sector2', 'etc'],
由于这是您的主要查询,您需要进行一些更改。正如我所说,删除自定义查询。你的主循环应该看起来像这样
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<?php echo "<div class='col-md-6 col-sm-6 col-xs-12' style='margin-bottom:30px;'>"; ?>
<div class="row mobilemargin">
<div class="categorytiletext2">
<div class="col-md-6 col-sm-12 col-xs-12 nopr"><?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'hovereffect newimgheight')); ?> </div>
<div class="col-md-6 col-sm-12 col-xs-12 mobilewhite">
<div class="testdiv">
<h5 class="captext"><?php the_title(); ?></h5>
<?php $trimexcerpt = get_the_excerpt();
$shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 10, $more = '… ' );
echo '<a href="' . get_permalink() . '"><p>' . $shortexcerpt . '</p></a>';
?>
</div>
</div>
</div>
</div>
<?php echo "</div>"; ?>
</a>
<!-- If there is no posts, display an error message -->
<?php endwhile;
else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<!-- If there is no posts, display an error message -->
您现在可以使用 pre_get_posts
从分类页面中删除所需的标签。在您的 functions.php 中,执行以下操作:(需要 PHP 5.3+,并且还未经测试)
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
$q->set( 'tag__not_in', array( 145 ) );
}
});
旧版本使用
add_action( 'pre_get_posts', 'so30256167_remove_tags' );
function so30256167_remove_tags( $q )
{
if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
$q->set( 'tag__not_in', array( 145 ) );
}
}
请记住将 145
更改为您的确切标签 id 或一组标签 ids
如果您没有标签 ID,可以使用 get_term_by()
从标签 slug 中获取标签 ID。像这样的事情会做:(需要 PHP 5.3+,并且还未经测试)
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
$tag_object = get_term_by( 'slug', 'sector1', 'post_tag' );
$tagID = $tag_object->term_id;
$q->set( 'tag__not_in', array( $tagID ) );
}
});
旧版本使用
add_action( 'pre_get_posts', 'so30256167_remove_tags' );
function so30256167_remove_tags( $q )
{
if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
$tag_object = get_term_by( 'slug', 'sector1', 'post_tag' );
$tagID = $tag_object->term_id;
$q->set( 'tag__not_in', array( $tagID ) );
}
}
如果你有一个标签 block 数组,你可以替换下面的
$tag_object = get_term_by( 'slug', 'sector1', 'post_tag' );
$tagID = $tag_object->term_id;
$q->set( 'tag__not_in', array( $tagID ) );/*
与
$tag_array = array( 'slug1', 'slug2', 'slug3' );
foreach ( $tag_array as $tag ) {
$tag_object = get_term_by( 'slug', $tag, 'post_tag' );
$tagids[] = $tag_object->term_id;
}
$q->set( 'tag__not_in', $tagids );
请记住相应地更改 slug
你在 functions.php 中带有 pre_get_posts
的最终代码应该是
add_action( 'pre_get_posts', 'so30256167_remove_tags' );
function so30256167_remove_tags( $q )
{
if ( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
$tag_array = array( 'sector1', 'sector2', 'sector3', 'sector4' );
foreach ( $tag_array as $tag ) {
$tag_object = get_term_by( 'slug', $tag, 'post_tag' );
$tagids[] = $tag_object->term_id;
}
$q->set( 'tag__not_in', $tagids );
}
}
关于php - 在分类页面上显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30256167/
假设我有父页面。 当我单击“打印”时,会打开一个新选项卡(页面)以进行打印预览。 我想要的是,当我单击“打印”时,父选项卡(页面)上应显示覆盖层,以停止进一步的工作,直到子选项卡关闭。 Because
我知道这是不可能的,但我想我还是会问这个问题。 我有一个 HTTPS 页面,并且正在动态创建一个表单。我想将表单发布到 HTTP 页面。在浏览器不弹出警告的情况下这可能吗?当我在 IE8 上执行此操作
在我的应用程序中,我实现了ajax 4.0客户端模板 目前我的模板位于同一个.aspx页面上。 (例如 Main.aspx) 但我想将它们外部化。(即所有 HTML 都会放在另一个页面上) 为此,我使
我目前正在构建自己的网络应用程序。在此应用程序的标题中有一个导航栏。 我将相同的导航栏添加到我的所有 html 页面,以便在页面之间导航时保持可见,并将导航元素设置为事件到我现在所在的页面。 我的
我们有一个在集成管道下运行的 IIS7 Intranet 站点,该管道主要是 ASP.Net,带有一些传统的经典 ASP 页面。该站点允许匿名访问大多数区域,但使用 Windows 身份验证来保护某些
有一个要求,我需要根据 div 标签的高度超过 700 像素的条件将大型 HTML 页面拆分为多个页面。 我还可以根据字数拆分页面。 下面的示例根据 HTML 标签分割页面,而我需要根据 div 高度
有两个 html 页面,如果在第一个页面中单击特定图像,它将转到第二个 html 页面。如果在第二个 html 页面上的任何地方单击它应该返回到第一页并将第二页完全显示在第一页的一个 div 中 1s
我有一个简单的 hello world Servant 应用程序。我需要向它添加一些静态或动态的 html 页面。我怎样才能做到这一点?在文档中没有提到它。注意我不想在 Haskell 代码中创建 h
我有两个文件 index.html 和 index2.html。这两个文件都在本地机器上的同一目录中(无法访问 PHP 等)。 我正在努力 Page Title 使用 jQuery 从 index.h
假设我有一个 JSP 页面 index.jsp User id:
我面临着一个对我来说非常陌生的情况。我会尽力让事情变得简单来解释它一步一步来。 1)在页面 A(用户表单)中,我从字段中获取一些值并将其作为 jQuery AJAX POST 发送到页面 B。这是主要
我正在尝试弄清楚如何从 buttons.html 获取用户按钮点击动态切换第二个列的可见性 userinput.html 。我正在使用 DataTables userinput.html表并认为我有
我有asp。净 mvc4 项目。 Angularjs 已集成。 我已经按照以前的要求构建了 HTML 页面和 WEB API 2。 现在出于某种原因,我必须使用 CSHTML 页面。以前我只有 web
我的网站包含大约 18 个不同的页面。以管理员身份登录后,我单击“页面”。 我看到了以下内容 All (20) | Mine (19) | Published (19) | Draft (1) No
Haskell 中每个函数都有手册页吗?换句话说,我可以在某处输入 man fst 并获取有关该函数的手册或帮助页面吗?我发现大量的功能势不可挡! 我正在使用 GHC 和 GHCi。 最佳答案 我不知
我一直在用github pages很长一段时间以来,并使用自定义域重定向功能来重定向我的 user website来自 prahladyeri.github.io到我自己的域名 https://www
我对 Github 有一个基本的了解:我知道如何创建、添加、提交、推送和克隆存储库。我也开始探索 Github 页面来托管我的项目。我的最新项目于 3 月开始并将其推送到 gh-page。从那以后,我
在尝试查看日志时,Airflow 会抛出一个带有以下消息的 oops 页面: File "/Users/user/.pyenv/versions/3.5.2/lib/python3.5/locale.
我已经按照此处列出的步骤 (https://pages.github.com/) 为我正在处理的网站设置了一个 GitHub 页面 我设置的站点当前托管在 IIS 下,并使用 URL Rewrite
我想将我的 domain.com 重定向到 openshift 页面 (openshift-app.rhcloud.com)。我已经按照描述完成了 here它有效。唯一的问题是,在任何网站上,我都不会
我是一名优秀的程序员,十分优秀!