I was wondering if it's possible to display value returned from a function on the Main Menu in WordPress? I figured I can use custom links to display text on the menu. Now I would like call a function which calculates the number of users online and display the result on the menu.
Something like chinesepod.com does
Here's the code for calculating the number of users : -
function ray_number_online_users() {
$i = 0;
if ( bp_has_members( ‘user_id=0&type=online&per_page=999&populate_extras=0′ ) ) :
while ( bp_members() ) : bp_the_member();
$i++;
endwhile;
endif;
return $i;
}`
The easiest way to target a single link on your menu is by giving it a class (user-number in this case).
The theme location is defined by the register_nav_menus function
register_nav_menus( array(
'primary' => __( 'Primary Menu', 'twentyfifteen' ),
'social' => __( 'Social Links Menu', 'twentyfifteen' ),
) );
Here I target the primary menu location, but I could also target the social menu.
function so30559666_nav_description( $item_output, $item, $depth, $args ) {
if ( 'primary' == $args->theme_location && in_array("user-number", $item->classes)) {
$count = ray_number_online_users();
$item_output = str_replace( $args->link_after . '</a>', '<div class="menu-user-count">' . $count . '</div>' . $args->link_after . '</a>', $item_output );
}
return $item_output;
}
add_filter( 'walker_nav_menu_start_el', 'so30559666_nav_description', 10, 4 );
Related
I recently installed a plugin for wordpress/woocommerce that allows you to choose a primary category for a product when it's in multiple categories. It works well to change the breadcrumbs, but my theme (peakshops) also displays the primary category under the corresponding thumbnails on the the shop page.
This is the core function that is built into the theme to display the category (and link to the category) under the thumbnail:
// Add Title with Link.
function thb_template_loop_product_title() {
global $product;
$product_url = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
?>
<?php
if ( 'on' === ot_get_option( 'shop_product_listing_category', 'on' ) ) {
$shop_product_listing_category_single = ot_get_option( 'shop_product_listing_category_single', 'on' );
?>
<div class="product-category">
<?php
if ( 'on' === $shop_product_listing_category_single ) {
$product_cats = wc_get_product_category_list( get_the_ID(), '\n', '', '' );
if ( $product_cats ) {
list( $first_part ) = explode( '\n', $product_cats );
echo wp_kses(
$first_part,
array(
'a' => array(
'href' => array(),
'rel' => array(),
),
)
);
}
} else {
echo wc_get_product_category_list( $product->get_id(), ', ' );
}
?>
</div>
<?php } ?>
<h2 class="<?php echo esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ); ?>"><?php the_title(); ?></h2>
<?php
}
add_action( 'woocommerce_shop_loop_item_title', 'thb_template_loop_product_title', 10 );
This core function seems to grab the first category rather than the primary category assigned by the plugin. Is there a way to change this action (or remove it and create a new one) so that it references the primary category assigned. For reference, the primary category plugin stores the value as a post meta field like this:
I tried using this code below to change the URL of the word "Shop" but it seems to do nothing:
add_filter( 'woocommerce_page_title', 'woo_shop_page_title');
function woo_shop_page_title( $page_title ) {
if( 'Shop' == $page_title) {
return "My new title";
}
}
I don't want users to go to the main "Shop" page and setup many categories.
Unfortunately I have not been able to find a way to remove or change the URL of that "Shop" breadcrumb. The official docs only mention how to change the "Home" URL, not "Shop".
You're help is appreciated.
To rename any breadcrumb item and change its link, use woocommerce_get_breadcrumb filter hook this way:
add_filter( 'woocommerce_get_breadcrumb', 'custom_get_breadcrumb', 20, 2 );
function custom_get_breadcrumb( $crumbs, $breadcrumb ){
if( ! is_shop() ) return $crumbs; // Only shop page
// The Crump item to target
$target = __( 'Shop', 'woocommerce' );
foreach($crumbs as $key => $crumb){
if( $target === $crumb[0] ){
// 1. Change name
$crumbs[$key][0] = __( 'Name', 'woocommerce' );
// 2. Change URL (you can also use get_permalink( $id ) with the post Id
$crumbs[$key][1] = home_url( '/my-link/' );
}
}
return $crumbs;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Please check this code in on home replace Start chenag in woocommerce
function woocommerce_breadcrumb( $args = array() ) {
$args = wp_parse_args( $args, apply_filters( 'woocommerce_breadcrumb_defaults', array(
'delimiter' => ' / ',
'wrap_before' => '',
'wrap_after' => '',
'before' => '',
'after' => '',
'home' => _x( 'Home', 'breadcrumb', 'woocommerce' ),
) ) );
$breadcrumbs = new WC_Breadcrumb();
$args['home'] = "Start"; // home replace start set
if ( ! empty( $args['home'] ) ) {
$breadcrumbs->add_crumb( $args['home'], apply_filters( 'woocommerce_breadcrumb_home_url', home_url() ) );
}
$args['breadcrumb'] = $breadcrumbs->generate();
The 'shop' breadcrumb is coming from you 'Shop' pages'. Simply change your shop page slug to whatever you want, or if it is another page altogether then in the woocommerce settings you set the shop page to your new page. The url will always be the page's slug.
EDIT : Even after making the Permalink Settings to Post name, http://domain.com/?paged=3 still works.
I have posts collection in my home page which has pagination. Let's say I have
domain.com
If my Permalink settings is in default which is Plain Format like
http://domain.com/?p=123
Then my pagination http://domain.com/?paged=3 will work.
How ever if I want my permalink settings to be in Post Name Format like
http://domain.com/sample-post/
Then my pagination in my home page will not work anymore. I tried inspect element in the link of pagination if set in post name permalink and it's
http://domain.com/page/23/
Then what happened is it will not go to page 23. It will always redirect to my home page
http://domain.com
I already tried this
https://wordpress.stackexchange.com/questions/134339/pagination-on-custom-post-type-not-working-if-permalinks-set-to-rewrite-url
Which I put this code in my functions.php
add_filter( 'redirect_canonical','custom_disable_redirect_canonical' );
function custom_disable_redirect_canonical( $redirect_url ){
if ( is_singular('your_custom_post_type') ) $redirect_url = false;
return $redirect_url;
}
And it didn't work.
Update I checked the functions.php and i see this
/* -----------------------------------------------------------------------------
Woo commerce
*/
if (in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' )))) { // check if we have woo commerce installed
// breadcrumb
add_filter( 'woocommerce_breadcrumb_defaults', 'td_woocommerce_breadcrumbs' );
function td_woocommerce_breadcrumbs() {
return array(
'delimiter' => ' <span class="td-sp td-sp-breadcrumb-arrow td-bread-sep"></span> ',
'wrap_before' => '<div class="entry-crumbs" itemprop="breadcrumb">',
'wrap_after' => '</div>',
'before' => '',
'after' => '',
'home' => _x( 'Home', 'breadcrumb', 'woocommerce' ),
);
}
// number of products to display on shop page
add_filter('loop_shop_per_page', create_function('$cols', 'return 8;'));
if (!function_exists('woocommerce_pagination')) {
// pagination
function woocommerce_pagination(){
echo td_page_generator::get_pagination();
}
}
if (!function_exists('woocommerce_output_related_products')) {
// number of related product
function woocommerce_output_related_products() {
woocommerce_related_products(4,4); // Display 4 products in rows of 4
}
}
}
/**
* Add prev and next links to a numbered link list - the pagination on single.
*/
function wp_link_pages_args_prevnext_add($args)
{
global $page, $numpages, $more, $pagenow;
if (!$args['next_or_number'] == 'next_and_number')
return $args; # exit early
$args['next_or_number'] = 'number'; # keep numbering for the main part
if (!$more)
return $args; # exit early
if($page-1) # there is a previous page
$args['before'] .= _wp_link_page($page-1)
. $args['link_before']. $args['previouspagelink'] . $args['link_after'] . '</a>'
;
if ($page<$numpages) # there is a next page
$args['after'] = _wp_link_page($page+1)
. $args['link_before'] . $args['nextpagelink'] . $args['link_after'] . '</a>'
. $args['after']
;
return $args;
}
add_filter('wp_link_pages_args', 'wp_link_pages_args_prevnext_add');
add_theme_support('woocommerce');
It doesn't have woo commerce plugin but It has that code. Hope it helps you find out whats going on?
Is is not possible to have a page name and a custom post type with the same name. If you do, the page url-slug of the page name and custom post type slug are the same and will cause a not found error.
If you are using WP_Query for retrieving posts then change your code to below
$paged = ( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;
$args = array(
'posts_per_page' => 5,
'post_type' => 'post',
'paged' => $paged,
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
echo '<br>';
endwhile;
$big = 999999999;
echo paginate_links( array(
'format' => '?paged=%#%',
'current' => max( 1, $paged ),
'total' => $the_query->max_num_pages
) );
Beside other mentioned scenarios, Make sure you do not have the slug of the template you are using the loop on similar to any of the post types exist in the theme. If a page slug is similar to the post type slug, the pagination will not work properly.
I have a custom Wordpress Taxonomy called 'directory-category' with posts sitting in each relevant category within the taxonomy.
I'm using the Avada template with a hope to use their sidebar mechanism, however I'm unable to modify (successfully) this code to not list page ancestors, but rather, listing categories within the taxonomy and then the posts within the categories.
Here is the current sidebar helper function building a sidebar menu based on pages / page ancestors;
$html = '<ul class="side-nav">';
$post_ancestors = get_ancestors( $post_id, 'page' );
$post_parent = end( $post_ancestors );
$html .= '<li';
if( is_page( $post_parent ) ) {
$html .= ' class="current_page_item"';
}
if( $post_parent ) {
$html .= sprintf( '>%s</li>', get_permalink( $post_parent ), __( 'Back to Parent Page', 'Avada' ), get_the_title( $post_parent ) );
} else {
$html .= sprintf( '>%s</li>', get_permalink( $post_id ), __( 'Back to Parent Page', 'Avada' ), get_the_title( $post_id ) );
}
if( $post_parent ) {
$children = wp_list_pages( sprintf( 'title_li=&child_of=%s&echo=0', $post_parent ) );
} else {
$children = wp_list_pages( sprintf( 'title_li=&child_of=%s&echo=0', $post_id ) );
}
if ( $children ) {
$html .= $children;
}
$html .= '</ul>';
return $html;
I really would appreciate the help if someone is able to assist!
For my WC product pages, I need to add a class to the body tag so that I can perform some custom styling. Here's the function I'm creating for this...
function my_add_woo_cat_class($classes) {
$wooCatIdForThisProduct = "?????"; //help!
// add 'class-name' to the $classes array
$classes[] = 'my-woo-cat-id-' . $wooCatIdForThisProduct;
// return the $classes array
return $classes;
}
//If we're showing a WC product page
if (is_product()) {
// Add specific CSS class by filter
add_filter('body_class','my_add_woo_cat_class');
}
...but how do I get the WooCommerce cat ID?
A WC product may belong to none, one or more WC categories. Supposing you just want to get one WC category id.
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
break;
}
Please look into the meta.php file in the "templates/single-product/" folder of the WooCommerce plugin.
<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>
$product->get_categories() is deprecated since version 3.0! Use wc_get_product_category_list instead.
https://docs.woocommerce.com/wc-apidocs/function-wc_get_product_category_list.html
I literally striped out this line of code from content-single-popup.php located in woocommerce folder in my theme directory.
global $product;
echo $product->get_categories( ', ', ' ' . _n( ' ', ' ', $cat_count, 'woocommerce' ) . ' ', ' ' );
Since my theme that I am working on has integrated woocommerce in it, this was my solution.
Thanks Box. I'm using MyStile Theme and I needed to display the product category name in my search result page. I added this function to my child theme functions.php
Hope it helps others.
/* Post Meta */
if (!function_exists( 'woo_post_meta')) {
function woo_post_meta( ) {
global $woo_options;
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat = $term->name;
break;
}
?>
<aside class="post-meta">
<ul>
<li class="post-category">
<?php the_category( ', ', $post->ID) ?>
<?php echo $product_cat; ?>
</li>
<?php the_tags( '<li class="tags">', ', ', '</li>' ); ?>
<?php if ( isset( $woo_options['woo_post_content'] ) && $woo_options['woo_post_content'] == 'excerpt' ) { ?>
<li class="comments"><?php comments_popup_link( __( 'Leave a comment', 'woothemes' ), __( '1 Comment', 'woothemes' ), __( '% Comments', 'woothemes' ) ); ?></li>
<?php } ?>
<?php edit_post_link( __( 'Edit', 'woothemes' ), '<li class="edit">', '</li>' ); ?>
</ul>
</aside>
<?php
}
}
?>
<?php
$terms = get_the_terms($product->ID, 'product_cat');
foreach ($terms as $term) {
$product_cat = $term->name;
echo $product_cat;
break;
}
?>
To add custom classes to the body tag you can use the body_class hook.
To add one or more classes on the product page based on specific product categories you can use the Wordpress has_term function (to check if a product belongs to that specific product category).
For this I created the array $classes_to_add where:
key: It can be the product category id (or the slug or the name). Or an array of them. Read the documentation.
value: a string containing the classes to add to the body tag. If you want to add more than one class create a string with multiple values separated by a space (see my example below)
So:
// adds a class to the body element based on the product category
add_filter( 'body_class', 'add_body_class_based_on_the_product_category' );
function add_body_class_based_on_the_product_category( $classes ) {
// only on the product page
if ( ! is_product() ) {
return $classes;
}
// create an array with the ids (or slugs) of the product categories and the respective class (or classes) to add
$classes_to_add = array(
30 => 'class-1',
'cat_slug' => 'class-2 class-3',
32 => 'class-4 class-5',
);
// if the product belongs to one of the product categories in the array it adds the respective class (or classes)
foreach ( $classes_to_add as $product_cat => $new_classes ) {
if ( has_term( $product_cat, 'product_cat', get_the_ID() ) ) {
$classes[] = $new_classes;
}
}
return $classes;
}
The code has been tested and works. Add it to your active theme's functions.php.