权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>WooCommerce product title formatting
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WooCommerce product title formatting

programmeradmin5浏览0评论

What is the best way to format my product titles?

I would like the brand of the product to be on it’s own line in a larger bolder font and then underneath the name of the products in a smaller more refined format.

I see I can add the markup straight into the product title in wordpress and use a line break to separate the brand name and the product name, but is there a more efficient way to achieve this?

I came across this code recommended on a thread where you use a pipe in the product title and it converts it to to a line break but it doesn’t appear to work and just adds a pipe to my product title, I added it to my functions.php

I am using Elementor for the majority of the woocommerce page design

Thanks in advance

//ADD LINE BREAK
add_filter( 'the_title', 'custom_the_title', 10, 2 );

function custom_the_title( $title, $post_id ) {
    $post_type = get_post_field( 'post_type', $post_id, true );

    if( $post_type === 'product' || $post_type === 'product_variation' ) {
        $title = str_replace( '|', '<br/>', $title ); // we replace '|' by '<br>'
    }

    return $title;
}

What is the best way to format my product titles?

I would like the brand of the product to be on it’s own line in a larger bolder font and then underneath the name of the products in a smaller more refined format.

I see I can add the markup straight into the product title in wordpress and use a line break to separate the brand name and the product name, but is there a more efficient way to achieve this?

I came across this code recommended on a thread where you use a pipe in the product title and it converts it to to a line break but it doesn’t appear to work and just adds a pipe to my product title, I added it to my functions.php

I am using Elementor for the majority of the woocommerce page design

Thanks in advance

//ADD LINE BREAK
add_filter( 'the_title', 'custom_the_title', 10, 2 );

function custom_the_title( $title, $post_id ) {
    $post_type = get_post_field( 'post_type', $post_id, true );

    if( $post_type === 'product' || $post_type === 'product_variation' ) {
        $title = str_replace( '|', '<br/>', $title ); // we replace '|' by '<br>'
    }

    return $title;
}
Share Improve this question edited Sep 17, 2020 at 15:33 phatskat 3,1741 gold badge18 silver badges26 bronze badges asked Sep 10, 2020 at 19:02 laxuslaxus 133 bronze badges 8
  • Hi, you are adding brand name directly in the title or using custom field or taxonomy for brand selection. – MMK Commented Sep 11, 2020 at 8:35
  • Hi there The brand name as well as product name is directly in the title currently – laxus Commented Sep 11, 2020 at 8:45
  • In this case you need to put pipe sign in the backend and above code in functions.php on runtime it will replace pipe with <br>.. The other way around is to use custom taxonomomies (like categories) or add brand as custom taxonomy i will put the code later... it is more efficient in a way that many brand repeat most of the time so you just need to select it. plus in front end you can create filter by brand too. – MMK Commented Sep 11, 2020 at 9:27
  • I don't see how adding a pipe is any more efficient than adding a <br/> tag to the title when creating a product. You're still just adding something to the title & then you're adding another function for WordPress to process when it loads a page. Just stick with <span>Brand</span> Product Name in the title & then use CSS to apply the styling you need. Personally,I 'd make the Brand a custom taxonomy & then re-work the product template to display it exactly as I want it. That then gives you opportunities to do lots of other things, like filtering the products by Brand names, etc. – Tony Djukic Commented Sep 24, 2020 at 14:56
  • 1 @TonyDjukic Hi Tony, for some reason I didn't get a notification for your response, I have gone ahead with Muneebs answer, thank you for your input though on the subject – laxus Commented Nov 10, 2020 at 23:53
 |  Show 3 more comments

1 Answer 1

Reset to default 2

Add Brand as taxonomy:

if ( ! function_exists( 'brand_tax' ) ) {

// Register Custom Taxonomy
function brand_tax() {

    $labels = array(
        'name'                       => _x( 'Brands', 'Taxonomy General Name', 'text_domain' ),
        'singular_name'              => _x( 'Brand', 'Taxonomy Singular Name', 'text_domain' ),
        'menu_name'                  => __( 'Brands', 'text_domain' ),
        'all_items'                  => __( 'All brands', 'text_domain' ),
        'parent_item'                => __( 'Parent brand', 'text_domain' ),
        'parent_item_colon'          => __( 'Parent brand:', 'text_domain' ),
        'new_item_name'              => __( 'New brand', 'text_domain' ),
        'add_new_item'               => __( 'Add New brand', 'text_domain' ),
        'edit_item'                  => __( 'Edit brand', 'text_domain' ),
        'update_item'                => __( 'Update brand', 'text_domain' ),
        'view_item'                  => __( 'View brand', 'text_domain' ),
        'separate_items_with_commas' => __( 'Separate brands with commas', 'text_domain' ),
        'add_or_remove_items'        => __( 'Add or remove brands', 'text_domain' ),
        'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
        'popular_items'              => __( 'Popular brands', 'text_domain' ),
        'search_items'               => __( 'Search brands', 'text_domain' ),
        'not_found'                  => __( 'Not Found', 'text_domain' ),
        'no_terms'                   => __( 'No brands', 'text_domain' ),
        'items_list'                 => __( 'brands list', 'text_domain' ),
        'items_list_navigation'      => __( 'brands list navigation', 'text_domain' ),
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
    );
    register_taxonomy( 'brand', array( 'product' ), $args );

}
add_action( 'init', 'brand_tax', 0 );

}

Modify your code like this now

add_filter( 'the_title', 'custom_the_title', 10, 2 );  
function custom_the_title( $title, $post_id ) {      
    $post_type = get_post_field( 'post_type', $post_id, true );  
    if( $post_type == 'product' ){
        $terms = get_the_terms( $post_id, 'brand' );
        foreach($terms as $term){
            $output .=  "<span>".$term->name."</span>";
        }
        $title .= "<br><p>".$output ."</p>";
    }
    return $title;  
}

You can change markup as per your design..

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论