ACF Wordpress display an image shortcode problem - php

I am trying to display an image on the homepage using a shortcode and ACF.
The problem I am having is that the code is fetching the data for the correct post, but it is displaying a line of text of the image address, rather than an image. When I view the page source it appears the the img tag is not working.
This is the code I have. Any advice would be greatly appreciated.
function grab_home_image_init(){
add_shortcode( 'grab_home_image', 'grab_home_image_cb' );
}
add_action('init', 'grab_home_image_init');
function grab_home_image_cb() {
extract( shortcode_atts(
array(
'numberposts' => 1,
'post_type' => 'project',
),
$atts,
'grab_home_image'
) );
$args = array (
'post_type' => $post_type,
'numberposts' => $numberposts,
);
$ML_home_image = get_posts( $args );
if( ! empty( $ML_home_image ) ){
foreach ( $ML_home_image as $p ){
$output = '<img src="' . the_field('main_image',$p->ID) . '">';
}
}
return $output ?? '<strong>Error</strong>';
}

Related

How to add category option to shortcode

I'm new to creating wordpress shortcodes and have one just about working the way I want. Missing something specific. Currently I am able to put the following on any page - [children] and it pulls a query of all posts from the custom post type "children" I would like to add the option to add the category id within the shortcode - something like [children category="8"] Here is the code I have so far:
add_shortcode( 'children', 'display_custom_post_type' );
function display_custom_post_type(){
$args = array(
'post_type' => 'children',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
);
$string = '';
$query = new WP_Query( $args );
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
$string .= '<div id="childWrapper"><div id="childImage">' . get_the_post_thumbnail() . '</div><div style="clear: both;"></div><div id="childName">' . get_the_title() . '</div><div style="clear: both;"></div></div>';
}
}
wp_reset_postdata();
return $string;
}
Secondary - is it possible for it to show posts in multiple categories, but only where the posts are in each of the categories. For example showing a list of children, who fall under a category for critical care and surgery needed.
Any help would be greatly appreciated.
You should refer Shortcode function from WordPress Codex. Assuming your taxonomy name is category, I made some changes in your current code it should work as per your requirements.
/**
* [children category="5,7,8"]
*/
add_shortcode( 'children' , 'display_custom_post_type' );
function display_custom_post_type($atts) {
$atts = shortcode_atts( array(
'category' => ''
), $atts );
//If category is multiple: 8,9,3
$categories = explode(',' , $atts['category']);
$args = array(
'post_type' => 'children',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page'=> -1,
'tax_query' => array( array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $categories
) )
);
$string = '';
$query = new WP_Query( $args );
if( ! $query->have_posts() ) {
return false;
}
while( $query->have_posts() ){
$query->the_post();
$string .= '<div id="childWrapper"><div id="childImage">' . get_the_post_thumbnail() . '</div><div style="clear: both;"></div><div id="childName">' . get_the_title() . '</div><div style="clear: both;"></div></div>';
}
wp_reset_postdata();
return $string;
}

retrieving and display images by tag in wordpress

i iam creating a section with tags with this code, is a function to retrieve tags and exclude some tags also,
$args = array('name__like' => $name_like, 'exclude' => array(75,177,42,74,197,36,40,140,162,108,86,47,4,29,22,215,87,151,104),'order' => 'ASC');
$tags = get_tags( $args );
if ( !empty( $tags ) && !is_wp_error( $tags ) ) {
$count = count($tags);
$i=0;?>
<ul class="my_term-archive">
<?php
foreach ($tags as $tag) {
$i++;
$tag_link = get_tag_link( $tag->term_id );
$tag_id = get_tag_ID($tag->name);
if(strtolower(substr($tag->name,0,1)) !=$name_like){
continue;
}
//i need a function here to retrieve images with the id of the tag
//attached
//////
$html .= "<li><a href='{$tag_link}' id='{$tag_id}' title='{$tag->name} Tag' class='{$tag->slug}'>";
$html .= "{$tag->name}</a></li>";
}
}
echo $html;
?>
</ul>
then i put this code in my functions.php file in wordpress, to make avaliable the tag box in the picture managment, so i can tag pictures now,
function wptp_add_tags_to_attachments() {
register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init' , 'wptp_add_tags_to_attachments' );
so my question is how can find and display the images by the id tag ?
sorry my bad english, is not my native lenguage. any help is very welcome. thanks
You can actually handle this with a basic WP_Query call. There's lots of details and options for the WP_Query object here, but I'd think you could do something like this:
$args = array(
'post_type' => 'attachment',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'whatever-your-tag-slug-is',
),
),
);
$query = new WP_Query( $args );

Get images from gallery in taxonomy

