修改子比主题排行榜时记录的一些WordPress调用函数代码写法(来自百度一下)

我更新的动力 - 源自您的关注。

------正文内容展示,开始汲取新知识啦------
  • Posts_per_page – 设置要显示的文章数。
  • Author – 按一位或多位作者缩小结果范围。
  • Cat – 指定结果应属于的类别。
  • Tag – 拉出具有特定标签的文章。
  • Orderby – 按作者、文章类型、日期等对结果进行排序。
  • Order – 按升序或降序对结果进行排序。
  • Post_type – 定义查询是否应检索文章、页面或自定义文章类型。
  • Post_status – 指定文章是否正在进行、已安排、已发布或已删除。

讲讲关于排序相关的参数,排序相关的参数就是 order 和 orderby 两个参数,但是值比较多比较多:

首先是 order 参数,数据类型为:(string | array),用于指定 “orderby” 参数的升序或降序,默认为”DESC”,即为降序,如果是数组的话,可用于多个 order/orderby 集:

  • ASC – 升序,从最低值到最高值 (1, 2, 3; a, b, c)
  • DESC – 降序,从最高值到最低值 (3, 2, 1; c, b, a)

然后是 orderby 参数,数据类型为:(string | array),按参数对检索到的文章进行排序。默认为:date (post_date)。可以传递一个或多个选项:

  • none – 无顺序
  • ID – 按文章 ID 排序,注意 ID 是大写的。
  • author – 按文章作者排序。
  • title – 按文章标题排序
  • name – 按文章名称排序,即 URL别名。
  • type – 按文章类型排序。
  • date – 按文章发布日期排序。
  • modified – 按文章修改日期排序。
  • parent – 按文章或页面的父 ID 排序
  • rand – 随机排序
  • comment_count – 按文章评论数排序
  • relevance – 按相关性排序,根据以下顺序按搜索词排序:首先是否匹配整个句子,其次如果所有搜索词都在标题内,第三是否有任何搜索词出现在标题中,第四是否完整的句子出现在内容中。
  • menu_order – 按照页面的顺序排序。常用于页面(排序字段在页面编辑页面的「页面属性」框中),也可用于具有不同 menu_order 值的任何文章类型(默认值都是 0)。
  • meta_value – 按照自定义字段排序,请先确保在查询中已经设置了 meta_key,额外要注意,是按字母顺序排列的,这对于字符串来说没有问题,但对于数字可以结果不是你预期的,(例如结果是 1、3、34、4、56、6 ,而不是 1、3、4、6、 34, 56)。如果要按照数字排序,请使用 meta_value_num 代替数值。如果要将自定义字段的值转换为特定类型之后再排序,可以通过指定 meta_type 来实现,可以这些类型:NUMERICBINARYCHARDATEDATETIMEDECIMALSIGNEDTIMEUNSIGNED。也可以使用 meta_value_* 来指定,例如转换为 DATETIME 类型时,也可以使用 meta_value_datetime 来作为 orderby 参数。
  • meta_value_num – 按照数字类型的自定义字段排序。
  • post__in – 按照 post__in 参数中给出的文章 ID 顺序进行排序,注意使用 post__inorder 参数的值无效。
  • post_name__in – 按照 post_name__in 参数中给出的文章名称(URL别名)顺序进行排序,同样这时候 order 参数的值无效。
  • post_parent__in – 按照 post_parent__in 参数中给出的文章父 ID 顺序进行排序,同样这时候 order 参数的值无效。

按照文章标题降序:

 
 
 
$args = array(
‘orderby’ => ‘title’,
‘order’ => ‘DESC’,
);
$query = new WP_Query( $args );

先按照「页面顺序」排序,然后再按文章标题,降序:

 
 
 
$args = array(
‘orderby’ => ‘menu_order title’,
‘order’ => ‘DESC’,
);
$query = new WP_Query( $args );

随机显示一遍文章:

 
 
 
$args = array(
‘orderby’ => ‘rand’,
‘posts_per_page’ => ‘1’,
 
);
$query = new WP_Query( $args );

