I am trying to get an image URL with attachment size using an advanced custom field. The field is set to ID. I've used this approach to get other images by ID with no problem. Yet this one isn't pulling the rendered image. It's pulling the ID number and displaying it on the page. I'm stumped...
<?php
$the_query = new WP_Query( array(
'post_type' => 'listicles',
'tax_query' => array(
array (
'taxonomy' => 'visibility',
'field' => 'slug',
'terms' => 'listicles-resortsvisible',
),
),
) );
$listicleimage = the_field('listicle_featured_image', $post->ID);
$listicleimgsize = 'listicle-thumb';
$listicleimg_array = wp_get_attachment_image_src($listicleimage, $listicleimgsize);
$listicleimg_url = $listicleimg_array[0];
?>
<ul
class="row small-up-1 medium-up-2">
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li class="small-12 medium-6 column">
<img
src="<?php echo $listicleimg_url; ?>"
class="align-center" />
//THE REST OF MY CONTENT//
</li>
<?php endwhile;?>
<?php endif; ?>
</ul>
<?php wp_reset_query(); ?>
You only need to change
the_field('listicle_featured_image', $post->ID);
to
get_field('listicle_featured_image', $post->ID);
You should be using wp_get_attachment_image and not wp_get_attachment_image_src.
wp_get_attachment_image accepts ID's as an argument.
Source: https://developer.wordpress.org/reference/functions/wp_get_attachment_image/
Add this to functions.php
function img_by_id($id, $size, $url) {
$the_img = wp_get_attachment_image($id, $size);
if ($url != true) {
return $the_img;
}
else {
$img_src = get_attr($the_img, 'src');
return $img_src;
}
}
function get_attr($html, $attr) {
$pc = explode($attr.'="', $html);
$pc2 = explode('"', $pc[1]);
return $pc2[0];
}
Then
<?php
$the_query = new WP_Query( array(
'post_type' => 'listicles',
'tax_query' => array(
array (
'taxonomy' => 'visibility',
'field' => 'slug',
'terms' => 'listicles-resortsvisible',
),
),
) );
$listicleimage = the_field('listicle_featured_image', $post->ID);
$listicleimg_url = img_by_id( $listicleimage, 'listicle-thumb', true);
?>
if you need full image attribute with sizes $img = img_by_id( $listicleimage, 'listicle-thumb', false);
If you need specific attribute from html tag $attr = get_attr($html, $attr);
I have this on my blank WP theme template.
Related
Try to achieve this:
I made a ACF (custom field) in my WooCommerce Product and now I try to get this field shown in my template with this code:
<ul class="products">
<?php
$args = array(
'posts_per_page' => 20,
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_type',
'field' => 'name',
'terms' => 'grouped',
),
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$linked_with_items = the_field('linked_with_items');
the_title('<strong>', '</strong>'); echo '<br />';
echo $linked_with_items;
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
But whatever I try also with get_field() the field does not show in my template.
Can someone help?
https://www.advancedcustomfields.com/
This is the final code fyi
<?php if( have_rows('catalogue') ): ?>
<?php
while( have_rows('catalogue') ): the_row(); // catalogue is the field
the_sub_field('linked_with_items'); ?>
<?php endwhile; ?>
<?php endif; ?>
You could try with this:
$linked_with_items = get_field('linked_with_items', get_the_ID());
If that doesn't work, just as a test, you could try to simply loop over posts with a foreach
foreach ( $loop->posts as $post ) {
$linked_with_items = get_field('linked_with_items', $post->ID);
}
If none of those work, please make sure that your product actually have that custom field, double check the ACF field settings (rule section), the field slug, and double check your product edit page to see if the fields shows there.
I have three drop-down select option. Inside each drop-down, I have shown the Sub-category for example shown below.
Service
Mobile development
Web development
Software development
Technology
Android
IOS
Java
Industry
Commerce
Education
Lifestyle
Here Service, Technology, and Industry are the category of CPT and inside it like Mobile Development, IOS and Commerce are the sub-categories of the main category.
Now what I want to do on change of any of the three category show post of that sub-category only.
For example, if I select IOS then show the post of IOS only similar with others if the user selects IOS, Mobile Development, and Lifestyle then show post of all three.
To achieve this I have to use the change jQuery function and calling the ajax on change event. The problem is I am getting the same post for all the post select please help me through it.
You Can achieve this by Following this -
Firstly You Register a Custom Post Type like 'Course' in my Code.
Then you need to Add Custom Taxonomy For this like 'Course Type' in my Code.
Then get All Posts with using Page Template or Any other method which you are followed.
I add this code in my Page Template then attach those Page Template to a Page, Here i get all posts when page reload with thre Select boxes for Parent Terms like 'Service', 'Technology', 'Industry' -
<div class="course_content">
<?php $parent_terms = get_terms( 'course_type', array( 'parent' => 0 ) );
foreach( $parent_terms as $parent_term ){
$children_terms = get_terms('course_type', array('parent' => $parent_term->term_id) );
if($children_terms){
?>
<select class="select_course_type <?php echo 'course_type-'.$parent_term->slug; ?>">
<option value=""><?php echo 'Select '.$parent_term->name; ?></option>
<?php
foreach( $children_terms as $children_term ){
echo '<option value="'.$children_term->slug.'">'.$children_term->name.'</option>';
}
?>
</select>
<?php
}
}
?>
<input type="hidden" value="" id="selected_course_type" />
<div class="display_posts">
<?php
$args = array(
'post_type' => 'course',
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) :
echo '<ul>';
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<h4><?php the_title() ;?></h4>
</li>
<?php endwhile;
echo '</ul>';
endif;
wp_reset_query(); ?>
</div>
</div>
Then you add jquery code in your JS File For Calling Ajax, like this -
$('.select_course_type').on('change', function(){
var s = $(this).val();
$("option", this).removeAttr('selected');
$("option[value='" + s + "']", this).attr('selected', 'selected');
ct_Array = [];
$('.select_course_type').each(function(){
var t = $(this).val();
if(t != ''){
ct_Array.push($(this).val());
}
});
var ajax_url = FrontAjax.ajaxurl;
data = {
action: 'ajax_filter_for_courses',
s_ctValues: ct_Array,
};
$.post( ajax_url, data, function(response) {
if( response ){
$('.display_posts').html(response);
}
});
});
Then add this code in Functions.php File for getting Posts after Filter-
add_action('wp_ajax_nopriv_ajax_filter_for_courses', 'filter_for_courses');
add_action('wp_ajax_ajax_filter_for_courses', 'filter_for_courses');
function filter_for_courses() {
$explode_ct = $_POST['s_ctValues'];
$tax_query = array();
if(isset($explode_ct)) {
$tax_query[] = array(
'taxonomy' => 'course_type',
'field' => 'slug',
'terms' => $explode_ct
);
}
$args = array(
'post_type' => 'course',
'tax_query' => $tax_query
);
$filter_query = new WP_Query( $args );
if ( $filter_query->have_posts() ) {
echo '<ul>';
while ( $filter_query->have_posts() ) : $filter_query->the_post();
?>
<li>
<h4><?php the_title() ;?></h4>
</li>
<?php
endwhile;
echo '</ul>';
}
wp_reset_query();
die(); //stop "0" from being output
}
$posts_array = get_posts(
array(
'posts_per_page' => 6,
'post_type' => 'acme_portfolio',
'tax_query' => array(
array(
'taxonomy' => 'portfolio_category',
'field' => 'term_id',
'terms' => array($post_id),
)
)
));
Put this in function.php
I am trying to display same custom post query multiple time in same page. The between the post is it will call different custom taxonomy based on one custom post type. When I am trying to call same custom post second time its not working. But showposts =2 is working but I need to display one post for a single post query. Here is the codes:
<div class="col-sm-6">
<?php
$querevent = new WP_Query( array(
'post_type' => 'tatstory', // name of post type.
'showposts' => 1,
) );
if ( $querevent->have_posts() ):
// Yep, we have posts, so let's loop through them.
while ( $querevent->have_posts() ) : $querevent->the_post(); ?>
<?php if ( has_term('event','eventname' ) && has_term('featured-in-story-page','fetstory' )) { ?>
<div class="listing_inner">
<div class="listing_img">
<?php the_post_thumbnail( 'shop-story', array( 'class' => 'img-fluid' ) ); ?>
</div>
<div class="listing_texts">
<p class="event yellow">Event</p>
<h2><?php the_title(); ?></h2>
</div>
</div>
<?php
} ?>
<?php endwhile;
else :
// No, we don't have any posts, so maybe we display a nice message
echo "<p class='no-posts'>" . __( "Sorry, there are no posts at this time." ) . "</p>";
endif;
?>
</div>
<div class="col-sm-6">
<?php
$quernews = new WP_Query( array(
'post_type' => 'tatstory', // name of post type.
'showposts' => 1,
) );
if ( $quernews->have_posts() ):
// Yep, we have posts, so let's loop through them.
while ( $quernews->have_posts() ) : $quernews->the_post(); ?>
<?php if ( has_term('news','eventname' ) && has_term('featured-in-story-page','fetstory' )) { ?>
<div class="listing_inner">
<div class="listing_img">
<?php the_post_thumbnail( 'shop-newscat', array( 'class' => 'img-fluid' ) ); ?>
</div>
<div class="listing_texts_right">
<p class="event blue">News</p>
<h2><?php the_title(); ?></h2>
</div>
</div>
<?php } ?>
<?php endwhile;
wp_reset_postdata();
else :
// No, we don't have any posts, so maybe we display a nice message
echo "<p class='no-posts'>" . __( "Sorry, there are no posts at this time." ) . "</p>";
endif;
?>
First i would change your WP_Query, example:
$query = new WP_Query( array(
'post_type' => 'tatstory',
'posts_per_page' => '1',
'tax_query' => array(
array (
'taxonomy' => 'eventname',
'field' => 'slug',
'terms' => 'news',
),
array (
'taxonomy' => 'fetstory',
'field' => 'slug',
'terms' => 'featured-in-story-page',
)
),
) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// display content
}
} else {
// display when no posts found
}
wp_reset_postdata(); // Restore original Post Data
The query will only collect posts that have the correct terms set.
Restoring original Post Data on the end (wp_reset_postdata()) may help you with calling a new WP_Query again. But I don't know of any reason why you cannot call it twice in a single page template.
I have a site that was developed by the developer before me and I am the lucky one that gets to try to figure out how to resolve his issues.
I am currently trying to display events from today forward. This is a CPT and has custom fields. I am not sure if I have just looked at this code for to long and am just missing the answer or what but I just cannot see where I am going wrong.
Not only am I not getting ANY results I am getting "Fatal error: Call to a member function format() on a non-object" in my "Dates:" section.
I have read and tried countless possibilities without success. I am hoping that a fresh pair of eyes can help me out.
Here is the code:
<?php
$today = current_time('Ymd');
$args = array(
'post_type' => 'posts_events',
'post_status' => 'publish',
'posts_per_page' => 3,
'meta_query' => array(
array(
'key' => 'start_date',
'compare' => '>=',
'value' => $today,
)
),
'meta_key' => 'start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ));
?>
<!-- CONTENT STARTS -->
<div class="wrapper">
<div class="container_16" id="event-list_container">
<div class="col_14 prefix_1">
<?php
$taxonomy = 'sbts';
$orderby = 'name';
$show_count = 1; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
//'show_count' => $show_count,
//'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
?>
<ul class="taxonomy-list-links">
<?php wp_list_categories( $args ); ?>
</ul>
</div>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="container_14 prefix_1 suffix_1 event-list">
<?php $background_image = MultiPostThumbnails::get_post_thumbnail_id(get_post_type(), 'event-thumb', $post->ID); $background_image = wp_get_attachment_image_src( $background_image,'event-thumb' ); ?>
<div class="col_5 event-photo" style="background-image:url(<?php echo $background_image[0]; ?>);"></div>
<div class="col_8 prefix_5 event-content">
<h2><?php the_title(); ?><?php if($today > get_field('end_date')) echo '</h2><p>Past Event</p>'; ?>
<?php $firm = get_field('host_firm'); ?>
<h3>Member Host Firm: <span class="event-p"><?php echo $firm[0]->post_title; ?></span></h3>
<h3>Location: <span class="event-p"><?php echo get_field('event_location'); ?></span></h3>
<h3>Dates: <span class="event-p"><?php $date = DateTime::createFromFormat('Ymd', get_field('start_date')); echo $date->format('M j, Y'); ?> - <?php $date = DateTime::createFromFormat('Ymd', get_field('end_date')); echo $date->format('M j, Y'); ?></span></h3>
Event Details
</div>
</div>
<?php endwhile; ?>
<div class="wrapper">
<div class="container_16 nested" id="page-navigation_container">
<div class="col_14 gap prefix_1" id="page-navigation"><?php theme_pagination(); ?></div>
</div>
</div>
<?php endif; ?>
</div>
</div>
<!-- CONTENT STOPS -->
<?php get_footer(); ?>
You need to use a new query with the args you want to pass for the loop:
Check the wp codex
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
I have a search form. Choose a taxonomy, and select one or more terms. I want to paginate the results.
Here is the code:
<?php
if(isset($_POST['tax_type'])) {
$tax_type=$_POST['tax_type'];
$terms = $_POST[$tax_type];
$term_list = implode("','", $terms);
$term_list1 = implode(", ", $terms);
$term_list = "'".$term_list."'";
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$giftfinder_args = array(
'post_type' => 'products',
'posts_per_page'=>"1",
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => $tax_type,
'field' => 'slug',
'terms' => explode(',',$term_list),
),
),
);
echo '<div class="results"><span class="eyebrow">search results</span>';
echo '<div class="cat_label">“' . $term_list1 . '”</div></div>';
// the query
$giftfinder_query = new WP_Query( $giftfinder_args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $giftfinder_query;
?>
<?php if ( $giftfinder_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $giftfinder_query->have_posts() ) : $giftfinder_query->the_post();
$thumb = get_the_post_thumbnail(null, 'medium');
?>
<div class="prod"><?php if(empty($thumb)) {echo '<div style="width:265px;height:265px;background:#eee;"></div>';} else {the_post_thumbnail('medium');} ?><span class="truncate"><?php the_title(); ?></span></div>
<?php endwhile; ?>
<div style="clear:both;"></div>
<!-- end of the loop -->
<?php
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'twentyfifteen' ),
'next_text' => __( 'Next page', 'twentyfifteen' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>',
) );
?>
<?php wp_reset_postdata(); ?>
<?php else : get_template_part( 'content', 'none' ); endif; ?>
<?php } ?>
First page is fine, but no results appear on paginated pages.
As you can see, I'm trying to use the pagination fix that turns my query into $wp_query:
// the query
$the_query = new WP_Query( $args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $the_query;
But like I said, no results beyond first page.
I was going to try pre_get_posts but I can't figure out how to code it since every query has unique arguments based on the user's choice of taxonomy and searched terms.
I also tried 'add_args' to the paginate_links() function but could not figure out how to format the resulting get string.
BTW, right now, the query just returns one post to make it easy to check pagination.
Any suggestions appreciated!
Not sure if this is the only issue, but it looks like you have a typo.
You are setting $page:
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
But looking for $paged:
'paged' => $paged,