Wordpress Shortcode Function is displaying before content - wordpress

I have been working on trying to get this shortcode to show correctly but everything I have tried does not work. Below is my function:
function monster_shortcode( $atts ) {
$monster = $atts['name'];
$query = new WP_Query( array(
'post_type' => 'monsters',
'name' => $monster,
));
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
$monster_title = the_title();
$monster_size = the_field('size');
$monster_type = 'type';
$monster_alignment = the_field( 'alignment' );
$monster_ac = the_field( 'armor_class' );
$monster_ac_type = the_field( 'ac_type' );
$monster_hp = the_field( 'hit_points' );
$monster_hd = the_field( 'hit_die' );
$monster_speed = the_field( 'speed' );
$monster_str = the_field( 'str' );
$monster_strb = monster_stats( get_field( 'str' ) );
$monster_dex = the_field( 'dex' );
$monster_dexb = monster_stats( get_field( 'dex' ) );
$monster_con = the_field( 'con' );
$monster_conb = monster_stats( get_field( 'con' ) );
$monster_int = the_field( 'int' );
$monster_intb = monster_stats( get_field( 'int' ) );
$monster_wis = the_field( 'wis' );
$monster_wisb = monster_stats( get_field( 'wis' ) );
$monster_cha = the_field( 'cha' );
$monster_chab = monster_stats( get_field( 'cha' ) );
$monster_saves = the_field( 'saving_throws' );
$monster_skills = the_field( 'skills' );
$monster_dmg = the_field( 'damage_adjustments' );
$monster_senses = the_field( 'senses' );
$monster_lang = the_field( 'languages' );
$monster_cr = the_field( 'cr' );
$monster_actions = the_field( 'actions' );
$monster_reactions = the_field( 'reactions' );
$monster_char = the_field( 'characteristics' );
$monster_content = the_content();
$monster_enviro = the_field( 'enviroments' );
$return = '<h1 class="entry-title">' . $monster_title . '</h1>';
$return .= '<div class="monster-meta">' . $monster_size . ' ' . $monster_type . ', ' . $monster_alignment . '</div>';
$return .= '<ul class="monster-stat">';
$return .= '<li><label>Armor Class</label> ' . $monster_ac . ' (' . $monster_ac_type . ')</li>';
$return .= '<li><label>Hit Points</label> ' . $monster_hp . ' (' . $monster_hd . ')</li>';
$return .= '<li><label>Speed</label> ' . $monster_speed . '</li>';
$return .= '</ul>';
$return .= '<ul class="monster-stat abilities">';
$return .= '<li><label>STR</label>' . $monster_str . ' (' . $monster_strb . ')</li>';
$return .= '<li><label>DEX</label>' . $monster_dex . ' (' . $monster_dexb . ')</li>';
$return .= '<li><label>CON</label>' . $monster_con . ' (' . $monster_conb . ')</li>';
$return .= '<li><label>INT</label>' . $monster_int . ' (' . $monster_intb . ')</li>';
$return .= '<li><label>WIS</label>' . $monster_wis . ' (' . $monster_wisb . ')</li>';
$return .= '<li><label>CHA</label>' . $monster_cha . ' (' . $monster_chab . ')</li>';
$return .= ' </ul>';
$return .= '<ul class="monster-stat">';
$return .= '<li><label>Saving Throws</label> ' . $monster_saves . '</li>';
$return .= '<li><label>Skills</label> ' . $monster_skills . '</li>';
$return .= '<li><label>Damage Adjustments</label> ' . $monster_dmg . '</li>';
$return .= '<li><label>Senses</label> ' . $monster_senses . '</li>';
$return .= '<li><label>Langauage</label> ' . $monster_lang . '</li>';
$return .= '<li><label>Challenge Rating</label> ' . $monster_cr . '</li>';
$return .= '</ul>';
$return .= '<p>' . $monster_traits . '</p>';
$return .= '<h4 class="monster-label">Actions</h2><p>' . $monster_actions . '</p>';
$return .= '<h4 class="monster-label">Reactions</h2><p>' . $monster_reactions . '</p>';
$return .= '<h4 class="monster-label">Characteristics</h2><p>' . $monster_char . '</p>';
$return .= '<h4 class="monster-label">Details</h2><p>' . $monster_content . '</p>';
$return .= '<p><label>Enviroments:</label> ' . $monster_enviro . '</p>';
endwhile;
}
return $return;
wp_reset_postdata();
}
add_shortcode( 'monster', 'monster_shortcode' );
I then put the shortcode [monster name="men-at-arms"] on a page. All the function $variables show up before the post content and all the function html shows where it should be. You can see the output here https://www.dropbox.com/s/xvuqofya1jylsfl/Screenshot%20%283%29.png?dl=0