按照评论数排序(最受欢迎):

 
 
 
$args = array(
‘orderby’ => ‘comment_count’
);
$query = new WP_Query( $args );

获取按照价格自定义字段排序的商品:

 
 
 
$args = array(
‘post_type’ => ‘product’,
‘orderby’ => ‘meta_value_num’,
‘meta_key’ => ‘price’,
);
$query = new WP_Query( $args );

按照标题降序,页面顺序升序:

 
 
 
$args = array(
‘orderby’ => array( ‘title’ => ‘DESC’, ‘menu_order’ => ‘ASC’ )
);
$query = new WP_Query( $args );

按照数字类型自定义字段降序,标题升序:

 
 
 
$args = array(
‘orderby’ => array( ‘meta_value_num’ => ‘DESC’, ‘title’ => ‘ASC’ ),
‘meta_key’ => ‘age’
);
$query = new WP_Query( $args );

获取 my_custom_post_type 的文章,按照数字自定义字段 age 排序,并通过 meta_query 过滤数据只显示 age 为:3-4 的数据

 
 
 
$args = array(
‘post_type’ => ‘my_custom_post_type’,
‘meta_key’ => ‘age’,
‘orderby’ => ‘meta_value_num’,
‘order’ => ‘ASC’,
‘meta_query’ => array(
array(
‘key’ => ‘age’,
‘value’ => array( 3, 4 ),
‘compare’ => ‘IN’,
),
),
);
$query = new WP_Query( $args );

如果要通过两个自定义字段来排序,比如先通过 city 升序 然后 province 降序,这时候就需要通过「命名的 meta 查询」(named meta queries)来组合并链接 meta_query 到 orderby 数组:

 
 
 
$q = new WP_Query( array(
‘meta_query’ => array(
‘relation’ => ‘AND’,
‘province_clause’ => array(
‘key’ => ‘province’,
‘value’ => ‘广东省’,
),
‘city_clause’ => array(
‘key’ => ‘city’,
‘compare’ => ‘EXISTS’,
),
),
‘orderby’ => array(
‘city_clause’ => ‘ASC’,
‘province_clause’ => ‘DESC’,
),
) );

调用内容循环标签

目标:循环调用某个文章类型;可以设置调用的分类,数量,排序;显示的内容列表包括调用图片、链接、标题。

 
 
 
<?php
// 设置查询参数
$args = array(
‘post_type’ => ‘your_post_type’, // 替换为你的自定义文章类型
‘cat’ => ‘your_category_id’, // 替换为你的分类的 ID
‘posts_per_page’ => 5, // 要显示的文章数量
‘orderby’ => ‘date’, // 排序方式(按日期排序)
‘order’ => ‘DESC’, // 排序顺序(降序)
);
 
// 创建查询对象
$query = new WP_Query($args);
 
// 检查查询是否有结果
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// 获取文章特色图像
$thumbnail = get_the_post_thumbnail_url();
// 获取文章链接
$permalink = get_permalink();
// 获取文章标题
$title = get_the_title();
// 获取文章日期
$date = get_the_date(‘Y-m-d’); // 根据你的需求设置日期格式
 
// 输出显示的内容
echo ‘<div class=”post-item”>’;
echo ‘<a href=”‘ . $permalink . ‘”>’;
echo ‘<img src=”‘ . $thumbnail . ‘” alt=”‘ . $title . ‘”>’;
echo ‘</a>’;
echo ‘<h2><a href=”‘ . $permalink . ‘”>’ . $title . ‘</a></h2>’;
echo ‘</div>’;
}
 
// 重置文章数据
wp_reset_postdata();
} else {
echo ‘没有找到相关文章。’;
}
?>

默认文章类型列表页左侧分类调用标签

 
 
 
<?php
$current_category = get_queried_object(); // 获取当前分类对象
 
// 获取当前分类的上一级分类
$parent_category = get_term($current_category->parent, ‘category’);
 
