2025年1月18日星期六

Shortcodes Ultimate插件添加自定义模板路径代码和简码实例

原文链接:https://www.chenweiliang.com/cwl-32423.html


有没有想过,如何在 Shortcodes Ultimate 插件中,自定义一个独一无二的模板路径?


不需要再去羡慕那些玩转 WordPress 的技术大神,因为现在我就带你揭开这背后的秘密!


来吧,让我们直奔主题,一步步搞定!


什么是 Shortcodes Ultimate 插件?


Shortcodes Ultimate 是 WordPress 中广受欢迎的插件之一,它可以通过简码轻松实现各种功能,比如创建按钮、图像轮播、文章列表等……


更棒的是,我们可以自定义模板路径,从而更灵活地展示内容。


如何添加自定义模板路径?


我们先来看最关键的一步——添加自定义模板路径。


这是实现个性化展示的第一步!


Shortcodes Ultimate插件添加自定义模板路径代码和简码实例


代码示例


在你的主题 functions.php 文件或自定义插件中添加以下代码:


add_filter(
'su/shortcode/posts/allowed_template_locations',
function( $locations )
// 添加自定义模板路径 /wp-content/custom-templates/
$locations[] = WP_CONTENT_DIR . '/custom-templates';

return $locations;
,
10,
1
);

解析代码




  1. 过滤器 su/shortcode/posts/allowed_template_locations

    这个过滤器允许我们扩展 Shortcodes Ultimate 的默认模板路径。




  2. 自定义路径 /wp-content/custom-templates/

    我们将模板存储在 /wp-content/custom-templates/ 目录中,这样更新插件时不会丢失。




  3. 安全性

    永远不要直接修改插件的默认模板文件,以免插件更新后丢失自定义内容。




创建自定义模板文件


接下来,我们需要在自定义路径中创建模板文件。


例如,我们创建一个名为 jiawen.php 的模板文件。


模板文件示例


/wp-content/custom-templates/ 目录下,创建 jiawen.php 文件,并添加以下代码:


<?php defined( 'ABSPATH' ) || exit; ?>
<div class="su-posts su-posts-teaser-loop <?php echo esc_attr( $atts['class'] ); ?>">

<?php if ( $posts->have_posts() ) : ?>
<?php while ( $posts->have_posts() ) : ?>
<?php $posts->the_post(); ?>

<?php if ( ! su_current_user_can_read_post( get_the_ID() ) ) : ?>
<?php continue; ?>
<?php endif; ?>

<div id="su-post-<?php the_ID(); ?>" class="su-post <?php echo esc_attr( $atts['class_single'] ); ?>">
<?php if ( has_post_thumbnail() ) : ?>
<a target="_blank" rel="nofollow" class="su-post-thumbnail" href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
<?php endif; ?>
<div class="su-post-title">
<a target="_blank" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</div>
</div>

<?php endwhile; ?>
<?php else : ?>

<p class="su-posts-not-found"><?php esc_html_e( 'Posts not found', 'shortcodes-ultimate' ); ?></p>

<?php endif; ?>
</div>

模板文件说明



  • $atts['class']:允许我们自定义 CSS 类,为模板设计独特的样式。

  • 文章缩略图和标题:展示文章的缩略图和标题,并链接到文章详情页。

  • 安全性:使用 defined('ABSPATH') || exit; 确保模板文件无法被直接访问。


创建好模板后,我们就可以在文章或页面中通过简码调用它了。


简码实例


在文章或页面中添加以下简码:


【su_posts template="jiawen.php" posts_per_page="-1" ignore_sticky_posts="yes" id="32277,30806"

上面的简码中使用了【】来避免文章中简码被转义,实际使用时需要将符号【】改为标准的[]。


简码参数说明




  1. template="jiawen.php"

    指定自定义模板文件的名称,路径是相对于 /wp-content/ 的。




  2. posts_per_page="-1"

    显示所有文章,没有数量限制。




  3. ignore_sticky_posts="yes"

    忽略置顶文章。




  4. id="32277,30806"

    仅显示指定 ID 的文章。这里指定了两个文章ID,32277和30806。只有这两个ID对应的文章会被显示。




总结



  • 自定义模板路径:通过过滤器扩展默认路径,避免插件更新丢失模板。

  • 创建模板文件:通过 PHP 代码控制文章列表的布局和内容。

  • 调用模板文件:通过简码参数实现灵活调用。


自定义模板路径的功能,极大地提升了 Shortcodes Ultimate 插件的灵活性。


通过这种方法,我们可以完全掌控文章列表的展示方式。


特别是当我们需要在不同页面中实现多样化的设计时,这种方式显得尤为重要。


如果你想让你的 WordPress 网站更具个性化,赶紧动手试试吧!





欢迎转载《Shortcodes Ultimate插件添加自定义模板路径代码和简码实例

欢迎分享本文链接:https://www.chenweiliang.com/cwl-32423.html


网站地址:https://www.chenweiliang.com/



欲获取更多资讯内幕和秘技,欢迎进入Telegram频道:https://www.chenweiliang.com/go/tgchannel

没有评论:

发表评论