wordpress仿站常用功能代码

news/2024/10/22 12:30:12/

wordpress调用代码

调用内容循环标签

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

<?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; //父分类的IDif ($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'. thumbnailid=MultiPostThumbnails::getpostthumbnailid(products,customimage.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"><?phpif (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>

为什么自定义文章类型的参数设置的方式与默认文章参数设置不一样?

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

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

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

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

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

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

希望这解答了你的疑问!如有其他问题,请随时提问。


http://www.ppmy.cn/news/893791.html

相关文章

macOS 开发 - NSAlert

文章目录 关于 NSAlert代码实现简单弹出Alert 类型贴着窗口 beginSheetModalForWindow添加按钮关于 NSAlert 官方文档:https://developer.apple.com/documentation/appkit/nsalert代码实现 简单弹出 - (void)showAlert3{NSAlert *alert

我被感动了!世间自有真爱 - 林林与静静

其实很早就有了&#xff0c;不过今天才知道&#xff0c;内容不知是否属实&#xff0c;但它动感动了我。 你会被感动吗&#xff1f; 原文&#xff1a; 我是一名搞计算机的,记得2001年那年我去了北京,然后一直在北京工作,日子过的挺苦的拉,因为一个自己到了一个陌生的城市,也没有…

一篇感人的文章

我是一名搞计算机的,记得2001年那年我去了北京,然后一直在北京工作,日子过的挺苦的拉,因为一个自己到了一个陌生的城市,也没有什么朋友,那就叫一个郁闷那,我是住在玄武门那边的,住了个破四合院,为了省钱每天早上就随便吃点门口卖的点心,记的那个点心店是叫清真寺,回族人开的,他…

感人的爱情故事

我是一名搞计算机的,记得2001年那年我去了北京,然后一直在北京工作,日子过的挺苦的拉,因为一个自己到了一个陌生的城市,也没有什么朋友,那就叫一个郁闷那,我是住在玄武门那边的,住了个破四合院,为了省钱每天早上就随便吃点门口卖的点心,记的那个点心店是叫清真寺,回族人开的,他…

运行sql文件出错

运行sql文件出错 报错&#xff1a; Unknown collation: ‘utf8mb4_0900_ai_ci’ 报错原因&#xff1a; 生成转储文件的数据库版本为8.0,要导入sql文件的数据库版本为5.6,因为是高版本导入到低版本&#xff0c;引起1273错误 解决方法&#xff1a; 打开sql文件&#xff0c;将文…

c#TextBox输入框自动提示、自动完成、自动补全功能

c#TextBox输入框自动提示、自动完成、自动补全功能 在C#中,可以使用AutoCompleteMode属性和AutoCompleteSource属性来实现TextBox输入框的自动提示、自动完成和自动补全功能。 首先,设置TextBox的AutoCompleteMode属性为Suggest或SuggestAppend。Suggest模式会根据用户输入…

PX4最新版固件

最近研究PX4在Ubuntu20上的仿真&#xff0c;然后下载PX4并且更新了子模块&#xff0c;特地备份一下&#xff0c;也分享出来 可以看到是所有子模块都更新完成的&#xff0c;我是在make px4 gazebo之前备份的&#xff0c;就是为了存档 链接: https://pan.baidu.com/s/1L7wICLdm…

iPhone4 iOS 4.3.3 越狱之后必装的插件

关于 iPhone4 如何越狱不在本文探讨范围之内,这里只是记录一下必装的 Cydia 插件,方便使用。 如果有其他疑问,请先参考: iPhone 4.3.3 重刷 4.3.3 固件教程 及 一键越狱 Cydia 源地址大合集 iPhone / iPad 文件目录介绍 进入正题,添加完 cydia 源之后,必装的插件有: 插件…