// 获取上一级分类下的所有子分类
if ($current_category->parent == 0) {
$child_categories = get_categories(array(
‘child_of’ => $current_category->term_id,
‘hide_empty’ => 0 // 显示空分类
));
} else {
$child_categories = get_categories(array(
‘child_of’ => $parent_category->term_id,
‘hide_empty’ => 0 // 显示空分类
));
}
//var_dump($child_categories);
 
foreach ($child_categories as $child_category) {
$active_class = ($child_category->term_id == $current_category->term_id) ? ‘nav-current’ : ;
 
echo ‘<li class=”‘ . $active_class . ‘”>’;
echo ‘<a href=”‘ . get_category_link($child_category) . ‘”>’ . $child_category->name . ‘</a>’;
 
// 检查子分类是否有内容
$child_category_posts = get_posts(array(
‘category’ => $child_category->term_id,
‘posts_per_page’ => 5, // 设置每个分类下显示的文章数量
));
 
if ($child_category_posts) {
echo ‘<ul class=”sub-menu”>’;
 
foreach ($child_category_posts as $post) {
echo ‘<li><a href=”‘ . get_permalink($post) . ‘”>’ . $post->post_title . ‘</a></li>’;
}
 
echo ‘</ul>’;
}
 
echo ‘</li>’;
}
 
?>

默认文章类型详情页左侧分类调用标签
目标:如果是分类ID的父ID为0,则父ID为本栏目ID;如果父ID不为0,则父ID是父ID。子类下面有内容,则输出内容列表。这样做的目的是用户点击父ID则显示该分类下的子分类,点击子分类则显示子栏目对应父ID下的所有子分类ID。被点击的分类设置新的样式。

 
 
 
<?php
$category = get_the_category(); //详情页中获取所属分类信息
$category_id = $category[0]>cat_ID; //当前文章所属分类的ID
$category_parent_id = $category[0]>category_parent; //父分类的ID
 
if ($category[0]>category_parent == 0) {
$child_categories = get_categories(array(
‘child_of’ => $category_id,
‘hide_empty’ => 0 // 显示空分类
));
} else {
$child_categories = get_categories(array(
‘child_of’ => $category[0]>category_parent,
‘hide_empty’ => 0 // 显示空分类
));
}
 
// 循环输出子类
foreach ($child_categories as $child_category) {
echo ‘<li><a href=”‘ . get_category_link($child_category->term_id) . ‘”>’ . $child_category->name . ‘</a>’;
 
// 获取子类下的内容列表
$child_category_args = array(
‘cat’ => $child_category->term_id,
‘posts_per_page’ => -1 // 显示所有内容
);
$child_category_query = new WP_Query($child_category_args);
 
// 循环输出子类下的内容
if ($child_category_query->have_posts()) {
echo ‘<ul class=”sub-menu”>’;
while ($child_category_query->have_posts()) {
$child_category_query->the_post();
echo ‘<li><a href=”‘ . get_permalink() . ‘”>’ . get_the_title() . ‘</a></li>’; // 输出内容标题和链接
}
echo ‘</ul>’;
}
 
echo ‘</li>’;
 
wp_reset_postdata(); // 重置内容查询
}
?>

自定义文章类型列表页左侧分类调用标签

 
 
 
<?php
$current_category_id = get_queried_object_id(); // 获取当前分类的 ID
//$current_category = get_queried_object(); // 获取当前分类对象
$current_category = get_term($current_category_id); // 获取当前分类对象
// 判断当前分类是否有父分类
if ($current_category->parent == 0) {
$parent_category_id = $current_category_id;
} else {
$parent_category_id = $current_category->parent;
}
 
$child_categories = get_terms(array(
‘taxonomy’ => $current_category->taxonomy,
‘parent’ => $parent_category_id,
‘hide_empty’ => 0 // 显示空分类
));
 