You're using the display functions instead of the return functions.
the_title() will literally echo the title. If you want it as a variable, you need to use get_the_title() (or you can set the third argument to false in the_title() - but that's generally not desirable)
This applies to all the functions you're using that are outputting a value.
the_title() => get_the_title()
the_field() => get_field()
the_content() => get_the_content()
Since it's now pulling in the hosting post's content, you need to pass the ID from the shortcode to the functions. Either like so:
$monster = $atts['name'];
$monster_id = $atts['id'];
...
$monster_title = get_the_title( $monster_id );
$monster_size = get_field( 'size', $monster_id );
Or like so:
$monster = $atts['name'];
...
$monster_title = get_the_title( $post->ID );
$monster_size = get_field( 'size', $post->ID );
Alternatively, you can remove ALL the variable definitions and just modify the HTML returning portion.
while ( $query->have_posts() ) : $query->the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="monster-meta"><?php the_field('size'); ?> <?php the_field('type'); ?> <?php the_field('alignment'); ?>
<ul class="monster-stat">
...
</div>
<?php endwhile; ?>

Related

Show star ratings in comments

I followed the steps in this article: cssigniter.com/add-rating-wordpress-comment-system to add a star rating to the comments system... but when I list the comments with the code below the stars are not showing up I have tried what seems like a million things and I can not seem to figure out why the stars are not showing up.
Here is the code I am using to pull the comments
if ( is_user_logged_in() ) {
$user_id = get_current_user_id(); $args = array( 'status' => 'approve', 'user_id' => $user_id );
$comments = get_comments($args);
foreach($comments as $comment) : echo '<p>';
$post_id = $comment->comment_post_ID;
$member_name = get_post( $comment->comment_post_ID );
echo ( ' <div style="color: #00205a;"> ' . mysql2date(get_option('date_format'), $comment->comment_date) . ' - </div>' . '<a style="color:#a27747;" href="' . get_permalink( $comment->comment_post_ID ) . '">' . $member_name->post_title . '</a><br />' . '(stars go here)' . '<br />' . $comment->comment_content ). '<br /><br />';
echo '</p>';
endforeach;
}
if ( is_user_logged_in() ) {
$user_id = get_current_user_id(); $args = array( 'status' => 'approve', 'user_id' => $user_id );
$comments = get_comments($args);
foreach($comments as $comment) : echo '<p>';
$member_name = get_post( $comment->comment_post_ID );
if ( $rating = get_comment_meta( $comment->comment_ID, 'rating', true ) ) {
$stars = '<p class="stars">';
for ( $i = 1; $i <= $rating; $i++ ) {
$stars .= '<span class="dashicons dashicons-star-filled"></span>';
}
$stars .= '</p>';
}
echo ( ' <div style="color: #00205a;"> ' . mysql2date(get_option('date_format'), $comment->comment_date) . ' - </div>' . '<a style="color:#a27747;" href="' . get_permalink( $comment->comment_post_ID ) . '">' . $member_name->post_title . '</a><br />'. $stars . '<br />' . $comment->comment_content ). '<br /><br />';
echo '</p>';
endforeach;
}

meta_key and meta_value put it in an array WP_User_Query in Wordpress

I want to display some information about the user using function new WP_User_Query
"umeta_id": "606",
"user_id": "18",
"meta_key": "facebook_login_data",
"meta_value": "a:24:{s:10:\"identifier\";s:16:\"XXXXX\";s:10:\"webSiteURL\";s:0:\"\";s:10:\"profileURL\";s:61:\"https://www.facebook.com/app_scoped_user_id/XXXXXX/\";s:8:\"photoURL\";s:72:\"https://graph.facebook.com/XXXXXX/picture?width=150&height=150\";s:11:\"displayName\";s:14:\"Osadchiy Yuriy\";s:11:\"description\";s:0:\"\";s:9:\"firstName\";s:5:\"Yuriy\";s:8:\"lastName\";s:8:\"Osadchiy\";s:6:\"gender\";s:4:\"male\";s:8:\"language\";s:5:\"ru_RU\";s:3:\"age\";N;s:8:\"birthDay\";i:25;s:10:\"birthMonth\";i:11;s:9:\"birthYear\";i:1988;s:5:\"email\";s:15:\"XXXX.com\";s:13:\"emailVerified\";s:15:\"XXXX.com\";s:5:\"phone\";N;s:7:\"address\";N;s:7:\"country\";N;s:6:\"region\";s:0:\"\";s:4:\"city\";N;s:3:\"zip\";N;s:9:\"job_title\";N;s:17:\"organization_name\";N;}"
How can I get the avatar of the user photoURL
I use this code:
$args = array(
'role' => 'Customer'
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo '<p>' . $user->ID . '</p>';
echo '<p>' . $user->display_name . '</p>';
echo '<p>' . $user->user_email . '</p>';
echo '<p>' . $user->billing_phone . '</p>';
echo '<p>' . $user->billing_first_name . '</p>';
echo '<p>' . $user->billing_last_name . '</p>';
echo '<p>' . $user->facebook_login_data
}
}
I would be grateful for the help.
My decision:
$args = array(
'role' => 'Customer'
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo '<p>' . $user->ID . '</p>';
echo '<p>' . $user->display_name . '</p>';
echo '<p>' . $user->user_email . '</p>';
echo '<p>' . $user->billing_phone . '</p>';
echo '<p>' . $user->billing_first_name . '</p>';
echo '<p>' . $user->billing_last_name . '</p>';
if ($user->facebook_login_data){
echo '<p>' . $user->facebook_login_data["profileURL"] .'</p>';
}
}
}