There seems to be several answers to similar questions but I have not yet found one working for me.
I have a custom post-type called entertainement. entertainement has a taxonomy called ent_categories.
One of the ent_categories is called Event
Each Event has a gallery and I am trying to make a query that will return the latest 10 images that has been added to any of the CPT entertainment with the category Event.
Im hoping to receive a list of urls in an array.
From what I read here something like this should do the trick:
$arg = array(
'post_status' => 'inherit',
'posts_per_page' => -1,
'post_type' => 'attachment',
);
$arg['tax_query'] = array(
array(
'taxonomy' => 'ent_categories',
'field' => 'name',
'terms' => array( 'Event' ),
),
);
$the_query = new WP_Query( $arg );
var_dump($the_query);
The var_dump($the_query); displays a lot of things but no images?
Any tips on this one?
Thank you
EDIT:
I just saw that I can do this:
function pw_show_gallery_image_urls( $content ) {
global $post;
// Only do this on singular items
if( ! is_singular() )
return $content;
// Make sure the post has a gallery in it
if( ! has_shortcode( $post->post_content, 'gallery' ) )
return $content;
// Retrieve all galleries of this post
$galleries = get_post_galleries_images( $post );
$image_list = '<ul>';
// Loop through all galleries found
foreach( $galleries as $gallery ) {
// Loop through each image in each gallery
foreach( $gallery as $image ) {
$image_list .= '<li>' . $image . '</li>';
}
}
$image_list .= '</ul>';
// Append our image list to the content of our post
$content .= $image_list;
return $content;
}
add_filter( 'the_content', 'pw_show_gallery_image_urls' );
This result in that all the gallery images urls get displayed below the images in the gallery.
Maybe this function could be called from a page instead than from functions.php?
You were on the right track, but you're querying for posts of type attachment with a term of the ent_categories taxonomy which only applies to entertainement posts, so there won't be any of them as you'll see if you:
var_dump($the_query->posts);
If you dump all $the_query you'll see lots of things because it's a WP_Query object.
You need to query your entertainement posts: (Be careful because you have a typo in your slug!)
$arg = array(
'posts_per_page' => -1,
'post_type' => 'entertainement',
);
$arg['tax_query'] = array(
array(
'taxonomy' => 'ent_categories',
'field' => 'name',
'terms' => 'Event',
),
);
$the_query = new WP_Query( $arg );
Then you can iterate the posts and get the gallery items like this:
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
if (get_post_gallery()) :
echo get_post_gallery();
print_r(get_post_gallery_images());
endif;
}
/* Restore original Post Data */
wp_reset_postdata();
}
get_post_gallery_images() will get you an array of the URL's of gallery images
get_post_gallery() will get you actual HTML to print the gallery.

Display custom posts with shortcode

I've been searching this forum for help for 2 days now but I can't find a solution for what I'm trying to achieve.
I have a custom post type called "product" and inside each post in that post type there are two custom fields called produktnamn(a textfield with product name) and produktbild(a product image). that's it.
Now I need a shortcode to display a particular post in that post type by entering it's slug (which is the same as it's name).
like this:
[product name="myproduct"]
That will render the name and product image inside some HTML tags.
Please help!
My code so far:
//Product image
function cpt_content_func($atts){
extract( shortcode_atts(['name' => null], $atts ) );
$args = [
'name' => $slug,
'post_type' => 'product',
'numberposts' => 1
];
$posts = get_posts( $args );
$post = $post[0];
$title = $post->title;
$id = $post->ID;
get_post_meta('produktbild', $id);
$content = $post[0]->post_title;
return '<h3>'.$content.'</h3>';
}
add_shortcode('product','cpt_content_func');
Solved it. Here's the code!
function display_custom_post_type( $atts ){
// Handle the attributes
$atts = shortcode_atts(
array(
'cat' => ' '
),
$atts, 'products_by_cat' );
// Set default values for the post
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => 'publish',
'category' => $atts['cat']
);
// Get posts
$posts_array = get_posts( $args );
// Check if posts exist
if( !empty($posts_array) ){
$string = '<div class="row">';
foreach( $posts_array as $post ){
$string .= '<div class="col-sm-3 text-center">';
$string .= '<div class="product-container">';
$string .= '<a href="' . get_field( "order_link", $post->ID ) . '">';
$string .= '<img class="product-img" src="' . get_field( "produktbild", $post->ID ) . '">';
$string .= '</a>';
$string .= '</div>';
$string .= '<p class="product-btn">' . get_field( "produktnamn", $post->ID ) . '</p>';
$string .= '</div>';
}
$string .= '</div>';
} else {
// Didn't find any posts.
}
// Return HTML
return $string;
I agree with James--I'd just use woocommerce.
But you first step has to be learning how to build a shortcode: http://code.tutsplus.com/tutorials/wordpress-shortcodes-the-right-way--wp-17165
Once you've attempted creating your own shortcodes, if things still aren't working, that's the time to ask for help.

Wordpress Get author Posts

Wonder if someone can help; its seems a bit complicated to me;
Baisically I've added this function in my wordpress site, so people can change the author's name;
add_filter( 'the_author', 'guest_author_name' );
add_filter( 'get_the_author_display_name', 'guest_author_name' );
function guest_author_name( $name ) {
global $post;
$author = get_post_meta( $post->ID, 'author_name', true );
if ( $author )
$name = $author;
return $name;
}
But now I want to add some code to show a list of posts from the current posts author but its returning the original author not the author that the top function changed it to; below is the function I'm using to do this, is this possible to change?
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $author_name->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
$output = '<div class="morepost"><h3>More posts from this author</h3>';
foreach ( $authors_posts as $authors_post ) {
$output .= '<li>' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</li>';
}
$output .= '</ul></div>';
return $output;
}
then this;
<?php echo get_related_author_posts(); ?>
Know its a bit complicated, if anyone can help that be great
D
This is what Ive come up with so far but could someone show me where I'm going wrong with this code
<?php $author = get_post_meta( $post->ID, 'author_name', true ); $args
= array(
'meta_query' => array (
array(
'key' => 'author_name',
'value' => $author
)
), 'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ); ?> <?php if ( $wp_query->have_posts() ) : ?> <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?> DO something
<?php endwhile; ?> NOT POSTS <?php endif; ?>
You need to get post based on the author meta value,
So get the virtual author name first for that post,
$author = get_post_meta( $post->ID, 'author_name', true );
Now get all posts based on this value,
$args = array(
'meta_query' => array(
array(
'key' => 'author_name',
'value' => $author
)
),
'post__not_in' => array( $post->ID )
'posts_per_page' => 3
);
$posts = get_posts($args);

Categories