foreach ($child_categories as $child_category) {
echo ‘<li’;
if ($child_category->term_id == $current_category_id) {
echo ‘ class=”nav-current”‘;
}
echo ‘>’;
echo ‘<a href=”‘ . get_term_link($child_category) . ‘”>’ . $child_category->name . ‘</a>’;
 
//halt($child_category->taxonomy);
// 判断子分类下是否有内容
$args = array(
‘post_type’ => ‘products’,
‘tax_query’ => array(
array(
‘taxonomy’ => $child_category->taxonomy,
‘field’ => ‘term_id’,
‘terms’ => $child_category->term_id,
),
),
);
//$posts = get_posts($args);
//halt($posts);
$query = new WP_Query($args);
if ($query->have_posts()) {
echo ‘<ul class=”sub-menu”>’;
while ($query->have_posts()) {
$query->the_post();
echo ‘<li><a href=”‘ . get_permalink() . ‘”>’ . get_the_title() . ‘</a></li>’;
}
echo ‘</ul>’;
}
wp_reset_postdata();
echo ‘</li>’;
}
?>

自定义文章类型详情页左侧分类调用标签

 
 
 
<?php
$categories = get_the_terms(get_the_ID(), ‘products_category’);
if ($categories && !is_wp_error($categories)) {
$category_id = $categories[0]>term_id;
$category_parent_id = $categories[0]>parent;
}
//halt($categories);
 
// 判断当前分类是否有父分类
if ($category_parent_id == 0) {
$parent_category_id = $category_id;
} else {
$parent_category_id = $category_parent_id;
}
 
$child_categories = get_terms(array(
‘taxonomy’ => $categories[0]>taxonomy,
‘parent’ => $parent_category_id,
‘hide_empty’ => 0 // 显示空分类
));
//halt($child_categories);
 
foreach ($child_categories as $child_category) {
echo ‘<li’;
if ($child_category->term_id == $category_id) {
echo ‘ class=”nav-current”‘;
}
echo ‘>’;
echo ‘<a href=”‘ . get_term_link($child_category) . ‘”>’ . $child_category->name . ‘</a>’;
 
//halt($child_category->taxonomy);
// 判断子分类下是否有内容
$args = array(
‘post_type’ => ‘products’,
‘tax_query’ => array(
array(
‘taxonomy’ => $child_category->taxonomy,
‘field’ => ‘term_id’,
‘terms’ => $child_category->term_id,
),
),
);
//$posts = get_posts($args);
//halt($posts);
$query = new WP_Query($args);
if ($query->have_posts()) {
echo ‘<ul class=”sub-menu”>’;
while ($query->have_posts()) {
$query->the_post();
echo ‘<li><a href=”‘ . get_permalink() . ‘”>’ . get_the_title() . ‘</a></li>’;
}
echo ‘</ul>’;
}
wp_reset_postdata();
echo ‘</li>’;
}
?>

内容循环列表调用标签

目标:wordpress循环调用某个文章类型;可以设置调用的分类,数量,排序;显示的内容列表包括调用图片、链接、简短内容、标题,其中标题,简短内容可以设置字数。

 
 
 
<?php
// 默认文章类型设置查询参数
$args = array(
‘post_type’ => ‘your_post_type’, // 替换为你的自定义文章类型
‘cat’ => ‘your_category_id’, // 替换为你的分类的 ID
‘posts_per_page’ => 5, // 要显示的文章数量
‘orderby’ => ‘date’, // 排序方式(按日期排序)
‘order’ => ‘DESC’, // 排序顺序(降序)
);
//自定义文章类型设置查询参数,与上面的只需要选其中一种
// 设置查询参数
$args = array(
‘post_type’ => ‘your_custom_post_type’, // 替换为你的自定义文章类型
‘tax_query’ => array(
array(
‘taxonomy’ => ‘your_taxonomy’, // 替换为你的分类法(taxonomy)名称
‘field’ => ‘slug’,
‘terms’ => ‘your_category_slug’, // 替换为你的分类别名(slug)
),
),
‘posts_per_page’ => 5, // 要显示的文章数量
‘orderby’ => ‘date’, // 排序方式(按日期排序)
‘order’ => ‘DESC’, // 排序顺序(降序)
);
 
// 创建查询对象
$query = new WP_Query($args);
 
