spot_img
HomeJoomla教程Joomla教程菜鸟教程 创建一个短代码以显示具有特定分类的自定义帖子类型

Joomla教程菜鸟教程 创建一个短代码以显示具有特定分类的自定义帖子类型

spot_img

JasperAI 10000字免费额度试用

我创建了一个页面模板来列出特定产品线的所有产品。 现在我想根据每个页面的短代码中描述的分类法列出来自此自定义帖子类型(产品)的所有帖子。

例子:

“所有 Prime 产品列表”页面

[products line=”prime”]

WordPress divi主题

我试过这段代码:

function shortcode_mostra_produtos ( $atts ) {
  $atts = shortcode_atts( array(
    'default' => ''
  ), $atts );
    $terms = get_terms('linhas');
    wp_reset_query();
    $args = array('post_type' => 'produtos',
      'tax_query' => array(
        array(
          'taxonomy' => 'linhas',
          'field' => 'slug',
          'terms' => $atts,
        ),
      ),
     );
     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        while($loop->have_posts()) : $loop->the_post();
            echo ' "'.get_the_title().'" ';
        endwhile;
     }
}
add_shortcode( 'produtos','shortcode_mostra_produtos' );

首先,在期间注册短代码总是好的 init 与一般情况相比 functions.php 文件。 至少 add_shortcode() 应该在 init. 不管怎样,让我们​​开始吧!

每当你使用 add_shortcode() 第一个参数将是短代码的名称,第二个参数将是回调函数。 这意味着:

[products line="prime"]

应该改为:

[produtos line="prime"]

到目前为止,我们有这个:

高质量外链购买
/**
 * Register all shortcodes
 *
 * @return null
 */
function register_shortcodes() {
    add_shortcode( 'produtos', 'shortcode_mostra_produtos' );
}
add_action( 'init', 'register_shortcodes' );

/**
 * Produtos Shortcode Callback
 * - [produtos]
 * 
 * @param Array $atts
 *
 * @return string
 */
function shortcode_mostra_produtos( $atts ) {
    /** Our outline will go here
}

让我们看一下处理属性。 道路 shortcode_atts() 有效的是它将尝试将传递给短代码的属性与传递的数组中的属性进行匹配,左侧是键,右侧是默认值。 所以我们需要改变 defaultsline 相反 – 如果我们想默认一个类别,这将是这个地方:

$atts = shortcode_atts( array(
    'line' => ''
), $atts );

如果用户向简码添加属性 line="test" 然后我们的数组索引 line 将举行 test:

echo $atts['line']; // Prints 'test'

所有其他属性将被忽略,除非我们将它们添加到 shortcode_atts() 大批。 最后,它只是 WP_Query 并打印您需要的内容:

WordPress建站服务
/**
 * Register all shortcodes
 *
 * @return null
 */
function register_shortcodes() {
    add_shortcode( 'produtos', 'shortcode_mostra_produtos' );
}
add_action( 'init', 'register_shortcodes' );

/**
 * Produtos Shortcode Callback
 * 
 * @param Array $atts
 *
 * @return string
 */
function shortcode_mostra_produtos( $atts ) {
    global $wp_query,
        $post;

    $atts = shortcode_atts( array(
        'line' => ''
    ), $atts );

    $loop = new WP_Query( array(
        'posts_per_page'    => 200,
        'post_type'         => 'produtos',
        'orderby'           => 'menu_order title',
        'order'             => 'ASC',
        'tax_query'         => array( array(
            'taxonomy'  => 'linhas',
            'field'     => 'slug',
            'terms'     => array( sanitize_title( $atts['line'] ) )
        ) )
    ) );

    if( ! $loop->have_posts() ) {
        return false;
    }

    while( $loop->have_posts() ) {
        $loop->the_post();
        echo the_title();
    }

    wp_reset_postdata();
}

    add_shortcode( 'product-list','bpo_product_list' );
function bpo_product_list ( $atts ) {
  $atts = shortcode_atts( array(
    'category' => ''
  ), $atts );
    $terms = get_terms('product_category');
    wp_reset_query();
    $args = array('post_type' => 'product',
      'tax_query' => array(
        array(
          'taxonomy' => 'product_category',
          'field' => 'slug',
          'terms' => $atts,
        ),
      ),
     );
     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        while($loop->have_posts()) : $loop->the_post();
            echo ' "'.get_the_title().'" ';
        endwhile;
     }

     else {
            echo  'Sorry, no posts were found';
          }
}

在上面的代码中,我为产品 CPT 创建了产品 CPT 和 product_category 分类法。

[product-list category=”shirts”]

上面的代码是完美的作品!

wordpress建站公司

5分钟生成10篇英文软文article forge软件试用
siteground guangda
WordPress花园官方账号
WordPress花园隶致力于为广大跨境电商和独立站爱好者提供优质的WordPress教程、Woocommerce教程、Facebook、Twitter、tiktok、Instagram教程和谷歌SEO教程等资料和对应的建站推广服务。关注‘哟派出海’公众号了解最新资讯。粉丝福利:Shopline免费独立站建设14天优惠 商务合作: [email protected]
RELATED ARTICLES