How to call woocommerce product title in html?

I'm trying to overylay woocommerce product title on its featured image, as i replaced featured image with some plane colored background and on it i need to call woocommerce product title. Please share how can i achieve this ?
if ( has_post_thumbnail() ) {
$html = '<div data-thumb="' . get_the_post_thumbnail_url( $post->ID, 'shop_thumbnail' ) . '" class="woocommerce-product-gallery__image"><a href="' . esc_url( $full_size_image[0] ) . '">';
$html .= get_the_post_thumbnail( $post->ID, 'shop_single', $attributes );
$html .= '</a></div>';
} else {
$html = '<div class="woocommerce-product-gallery__image--placeholder">';
$html .= sprintf( '<img src="%s" alt="%s" class="wp-post-image" />', esc_url( wc_placeholder_img_src() ), esc_html__( 'Awaiting product image', 'woocommerce' ) );
$html .= '<div class="centered">';
$html .= how to call here!!!
$html .= '</div>';
$html .= '</div>';
}
You need to get the product title from the product like this:
$product = wc_get_product( $post->ID );
$title = get_the_title($product->ID);
So your code becomes :
if ( has_post_thumbnail() ) {
$html = '<div data-thumb="' . get_the_post_thumbnail_url( $post->ID, 'shop_thumbnail' ) . '" class="woocommerce-product-gallery__image"><a href="' . esc_url( $full_size_image[0] ) . '">';
$html .= get_the_post_thumbnail( $post->ID, 'shop_single', $attributes );
$html .= '</a></div>';
} else {
$product = wc_get_product( $post->ID );
$title = get_the_title($product->ID);
$html = '<div class="woocommerce-product-gallery__image--placeholder">';
$html .= sprintf( '<img src="%s" alt="%s" class="wp-post-image" />', esc_url( wc_placeholder_img_src() ), esc_html__( 'Awaiting product image', 'woocommerce' ) );
$html .= '<div class="centered">';
$html .= $title ;
$html .= '</div>';
$html .= '</div>';
}
An edit :
Since you are within the loop, you do not need to get the product using the function I used. You can just use the global $product variable if the post is a product.

Bootstrap modal implementation in wordpress codex

