Wordpress - How to specify an ID in custom Visual Composer element - php

I'm current building a website for a non-profit, and am using an older edition of the Wordpress theme, Savior.
There is a custom post type of Donations, and it allows the admin to set up different "Causes", each of which is a post with a specific ID.
By default, the theme has a custom Visual Composer element (Screenshot 1 - https://i.stack.imgur.com/cmhTu.png) that allows you to feature the most recent "Cause"; the only customization option you have is for the Title of the VC element.
I'm trying to update this custom VC element so that the admin can specify the exact ID for the "Cause" they would like to feature on a page/post vs. only showing the latest "Cause."
I've adjusted the VC mapping for the cause.php template where commented below:
<?php
class STM_VC_Causes {
function __construct() {
add_action( 'init', array( $this, 'integrateWithVC' ) );
add_shortcode( 'stm_causes', array( $this, 'render' ) );
}
public function integrateWithVC() {
if ( function_exists( 'vc_map' ) ) {
vc_map( array(
'name' => __( 'Causes', STM_DOMAIN ),
'base' => 'stm_causes',
'category' => __( 'STM', STM_DOMAIN ),
'params' => array(
array(
'type' => 'textfield',
'class' => '',
'heading' => __( 'Title', STM_DOMAIN ),
'param_name' => 'title',
'value' => __( 'Our Causes', STM_DOMAIN )
),
/** ========================================
* Qing Custom 8-6-2018
* Allow admin to select Cause to feature
======================================== **/
array(
'type' => 'textfield',
'class' => '',
'heading' => __( 'Cause ID', STM_DOMAIN ),
'param_name' => 'id',
'value' => __( '', STM_DOMAIN )
)
)
) );
}
}
public function render( $atts, $content = null ) {
/** ========================================
* Qing Custom 8-6-2018
* Display selected Cause ID
======================================== **/
$title = '';
$id = '';
extract( shortcode_atts( array(
'title' => '',
'id' => ''
), $atts ) );
$fea_cause = $atts['id'];
$donations = new WP_Query( array( 'post_type' => 'donation', 'posts_per_page' => 1, 'post__in' => $fea_cause ) );
$output = '';
$output .= '<ul class="donation-grid first clearfix">';
while ( $donations->have_posts() ) {
$donations->the_post();
$target_amount = ( get_post_meta( get_the_ID(), 'donation_target', true ) == '' ) ? 0 : get_post_meta( get_the_ID(), 'donation_target', true );
$raised_amount = ( get_post_meta( get_the_ID(), 'donation_raised', true ) == '' ) ? 0 : get_post_meta( get_the_ID(), 'donation_raised', true );
$currency = ( get_post_meta( get_the_ID(), 'donation_currency', true ) == '' ) ? '$' : get_post_meta( get_the_ID(), 'donation_currency', true );
$donors = ( get_post_meta( get_the_ID(), 'donation_donors', true ) == '' ) ? 0 : get_post_meta( get_the_ID(), 'donation_donors', true );
$target_amount_percent = ( $raised_amount / $target_amount ) * 100;
$output .= '<li id="post-' . get_the_ID() . '" class="' . implode( ' ', get_post_class() ) . '">';
$output .= '<div class="donation-thumbnail">';
$output .= '<a href="' . get_the_permalink() . '">';
if ( has_post_thumbnail() ) {
$output .= get_the_post_thumbnail( get_the_ID(), 'thumb-150x150' );
}
$output .= '</a>';
$output .= '</div>';
$output .= '<div class="donation-content">';
$output .= '<h4>' . get_the_title() . '</h4>';
$output .= '<div class="progress_bar"><span style="width: ' . $target_amount_percent . '%;"></span></div>';
$output .= '<div class="donation-stat">';
$output .= '<span><i class="fa fa-child"></i> ' . __( 'Raised', STM_DOMAIN ) . '<br/>' . $currency . $raised_amount . '</span>';
$output .= '<span><i class="fa fa-users"></i> ' . __( 'Donors', STM_DOMAIN ) . '<br/>' . $donors . '</span>';
$output .= '<span><i class="fa fa-thumbs-up"></i> ' . __( 'Goal', STM_DOMAIN ) . '<br/>' . $currency . $target_amount . '</span>';
$output .= '</div>';
$output .= '<div class="donate_now">';
$output .= '' . __( 'DONATE NOW', STM_DOMAIN ) . '';
$output .= '</div>';
$output .= '</div>';
$output .= '</li>';
}
$output .= '</ul>';
wp_reset_query();
return $output;
}
}
if( defined( 'WPB_VC_VERSION' ) ){
new STM_VC_Causes();
}
?>
The custom VC element is showing up correctly for me on the admin backend side of the site (Screenshot 2 - https://i.stack.imgur.com/zKCgs.png), but I cannot figure out how to get the admin-inputted ID to show up on the frontend - no matter what, it still shows the most recent "Cause." Screenshot 3 (https://i.stack.imgur.com/13Qw4.png) is simply an example screenshot of what the individual "Cause" looks like when a page with the custom VC element is pushed live.
I've contacted the support team for the theme, but they only suggested this Post Types Order WP plugin, which only allows you to change the displayed "Cause" across the entire site, instead of allowing you to specify it in a page-by-page/post-by-post basis. I've also scoured Google/StackOverflow and tried various queries in the WP Codex, building out a custom shortcode (the custom VC element itself is a custom shortcode: [stm_causes]), but it just displays the most recent "Cause."
Edit 8/7/18:
I've made several edits to the causes.php template within the Savior theme, but for some reason, the updated WP_Query loop ended up not pulling in any data (Screenshot 3: https://i.stack.imgur.com/JLibO.png ).
The only exception is if I omit any ID in the VC editor backend; If I don't enter an ID, it defaults to the most recent Cause. However, if I enter any ID, even if it's the same ID as the most recent post, nothing shows up...
Any idea what could be wrong with my logic?
Thank you!

render function Fixed!
$fea_cause = $atts['id'];
$donations = new WP_Query( array( 'post_type' => 'donation', 'posts_per_page' => 1, 'post__in' => array($fea_cause )));
Huge props to Nilesh Sanura for his help!

Related

Indexed all categories of woocommerce according to alphabet

I need to get all the categories in indexed form according to the alphabet. I'm attaching picture output image of that i need. . is there someone that can help me out?
Advance Thanks
Try this:
/**
* This code should be added to functions.php of your theme
**/
add_filter('init', 'custom_default_catalog_orderby');
function custom_default_catalog_orderby() {
$result = array();
$categories = get_categories( array(
'hide_empty' => 0,
'orderby' => 'name',
'order' => 'ASC'
) );
// Store all category alphabet wise in $result array
foreach( $categories as $cat ) {
$first_char = mb_substr($cat->name, 0, 1);
$result[$first_char][] = $cat;
}
// Print all categories alphabetically
foreach($result as $alphabet => $c ) {
echo "<h3>". ucfirst($alphabet) . "</h3>";
// You can change HTML structure accordingly and manage like echo or return
foreach($c as $category ) {
$category_link = sprintf(
'%3$s',
esc_url( get_category_link( $category->term_id ) ),
esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
esc_html( $category->name )
);
echo '<p>' . sprintf( esc_html__( '%s', 'textdomain' ), $category_link ) . '( '
. sprintf( esc_html__( 'Post Count: %s', 'textdomain' ), $category->count ) . ')</p>';
}
}
}
You can change action and HTML structure accordingly.

If a post belongs to a many categories and I want to show a primary category in Genesis post_meta

On https://broadly.vice.com/en_us they show a single, primary category, if their post belongs to many categories.
How can you choose, on a post by post basis, what primary category to show in the post_meta in a Genesis Child theme?
Here is simple solution.
Use SEO Yoast free plugin https://wordpress.org/plugins/wordpress-seo/ . Which help you make any category primary when there are multiple categories assigned to a post.
See Plugin's interface for Primary cat selection:
Then in your WP theme have following function in functions file and then you can use to echo the category name anywhere as: <?php echo taxo_primary_term_name; ?>
// wp seo yoast get primary category name
function taxo_primary_term_name($taxo){
$wpseo_primary_term = new WPSEO_Primary_Term($taxo, get_the_ID());
$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
return $wpseo_primary_term = get_term($wpseo_primary_term)->name;
}
As of this post date, install the Trunk version of CMB2 (not the plugin).
See See Gist for a backup of this answer.
Put the following inside your functions.php or, better, your functions plugin. Don't add the opening php. Change yourprefix_ in many locations.
<?php
//don't add
/**
*
* Create Metabox and Primary Category Select Field
* Requires CMB2 TRUNK branch NOT the plugin from the repo
* Install CMB2-trunk.zip via plugin interface or incorporate in your theme, read their docs
* https://github.com/WebDevStudios/CMB2/branches
*
*/
function yourprefix_primary_category_selection() {
$prefix = 'yourprefix_';
$cmb_demo = new_cmb2_box( array(
'id' => $prefix . 'metabox',
'title' => esc_html__( 'Primary Category', 'yourtextdomain' ),
'object_types' => array( 'post', ),
'context' => 'side',
'priority' => 'low',
'show_names' => false,
) );
$cmb_demo->add_field( array(
'name' => esc_html__( 'Choose Primary Category', 'yourtextdomain' ),
'desc' => esc_html__( 'Choose primary category for display in post_meta', 'yourtextdomain' ),
'id' => $prefix . 'category_list',
'taxonomy' => 'category',
'type' => 'taxonomy_select',
) );
}
add_action( 'cmb2_admin_init', 'yourprefix_primary_category_selection' );
/**
*
* Add Primary to [post_categories] shortcode replacing the genesis shortcode of the same name
*
*/
function yourprefix_post_primary_category_shortcode( $atts ) {
//* get our CMB2 field and category stuff
$prefix = 'yourprefix_';
$primary_cat = get_post_meta( get_the_ID(), $prefix . 'category_list', true );
$category_id = get_cat_ID( $primary_cat );
$category_link = get_category_link( $category_id );
$category_name = get_cat_name( $category_id );
$defaults = array(
'sep' => ', ',
'before' => __( 'Filed Under: ', 'yourtextdomain' ),
'after' => '',
);
$atts = shortcode_atts( $defaults, $atts, 'post_categories' );
//* fallback to the standard array if the choice in the primary metabox is not set
if( empty( $primary_cat ) ) {
$cats = get_the_category_list( trim( $atts['sep'] ) . ' ' );
} else {
$cats = '' . $category_name . '';
}
//* Do nothing if no cats
if ( ! $cats ) {
return '';
}
if ( genesis_html5() )
$output = sprintf( '<span %s>', genesis_attr( 'entry-categories' ) ) . $atts['before'] . $cats . $atts['after'] . '</span>';
else
$output = '<span class="categories">' . $atts['before'] . $cats . $atts['after'] . '</span>';
return apply_filters( 'genesis_post_categories_shortcode', $output, $atts );
}
add_shortcode( 'post_categories', 'yourprefix_post_primary_category_shortcode' );

How to output categories with custom markup and hierarchy?

On this SO question I have found the following
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'number' => 20 // how many categories
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></li>›';
}
That's a step but that it isn't creating a hierarchy. I have read get categories in wp codex but I couldn't work it out.
My structure is:
CAT 1
cat 1.1
cat 1.2
CAT 2
cat 2.1
cat 2.2
Tried this but the output is simply a list of items with no hierarchy at all:
public function walk_taxonomy( $type = "checkbox", $args = array() ) {
$args['walker'] = new Search_Filter_Taxonomy_Walker($type, $args['name']);
$output = $argo = array(
'hierarchical' => 1
);
$categories = get_categories($argo);
foreach($categories as $category) {
echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></li>›';
}
if ( $output )
return $output;
}
you have to add this to the args:
'hierarchical' => 1,
in your foreach you have to check if $category->parent is empty or not.. if not empty indent by adding spaces or applying a class to your li like
<li class="indent">

WordPress, A page of thumbnails and titles for a grid of taxonomies for a custom post type

I am able to list the taxonomy children:
<?php
$taxonomy = 'store_name';
$tax_terms = get_terms($taxonomy);
?>
<ul class="split-list2">
<?php
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
</ul> ?>
And I found a plugin to make thumbnails for categories/taxonomies:
https://wordpress.org/plugins/taxonomy-images/
But I can't figure out how to add the code to include the thumbnails to the taxonomy category grids.
Does anyone know how to do this
Read the plugin doc perfactly it gives what you want
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'after' => '</div>',
'after_image' => '</span>',
'before' => '<div class="my-custom-class-name">',
'before_image' => '<span>',
'image_size' => 'detail',
'post_id' => 1234,
'taxonomy' => 'post_tag',
) );
Second parameter in apply_filters( 'taxonomy-images-get-terms', '' ); is array as show above
$terms = apply_filters( 'taxonomy-images-get-terms', '' );
if ( ! empty( $terms ) ) {
print '<ul>';
foreach( (array) $terms as $term ) {
print '<li><a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'detail' ) . '</li>';
}
print '</ul>';
}

