Here's a shortcode of mine:
function sc_link( $atts ) {
extract( shortcode_atts(
array(
'page' => '',
'style' => 'button',
'window' => 'self',
'label' => 'Missing Label Tag: label=""',
), $atts )
);
return '' . $label . '';
}
add_shortcode( 'mylink', 'sc_link' );
What I want to be able to do is a conditional before the return: if $window = 'new' then echo 'blank'.
Think this is what your trying to do what id suggest doing is leaving window blank and just doing an if statement on that
How you want it
function sc_link( $atts ) {
extract( shortcode_atts(
array(
'page' => '',
'style' => 'button',
'window' => 'self',
'label' => 'Missing Label Tag: label=""',
), $atts )
);
if($window == "new"){
$link = '' . $label . '';
}else{
$link = '' . $label . '';
}
return $link;
}
add_shortcode( 'mylink', 'sc_link' );
How it should be
function sc_link( $atts ) {
extract( shortcode_atts(
array(
'page' => '',
'style' => 'button',
'window' => '',
'label' => 'Missing Label Tag: label=""',
), $atts )
);
if(!$window){
$link = '' . $label . '';
}else{
$link = '' . $label . '';
}
return $link;
}
add_shortcode( 'mylink', 'sc_link' );
The your shortcode will be link
[shorcode page="" window="" label=""]
Here is an easy short code conditional output example. I would recommend being sparing with this type of syntax because it can get lost in the fray.
echo ($window === 'new') ? $window : '';
This format is conditional ? what to do if true : what to do if false
Related
I am not sure why my custom FacetWP facet will not render on the front end of WordPress
I need a custom FacetWP dropdown that shows the user options such as: today, tomorrow, December by taking in two acf fields of
event_date_start and event_date_end and filtering the posts based on this range.
I have attempted to set up the facet as follows:
First, create a custom facet type in FacetWP by adding this code to functions.php file:
add_filter( 'facetwp_facet_types', function( $types ) {
$types['custom_dropdown'] = array(
'label' => 'Custom Dropdown',
'query_type' => 'custom',
);
return $types;
});
Next, the custom query code in functions.php file:
add_filter( 'facetwp_query_args', function( $query_args, $renderer ) {
$params = $renderer->$params;
if ( 'custom_dropdown' == $params['facet']['type'] ) {
$selected = $params['selected_values'][0];
switch ( $selected ) {
case 'today':
$selected_date = strtotime( 'today' );
break;
case 'tomorrow':
$selected_date = strtotime( 'tomorrow' );
break;
case 'December':
$selected_date = strtotime( 'December' );
break;
}
$query_args['meta_query'][] = array(
'relation' => 'AND',
array(
'key' => 'event_date_start',
'value' => $selected_date,
'compare' => '<=',
'type' => 'NUMERIC',
),
array(
'key' => 'event_date_end',
'value' => $selected_date,
'compare' => '>=',
'type' => 'NUMERIC',
),
);
}
return $query_args;
}, 10, 2 );
Add the custom dropdown options to the facet settings in functions.php file:
add_filter( 'facetwp_render_output', function( $output, $params ) {
if ( 'custom_dropdown' == $params['facet']['type'] ) {
$output = '<select>';
$options = array( 'today', 'tomorrow', 'December' );
foreach ( $options as $option ) {
$selected = ( $option == $params['selected_values'][0] ) ? ' selected' : '';
$output .= '<option value="' . $option . '"' . $selected . '>' . $option . '</option>';
}
$output .= '</select>';
}
return $output;
}, 10, 2 );
I have placed this shortcode on the front end:
[facetwp facet="custom_dropdown"]
I have also tried to set the Date Source to be 'custom' but this does not work either:
add_filter( 'facetwp_facet_sources', function( $sources ) {
$sources['custom'] = array(
'label' => 'Custom Values',
'choices' => array(
'today' => 'Today',
'tomorrow' => 'Tomorrow',
'December' => 'December'
),
'props' => array(
'data_type' => 'custom'
)
);
return $sources;
});
If someone knows what the issue could be I would really appreciate if someone can help.
So here is shortcode that i can use to output names based on attributes.
For example, [my_site dfd_name="boy"] will output all boys name.
However if I put "girl" in the name attribute, it does not recognize it. ([my_site dfd_name="boy,girl"]).
Here is the full php:
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
function sp_shortcode_listings($atts, $content = null, $code = "") {
global $wp_query;
global $title;
global $active_tab;
global $showposts;
global $sp_taxonomy;
$defaults = array(
'type' => 'grid',
'before' => '',
'after' => '',
'wrap' => 'div',
'title' => '',
'showposts' => '12',
'type'=> 'grid',
);
extract( shortcode_atts( $defaults, $atts ) );
if ($showposts == 12) {
$showposts = isset($_GET['showposts']) ? $_GET['showposts'] : '12';
}
$shorcode = "sc";
if(!empty($atts))
{
foreach ( $atts as $key => $val)
{
if(strstr($key, 'dfd'))
{
$metaquery[] = array(
'key' => $key,
'value' => $val,
'compare' => 'LIKE',
'terms' => explode(',',$val)
);
}
}
}
if(empty($metaquery))
{
$shorcode = "";
}
global $active_tab;
$active_tab = isset( $_GET[ 'type' ] ) ? $_GET[ 'type' ] : $type;
include(my_site_dir . '/archive-filter.php');
global $post;
ob_start();
if ( have_posts() )
{
my_template_property_archive_content_main_shortcode();
}
else
{
echo apply_filters( 'my_site_no_match', 'Sorry, no name matched your criteria' ) ;
}
$output_string = ob_get_contents();
ob_end_clean();
$output = sprintf( '<%4$s class="sp">%1$s%3$s%2$s</%4$s>', $before, $after, $output_string, $wrap );
wp_reset_query();
return $output;
}
add_shortcode("my-listings", "sp_shortcode_listings");
?>
How can I change it so that I can add multiple values in the "dfd" attributes?
Thanks
Hello I'm working on a wordpress site and I currently have a popup set to display if an arrtibute in the shortcode is set to "popup="register")... I added another lightbox that I want to display if the attribute is set to "popup="apply")
My question is how to add another variable to the following function.. would I need an if statement? Im new to php, any advice is greatly appreciated.. Thanks!!!!
function oxy_shortcode_button_fancy($atts , $content = '' ) {
// setup options
extract( shortcode_atts( array(
'button_swatch' => 'swatch-coral',
'button_animation' => '',
'size' => 'default',
'xclass' => '',
'link' => '',
'label' => 'My button',
'icon' => '',
'link_open' => '_self',
'popup' => ''
), $atts ) );
$popup = ('register' == $popup) ? ' onclick="jQuery(\'#registerid, .overlayLogin\').show();"' : '';
$animation = ( $button_animation != "") ? ' data-animation="'.$button_animation.'"' :"";
return '<a'.$popup.' href="'. $link .'" class="btn '. $size.' btn-icon-right '. $xclass . ' '. $button_swatch .'" target="' . $link_open . '"> '. $label . '<span><i class="'.$icon.'" '.$animation.'></i></span></a>';
}
What i'm looking to add is:
$popup = ('apply' == $popup) ? ' onclick="jQuery(\'#applyid, .overlayLogin\').show();"' : '';
Try using this codes..Hope it helps you
function oxy_shortcode_button_fancy($atts , $content = '' ) {
// setup options
extract( shortcode_atts( array(
'button_swatch' => 'swatch-coral',
'button_animation' => '',
'size' => 'default',
'xclass' => '',
'link' => '',
'label' => 'My button',
'icon' => '',
'link_open' => '_self',
'popup' => ''
), $atts ) );
if('register' == $popup) {
$popup = ('register' == $popup) ? ' onclick="jQuery(\'#registerid, .overlayLogin\').show();"' : '';
} else {
$popup = ('apply' == $popup) ? ' onclick="jQuery(\'#applyid, .overlayLogin\').show();"' : '';
}
$animation = ( $button_animation != "") ? ' data-animation="'.$button_animation.'"' :"";
return '<a'.$popup.' href="'. $link .'" class="btn '. $size.' btn-icon-right '. $xclass . ' '. $button_swatch .'" target="' . $link_open . '"> '. $label . '<span><i class="'.$icon.'" '.$animation.'></i></span></a>';
}
i use link shortcode on my template functions.php like this,
function post_link_shortcode( $atts ) {
// Attributes
extract( shortcode_atts(
array(
'id' => '',
'kelime' => '',
), $atts )
);
// Code
if ( isset( $id ) ) {
return '<a target="_blank" rel="nofollow" href="' . $id . '"> <strong>' .$kelime . '</strong></a>';
}
}
add_shortcode( 'link', 'post_link_shortcode' );
Usage :
[link id="someurl.com" kelime="link title"]
could we change this like;
[link|someurl.com|link title]
is that possible ? than how ? thanks.
I've been reading a lot of how to customize excerpt function in WordPress but I have no idea how to proceed with this.
The theme that I am using already have 4 pre-customized excerpt functions and the one that I will show here is closest to my desired but still needs to improve.
My question is how to stop erasing HTML formating from my content (line breaks, paragraphs, font variants, etc)?
add_shortcode('display_news_s5', 'be_display_posts_shortcode5');
function be_display_posts_shortcode5($atts) {
// Pull in shortcode attributes and set defaults
extract( shortcode_atts( array(
'post_type' => 'post',
'post_parent' => false,
'id' => false,
'tag' => '',
'category' => '',
'offset' => 0,
'posts_per_page' => '1',
'order' => 'DESC',
'orderby' => 'date',
'include_date' => false,
'include_excerpt' => false,
'excerpt_l' => 8,
'taxonomy' => false,
'tax_term' => true,
'tax_operator' => 'IN'
), $atts ) );
// Set up initial query for post
$args = array(
'post_type' => explode( ',', $post_type ),
'tag' => $tag,
'category_name' => $category,
'p' => $id,
'posts_per_page' => $posts_per_page,
'order' => $order,
'orderby' => $orderby,
'offset' => $offset
);
// If Post IDs
if( $id ) {
$posts_in = explode( ',', $id );
$args['post__in'] = $posts_in;
}
// If taxonomy attributes, create a taxonomy query
if ( !empty( $taxonomy ) && !empty( $tax_term ) ) {
// Term string to array
$tax_term = explode( ', ', $tax_term );
// Validate operator
if( !in_array( $tax_operator, array( 'IN', 'NOT IN', 'AND' ) ) )
$tax_operator = 'IN';
$tax_args = array(
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $tax_term,
'operator' => $tax_operator
)
)
);
$args = array_merge( $args, $tax_args );
}
// If post parent attribute, set up parent
if( $post_parent ) {
if( 'current' == $post_parent ) {
global $post;
$post_parent = $post->ID;
}
$args['post_parent'] = $post_parent;
}
$listing = new WP_Query( apply_filters( 'display_posts_shortcode_args', $args, $atts ) );
$count = 0;
if ( !$listing->have_posts() )
return apply_filters ('display_posts_shortcode_no_results', false );
$inner = '';
while ( $listing->have_posts() ): $listing->the_post(); global $post;
$count++;
if( $count == 1 ){
$style = ' news-main-post';
} else {
$style = ' news-list-posts';
}
$title = '<div class="news-listing-title"><a class="title" href="'. get_permalink() .'">'. get_the_title() .'</a></div>';
if ($include_date == 'true') $date = ' <div class="news-listing-meta"><span class="news-listing-date">'. get_the_date() . '</span><span class="news-listing-comment">('. get_comments_number() .')</span></div>';
else $date = '';
if ($include_excerpt == 'true') $excerpt = '<span>' .excerpt($excerpt_l) . '</span>';
else $excerpt = '';
$output = '<div class="news-listing' . $style . '"><div class="news-listing-item">'. $title . $excerpt . $date . '</div></div>';
$inner .= apply_filters( 'display_posts_shortcode_output', $output, $atts, $title, $excerpt, $date );
endwhile; wp_reset_query();
$open = apply_filters( 'display_posts_shortcode_wrapper_open', '<div class="news-listing-wrapper-s3">' );
$close = apply_filters( 'display_posts_shortcode_wrapper_close', '<div class="clear"></div></div>' );
$return = $open . $inner . $close;
return $return;
}
Have a look here: LINK looks like its doing what you want to acchieve.