当前位置: 首页 > article >正文

wordpress仿站常用功能代码

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; //父分类的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'. 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">
                <?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>

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

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

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

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

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

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

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

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


http://www.kler.cn/a/37999.html

相关文章:

  • HarmonyOS/OpenHarmony应用开发-Stage模型UIAbility组件使用(四)
  • Java Vue物联网系统
  • vagrant和vitrulBox创建虚拟机后使用xshell连接
  • Android 中利用多个Button组合实现选项切换效果
  • layui入门
  • AI辅助瞄准系统开发与实战(二)
  • 二级分销小程序怎么做
  • 【自我提升】JPA从搭建到CRUD快速入门(IDEA+MAVEN)
  • 【Linux系列P6】自动化构建工具-make/Makefile详解
  • 【跨平台开发】Uni-app原理分析
  • 安全启动相关命令使用
  • 网页无插件播放265/264视频/监控大屏/GPU解码
  • Win32 汇编在对话框上画线
  • 【Docker】Docker高级网络(NetWork)
  • k8s 2003面试题(1):k8s有哪些特性?
  • 基于vue3+pinia2仿ChatGPT聊天实例|vite4.x仿chatgpt界面
  • 算法06-搜索算法-广度优先搜索
  • ❤️创意网页:如何用HTML制作菜单栏?制作好看的菜单栏样式网页
  • AA@有理系数多项式@整系数多项式@本原多项式@有理多项式可约问题
  • SpringBoot——在测试阶段验证Web表现层的接口是否正常