I'm having a little hard time here implementing a modal view to display the post content rather than sending the user to another page.
I have this
function get_grid_archive_theme( $post, $archive_template = null ) {
$archive_template = isset( $archive_template ) ? $archive_template : get_product_listing_template();
$return = '';
if ( $archive_template == 'grid' ) {
$product_id = $post->ID;
$excerpt = get_the_excerpt( $product_id );
$post_content = get_the_content( $product_id );
$image_id = get_post_thumbnail_id( $product_id );
$thumbnail_product = wp_get_attachment_image_src( $image_id, 'classic-grid-listing' );
$product_name = get_product_name();
if ( $thumbnail_product ) {
$img_class[ 'alt' ] = $product_name;
$img_class[ 'class' ] = 'classic-grid-image';
$image = wp_get_attachment_image( $image_id, 'classic-grid-listing', false, $img_class );
} else {
$url = default_product_thumbnail_url();
$image = '<img src="' . $url . '" class="classic-grid-image default-image" alt="' . $product_name . '" >';
}
$archive_price = apply_filters( 'archive_price_filter', '', $post );
$classic_grid_settings = get_classic_grid_settings();
$row_class = get_row_class( $classic_grid_settings );
$return = '<div class="col-xs-12 col-sm-6 col-md-3 product-' . $product_id . ' classic-grid ' . $row_class . ' ">';
$return .= '<a data-toggle="modal" data-target="#' . $product_id . ' " href="#">';
//$return .= '<div style="background-image:url(\'' . $url . '\');" class="classic-grid-element"></div>';
$return .= '<div class="classic-grid-image-wrapper"><div class="pseudo"></div><div class="image">' . $image . '</div></div>';
$return .= '<div class="excerpt-cnt"><div class="excerpt-text">' . $excerpt . '</div></div><h3 class="product-name">' . $product_name . '</h3>' . $archive_price;
if ( $classic_grid_settings[ 'attributes' ] == 1 && function_exists( 'product_attributes_number' ) ) {
$attributes_number = product_attributes_number();
if ( $attributes_number > 0 && has_product_any_attributes( $product_id ) ) {
$max_listing_attributes = apply_filters( 'max_product_listing_attributes', $classic_grid_settings[ 'attributes_num' ] );
$return .= '<div class="product-attributes">';
$a = 0;
for ( $i = 1; $i <= $attributes_number; $i++ ) {
$attribute_value = get_attribute_value( $i, $product_id );
if ( !empty( $attribute_value ) ) {
$return .= '<div><span class="attribute-label-listing">' . get_attribute_label( $i, $product_id ) . ':</span> <span class="attribute-value-listing">' . get_attribute_value( $i, $product_id ) . '</span> <span class="attribute-unit-listing">' . get_attribute_unit( $i, $product_id ) . '</span></div>';
$a++;
}
if ( $a == $max_listing_attributes ) {
break;
}
}
$return .= '</div>';
}
}
$return .= '</a>';
$return .= apply_filters( 'classic_grid_product_listing_element', '', $product_id );
$return .= '</div>';
}
$return .= '<div id="' . $product_id . ' " class="modal" role="dialog">';
$return .= '<div class="modal-dialog"><div class="modal-content">';
$return .= '<div class="modal-header">';
$return .= '<h4 class="modal-title">' . $product_name .'</h4>';
$return .= '</div>';
$return .= '<div class="modal-body">';
$return .= '<p>' . $post_content .'</p>';
$return .= '</div>';
$return .= '<div class="modal-footer">';
$return .= '</div>';
$return .= '</div></div>';
$return .= '</div>';
return $return;
}
This code extracts a series of posts excerpts and displays them in a 4 column layout As the picture below depicts
Each one can be clicked and it will show the content of that post in a modal. Well the thing is, it just doesn't but the modal is loaded in the DOM because when I inspect the code there are no JS errors, and the modals are shown in the HTML but still as display:none;. If I click that off or change it to block manually in the web-dev tool the modal shows.
Bootstrap is loaded in the site too. What am I missing? Why doesn't the data-target toggle the display from none to block?
I think there is just a small space problem in your code, in the line below.
$return .= '<div id="' . $product_id . ' " class="modal" role="dialog">';
Remove the space before the close of id attribute, as given below.
$return .= '<div id="' . $product_id . '" class="modal" role="dialog">';
I hope this will help!

Translatable output in shortcode

Hello I would like to make part of the output translatable in my shortcode but I don't know how to do it.
I tried several times but even if I managed to add the code, it was displayed outside of the div that outputs the variables so won't work..
My code without translation string is:
add_shortcode('cv', 'vp_cv');
function vp_cv($atts, $content=null) {
extract(shortcode_atts(array(
'number' => 6
), $atts));
global $post;
$output .= '<div class="container">';
$query = new WP_Query('post_type=resume&posts_per_page=' . $number . '&cat=' . $categories);
while($query->have_posts() ) : $query->the_post();
$year = get_post_meta($post->ID, 'resume_year', true);
$title = get_the_title();
$client = get_post_meta($post->ID, 'resume_client', true);
$address = get_post_meta($post->ID, 'resume_address', true);
$output .= '<p class="year">' . $year . '</p>';
$output .= '<p class="cv-title">' . $title . '</p>';
$output .= '<p class="cv-client"> <strong> Client:</strong> ' . $client . '</p>';
$output .= '<p class="cv-address"> <strong> Address:</strong> ' . $address. '</p>';
endwhile;
$output .= '</div>
<div class="clearboth"></div>';
return $output;
}
I'd like to add to the client and address a translatable string like:
<?php _e('Client:','ikos');?>
And it must result inside the tags
Thanks!
Assuming that you're loading text domain correctly, try this:
<?php
// ....
$output .= '<p class="cv-client"> <strong> ' . __( 'Client: ', 'ikos' ) . ' </strong> ' . $client . '</p>';
$output .= '<p class="cv-address"> <strong> ' . __( 'Address: ', 'ikos' ) . ' </strong> ' . $address. '</p>';
// ....
?>
Using __( 'Translatable string', 'your-text-domain' ); return the string translated without echo.
Using _e( 'Translatable string', 'your-text-domain' ); echoes the translated string.
Try it, hope it helps! If something is not clear feel free to ask.

Resources