Adding conditional div class to Genesis shortcode entry-header

I am desperately trying to find a solution to add a conditional div class to individual categories when using the Genesis shortcode [post_categories] in the entry-header.
I want to apply unique colors to category titles in the entry header for articles on the home page and on individual posts. www.mic.com has this affect. You will notice that articles under the "World" category have the category text one color, while articles under the "News" category have the category text displayed as a different color.
For example, if the category is "Infrastructure", I'd like there to be a class that wraps around the existing entry-categories class.
I want it to look like this:
<div class="infrastructure section"><span class="entry-categories"></span></div>
If the category is "Elections", I'd like it to show:
<div class="elections section"><span class="entry-categories"></span></div>
The code I need to edit to get this effect is listed below.
add_shortcode( 'post_categories', 'genesis_post_categories_shortcode' );
/**
* Produces the category links list.
*
* Supported shortcode attributes are:
* after (output after link, default is empty string),
* before (output before link, default is 'Tagged With: '),
* sep (separator string between tags, default is ', ').
*
* Output passes through 'genesis_post_categories_shortcode' filter before returning.
*
* #since 1.1.0
*
* #param array|string $atts Shortcode attributes. Empty string if no attributes.
* #return string Shortcode output
*/
function genesis_post_categories_shortcode( $atts ) {
$defaults = array(
'sep' => ', ',
'before' => __( 'Filed Under: ', 'genesis' ),
'after' => '',
);
$atts = shortcode_atts( $defaults, $atts, 'post_categories' );
$cats = get_the_category_list( trim( $atts['sep'] ) . ' ' );
if ( genesis_html5() )
$output = sprintf( '<span %s>', genesis_attr( 'entry-categories' ) ) . $atts['before'] . $cats . $atts['after'] . '</span>';
else
$output = '<span class="categories">' . $atts['before'] . $cats . $atts['after'] . '</span>';
return apply_filters( 'genesis_post_categories_shortcode', $output, $atts );
}
I have tried using the get_the_category_list function to conditionally create the class names, but the command is not completing an action. When viewing my site through firebug, the new class shows the command as text.
$cats = get_the_category_list( trim( $atts['sep'] ) . ' ' );
if ( genesis_html5() )
$output = sprintf( '<div class="<?php echo $cats[0]->cat_name; ?> section"><span %s>', genesis_attr( 'entry-categories' ) ) . $atts['before'] . $cats . $atts['after'] . '</span></div>';
Any ideas on how to achieve this affect?
Thanks so much!
Something like this should work ( but only for html5 ):
function custom_terms_shortcode( $atts ) {
$defaults = array(
'after' => '',
'before' => __( 'Filed Under: ', 'genesis' ),
'sep' => ', ',
'taxonomy' => 'category',
);
$atts = shortcode_atts( $defaults, $atts, 'custom_terms' );
$terms = get_the_terms( get_the_ID(), $atts['taxonomy'] );
if ( is_wp_error( $terms ) )
return;
if ( empty( $terms ) )
return;
if ( genesis_html5() ){
$output .= '<span class="entry-terms">';
$output .= $atts['before'];
foreach ($terms as $term) {
$output = '<div class="'. $term->name .'">';
$output .= ''.$term->name.''.$atts['sep'];
$output .= '</div>';
}
$output = substr($output, 0, -2);
$output .= $atts['after'];
$output .= '</span>';
}
return apply_filters( 'genesis_post_terms_shortcode', $output, $terms, $atts );
}

Categories