// 检查查询是否有结果
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// 获取文章特色图像
$thumbnail = get_the_post_thumbnail_url();
// 获取文章链接
$permalink = get_permalink();
// 获取文章标题
$title = get_the_title();
// 获取文章简短内容并限制字数
$excerpt = wp_trim_words(get_the_excerpt(), 20); // 替换 20 为你希望的字数
 
// 输出显示的内容
echo ‘<div class=”post-item”>’;
echo ‘<a href=”‘ . $permalink . ‘”>’;
echo ‘<img src=”‘ . $thumbnail . ‘” alt=”‘ . $title . ‘”>’;
echo ‘</a>’;
echo ‘<h2><a href=”‘ . $permalink . ‘”>’ . $title . ‘</a></h2>’;
echo ‘<p>’ . $excerpt . ‘</p>’;
echo ‘</div>’;
}
 
// 重置文章数据
wp_reset_postdata();
} else {
echo ‘没有找到相关文章。’;
}
?>

wordpress 文章类型调用缩略图

 
 
 
<?php
$args = array(
‘post_type’ => ‘attachment’,
‘post_status’ => ‘inherit’,
‘post_parent’ => get_the_ID(),
‘post_mime_type’ => ‘image’,
‘posts_per_page’ => -1,
‘order’ => ‘ASC’,
);
 
$query = new WP_Query($args);
 
if ($query->have_posts()) {
$current_class = ‘current’; // 当前附件的样式
$first_attachment = true; // 用于跟踪第一张附件
 
while ($query->have_posts()) {
$query->the_post();
 
// 在这里进行你想要的操作
$attachment_id = get_the_ID();
$attachment_title = get_the_title();
$attachment_url = wp_get_attachment_url($attachment_id);
 
// 设置第一张附件的样式为 “current”
$attachment_class = ($first_attachment) ? $current_class : ;
 
?>
<li class=“image-item <?php echo esc_attr($attachment_class); ?>>”><a class=“cloud-zoom-gallery item” href=“<?php echo esc_url($attachment_url); ?>” data-zoom=“useZoom:zoom1, smallImage:<?php echo esc_url($attachment_url); ?>”><img src=“<?php echo esc_url($attachment_url); ?>” alt=“<?php echo esc_attr($attachment_title); ?>” /></a></li>
 
<?php
 
$first_attachment = false; // 标记第一张附件已经处理过
}
 
// 恢复原始文章数据
wp_reset_postdata();
} else {
echo ‘没有找到满足条件的附件。’;
}
?>

wordpress 自定义文章类型调用缩略图
目标:自定义文章类型的post_type都是attachment,所以调用缩略图片连带内容上传的图片都会调出来,所以需要通过读取post_meta的内容中来判断是否是设置的缩略图片。涉及的函数是:

 
 
 
t h u m b n a i l i d = M u l t i P o s t T h u m b n a i l s : : g e t p o s t t h u m b n a i l i d ( ′ p r o d u c t s ′ , ′ c u s t o m − i m a g e ′ . thumbnail_id = MultiPostThumbnails::get_post_thumbnail_id(‘products’, ‘custom-image’.thumbnail
i
d=MultiPostThumbnails::get
p
ost
t
humbnail
i
d(
products
,
custom−image
.i, $post_id);

functions.php 设置缩略图片

 
 
 
if (class_exists(‘MultiPostThumbnails’)) {
new MultiPostThumbnails(
array(
‘label’ => ‘特色图片2’,
‘id’ => ‘custom-image2’,
‘post_type’ => ‘products’
)
);
new MultiPostThumbnails(
array(
‘label’ => ‘特色图片3’,
‘id’ => ‘custom-image3’,
‘post_type’ => ‘products’
)
);
new MultiPostThumbnails(
array(
‘label’ => ‘特色图片4’,
‘id’ => ‘custom-image4’,
‘post_type’ => ‘products’
)
);
new MultiPostThumbnails(
array(
‘label’ => ‘特色图片5’,
‘id’ => ‘custom-image5’,
‘post_type’ => ‘products’
)
);
}

调用图片

 
 
 
<div class=“product-view”>
<?php
if (has_post_thumbnail()) {
$thumbnail_id = get_post_thumbnail_id();
$thumbnail_url = wp_get_attachment_image_src($thumbnail_id, ‘full’)[0];
}
?>
<div class=“product-image”><a class=“cloud-zoom” id=“zoom1” data-zoom=“adjustX:0, adjustY:0” href=“<?php echo $thumbnail_url ?>”>
<img src=“<?php echo $thumbnail_url ?>” itemprop=“image” title alt=“<?php the_title(); ?> “ style=“width:100%” /></a>
</div>
<div class=“image-additional”>
<ul class=“image-items”>
<?php
//获取自定义文章类型特色图片
$post_id = get_the_ID(); //获取当前文章ID
$post_title = get_the_title();
 
//文章自带图片
echo ‘<li class=”image-item current”><a class=”cloud-zoom-gallery item” href=”‘. esc_url($thumbnail_url) .‘” data-zoom=”useZoom:zoom1, smallImage:’.$thumbnail_url.‘”><img src=”‘.$thumbnail_url.‘” alt=”‘.esc_attr($post_title).‘” /></a></li>’;
//$current_class = ‘current’; // 当前附件的样式
//$first_attachment = true; // 用于跟踪第一张附件
 
for ($i = 2; $i <= 5; $i++) {
$thumbnail_id = MultiPostThumbnails::get_post_thumbnail_id(‘products’, ‘custom-image’.$i, $post_id);
// 检查是否存在缩略图
if ($thumbnail_id) {
//$attachment_class = ($first_attachment) ? $current_class : ”; // 设置第一张附件的样式为 “current”
$thumbnail_url = wp_get_attachment_image_src($thumbnail_id, ‘full’)[0];
echo ‘<li class=”image-item”><a class=”cloud-zoom-gallery item” href=”‘. esc_url($thumbnail_url) .‘” data-zoom=”useZoom:zoom1, smallImage:’.$thumbnail_url.‘”><img src=”‘.$thumbnail_url.‘” alt=”‘.esc_attr($post_title).‘” /></a></li>’;
//$first_attachment = false; // 标记第一张附件已经处理过
}
}
?>
</ul>
</div>
</div>

为什么自定义文章类型的参数设置的方式与默认文章参数设置不一样?
自定义文章类型的参数设置与默认文章参数设置不同,是因为自定义文章类型是根据你的特定需求创建的,并且它们具有不同的属性和功能。以下是一些原因解释为什么自定义文章类型的参数设置方式与默认文章不同:

独立性和灵活性:自定义文章类型具有独立的属性和功能,可以根据你的需求进行自定义设置。这使得你能够创建与默认文章类型(post)不同的、具有不同特性的文章类型,例如产品、案例研究、活动等。

特定标识和分类:自定义文章类型通常具有自己的分类法(taxonomy),用于对其进行分类和组织。因此,参数设置需要允许你指定自定义文章类型及其相关分类的信息。

单独的模板和功能:自定义文章类型可能需要单独的模板文件和功能定制,以满足其独特的展示需求。通过使用不同的参数设置方式,可以为自定义文章类型提供更大的灵活性和定制性。

可扩展性:WordPress 的自定义文章类型是一个可扩展的功能,它允许开发者创建符合自己需求的完全定制化的文章类型。参数设置的灵活性和多样性能够满足各种不同的扩展需求。

总而言之,自定义文章类型是为了满足特定需求而创建的,因此具有独立的参数设置方式。这样可以确保你可以根据自己的需求和目标,灵活地定义自定义文章类型的属性、分类、展示方式等。

温馨提示:本文最后更新于2024-04-07 22:42:31,某些文章具有时效性,若有错误或已失效,请私信客服或联系 语画
------本文内容已结束,喜欢请分享------

感谢您的访问,Ctrl+D收藏本站吧。

© 版权声明
四月 7

语岛上的今时往日

    "吼吼~,往年的今天,作者不知道跑哪里偷懒去了~"
WWW.YUHUA6.CN
点赞2 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容