ACF row to show thumbnails of recent posts - php

I'm trying to create a ACF flexible content row to display the most recent post thumbnails for a given category. However it keeps throwing a critical error and I'm not sure why.
<?php
$section_id = get_sub_field('section_id')
$categories = get_sub_field('categories');
$tags = get_sub_field('tags');
$postnum = get_sub_field('number_of_posts');
if (!is_array($categories)) {
$categories = array($categories);
}
$tags = get_field('my_tags_field');
if (!is_array($tags)) {
$tags = array($tags);
}
$args = array(
'post_type' => 'post',
'numberposts' => $postnum,
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'terms' => $categories
),
array(
'taxonomy' => 'post_tag',
'terms' => $tags
)
)
);
$query = new WP_Query($args);
?>
<style>
</style>
<section class="post_row_with_thumbnails" id="<?php echo $section_id; ?>">
<div class="container-fluid">
<div class="row">
<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>
<div class="col">
<a href="<?php the_permalink(); ?>">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ); ?>" class="project_pics">
<h5 class="posttitle"><?php the_title(); ?></h5>
<h6 class="postdate"><?php the_date(); ?></h6>
</a>
</div>
<?php endwhile; endif; wp_reset_postdata(); ?>
</div>
</div>
</section>
I have tried substituting WP_Query() with get_posts() but it gives me the same critical error.

In line 2 the ; at the end of the line, in this part:
$section_id = get_sub_field('section_id') ; THE DOT AND COMMA IS MISSING
This is the reason for the critical error.

Related

Custom post loop with custom taxonomy in wordpress

I am going wrong somewhere and it is driving me crazy. I am trying to pull posts from a custom post type with a custom taxonomy. I am pulling the taxonomy via an ID in the url and it just seems to show all posts of that post type no matter what I do. Here is my code:
<?php
$cruise = $_GET['reederei'];
$info = get_term_by( 'slug', $cruise, 'reederei' );
$info_id = $info->term_id;
$logo = get_field('logo', $term);
foreach ($taxes as $tax) {
$the_taxes[] = array (
'taxonomy' => 'reederei',
'field' => 'term_id',
'terms' => 'array( $info_id)',
);
}
$the_taxes['relation'] = 'OR';
$query = new WP_Query( array(
'post_type' => 'angebote',
'tax_query' => $the_taxes,
) );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col-sm-4">
<div class="offer_box_container offer_main">
<div class="offer_box_thumbnail"><img src="<?php the_field('featured_image'); ?>"/></div>
<h2 class="offer_inner_title"><?php the_title(); ?></h2>
<div class="offer_inner_sub highlight"><?php the_field('number_of_nights'); ?> Nächte: <?php the_field('travelling_to'); ?></div>
<div class="offer_description"><?php echo custom_field_excerpt(); ?></div>
<div class="col-sm-7 offer_button"><a class="direct_button" href="<?php the_permalink(); ?>" target="_blank">Zum Angebot >>></a></div>
<div class="col-sm-5"><div class="before_price">pro Person ab</div>
<div class="main_price"><?php the_field('price'); ?> -</div></div>
</div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<!-- show pagination here -->
<?php else : ?>
<!-- show 404 error here -->
<?php endif; ?>
$the_taxes[] = array (
'taxonomy' => 'reederei',
'field' => 'term_id',
'terms' => 'array( $info_id)',
);
You turned your 'terms' => array() into a string. See if that helps.

post by taxonomy not showing at all in wordpress

Newbie here,
I'm trying to display the list of my post depends on the category. But it doesn't display my post. I tried the different type of array, but no luck. I named my page taxonomy-blog_category.php
I call the page via site.com/blog_category/category
here's my current code and I know I'm so close but can't figure it out.
Here is the array:
<div class="row">
<?php $ctr = 1;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'blog_post',
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'blog-category',
'field' => 'slug',
'terms' => array('business','people','technology'),
),
),
);
Here is how I display the post
$custom_query = new WP_Query( $custom_args ); ?>
<?php if ( $custom_query->have_posts() ) : ?>
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="col-md-4 text-center">
<div class="content-container">
<div class="wrap">
<figure class="tint t2">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>" width="317px" height="240">
</figure>
</div>
<h2><?php the_title(); ?></h2>
<h3>By <?php the_field('author'); ?> | <span><?php echo get_the_date(); ?></span></h3>
<?php $content = get_field('content'); echo mb_strimwidth($content, 0, 200, '...');?>
<div class="read-more-btn">
read more
</div>
</div>
</div>
<?php $ctr++; endwhile; ?>
I Don't know if this is necessary but here's my code for pagination:
<div class="pagination-holder">
<ul class="pagination">
<?php
if (function_exists(custom_pagination)) {
custom_pagination($custom_query->max_num_pages,"",$paged);
}
?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</ul>
</div>

How to restrict what tags are fetched in WordPress

I have this PHP script to fetch tags (as we use WordPress as a CMS, the tags are "categories" for our partners).... However, I'm trying to free up "tags" to be used on blog posts - but currently the code below fetches ALL tags - how can I restrict this to only tags I specify (happy to enter each Tag ID)
Here is the code:
<?php
$tags = get_tags('order=ASC&orderby=name);
foreach ( $tags as $tag ) { ?>
<?php $tag_name = $tag->slug;
echo '<div class="col-md-12 padbot50" id="tabs-' . $tag->slug . '">';
echo '<div class="green_txt font20">' .$tag->name. '</div>';
//echo $tag_name;
$args = array( 'posts_per_page' => 10, 'cat'=> 11, 'tag' => $tag_name, 'orderby' => 'slug', 'order' => 'ASC' );
query_posts( $args );
// The Loop
?>
<div class="row">
<?php
while ( have_posts() ) : the_post();
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'medium', true);
$thumb_url = $thumb_url_array[0];
$post_id = get_the_ID();
?>
<div class="col-md-6 col-sm-5 col-xs-7 pad10 center"> <a href="<?php the_permalink(); ?>">
<div class="partners_container">
<div class="row" style="height:125px;">
<div class="col-md-12 col-xs-12"><div class="left"> <img src="<?php echo $thumb_url; ?>" height="100%" alt="Volo Commerce Partners - Multichannel Automated Back Office Software. eBay, Amazon, Rakuten" title="Volo Commerce Partners - Multichannel eCommerce Software. Stock & Inventory Management"/><div class="partner-more-icon"></div></div></div>
</div>
<div class="padtop10" style="text-align:left;font-weight:bold;color:#f08f00!important;"> <?php echo '<div>' . the_title() . '</div>';?> </div>
<div class="padtop10 left"> <?php echo '<div>' . the_field('partner_page_description', $post_id ) . '</div>';?> </div>
</div>
</a> </div>
<?php
endwhile;
?>
</div>
</div>
<?php
wp_reset_postdata(); }?>
If anyone could be so kind to tell me how I fetch ONLY certain tag ID's - that would be absolutely perfect!
Thanks muchly!
This is a basic example of WP_Query which should get you started:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'orderby' => 'slug',
'order' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'term_id',
'field' => 11
),
array(
'taxonomy' => 'tag',
'field' => 'term_id',
'field' => array( /* Your Tag IDs */ ),
'operator' => 'IN'
)
)
);
$post_query = new WP_Query( $args );
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) :
$post_query->the_post();
// Inner loop stuff code
endwhile;
endif;
wp_reset_postdata();?>
get_tags function accepts include argument. You could pass by it ids of tags you want to get.
$tags = get_tags('order=ASC&orderby=name');
change to
$tags = get_tags('order=ASC&orderby=name&include=12,45,67');
12, 45 and 67 are ids of tags
get_tags on codex

WP_Query orderby 'rand' not working

Trying to order some posts I'm displaying on a single custom post type page with random, but they aren't random at all. :/
<?php
// Grab the taxonomy term slug for the current post
$terms = get_the_terms( get_the_ID(), 'category-staff' );
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->slug;
}
$on_draught = join( ", ", $draught_links );
?>
<div class="container hidden-xs">
<div class="row">
<div class="col-sm-12">
<hr />
<h3 class="text-center">Other People At Our Great Resort</h3>
</div>
</div>
<div class="row">
<div class="col-sm-12 col-lg-10 col-lg-offset-1">
<div class="row staff-list">
<?php
// WP_Query arguments
$args2 = array (
'post_type' => 'staff',
'tax_query' => array(
array(
'taxonomy' => 'category-staff',
'field' => 'slug',
'terms' => $on_draught,
),
),
'nopaging' => false,
'posts_per_page' => '4',
'order' => 'DESC',
'orderby' => 'rand',
);
// The Query
$query2 = new WP_Query( $args2 );
// The Loop
if ( $query2->have_posts() ) {
while ( $query2->have_posts() ) {
$query2->the_post(); ?>
<div class="staff staff-other col-sm-3 text-center">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php echo get_the_post_thumbnail( $_post->ID, 'large', array( 'class' => 'img-responsive img-circle img-staff' ) ); ?>
<h4><?php the_title(); ?></h4>
<?php if (get_field('staff_job')) { ?>
<p><?php the_field('staff_job'); ?></p>
<?php } ?>
</a>
</div>
<?php }
} else { ?>
<?php }
// Restore original Post Data
wp_reset_postdata(); ?>
</div>
</div>
</div>
</div>
<?php endif; // terms if statement ?>
Turns out it was something to do with WPEngine. They disable rand() from the server and it needs to be enabled manually.
Another solution may be to add this code before running the new WP_Query($args) function.
remove_all_filters('posts_orderby');
https://developer.wordpress.org/reference/functions/remove_all_filters/

How to display custom post type by custom taxonomy?

My homepage is displaing my listings( my custom post type) by the order i enter them. I would like my listings that have the custom taxonomy "tag" (Special offer) to be displayed on my first page from my homepage and after that the rest of the listings exactly how they where before. I am new in to wordpress and hope i asked my question right
This is my homepage code
<div id="content">
<?php include (TEMPLATEPATH . '/lib/slider.php'); ?>
<?php
$args = array(
'post_type' => 'listings',
'paged' => $paged,
'showposts' => 8 ,
'oferta' =>"oferta"
);
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="post propbox <?php if (++$counter % 2 == 0) { echo "lastbox"; }?> clearfix" id="post-<?php the_ID(); ?>">
<div class="archimg">
<?php if( has_term( 'featured', 'type', $post->ID ) ) { ?>
<span class="featspan">Featured</span>
<?php } else if ( has_term( 'sold', 'type', $post->ID ) ){ ?>
<span class="soldspan">Sold</span>
<?php } else if ( has_term( 'reduced', 'type', $post->ID ) ){ ?>
<span class="redspan">Reduced</span>
<?php } ?>
<?php
if ( has_post_thumbnail() ) { ?>
<img class="propimg" src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php get_image_url(); ?>&h=180&w=310&zc=1" alt=""/>
<?php } else { ?>
<img class="propimg" src="<?php bloginfo('template_directory'); ?>/images/dummy.jpg" alt="" />
<?php } ?>
</div>
<div class="cover">
<div class="title">
<h2><?php the_title(); ?></h2>
</div>
<div class="propmeta">
<div class="proplist"><span>Price</span> <span class="propval"> <?php $price=get_post_meta($post->ID, 'wtf_price', true); echo $price; ?></span></div>
<div class="proplist"><span>Location</span> <span class="propval"> <?php echo get_the_term_list( $post->ID, 'location', '', ' ', '' ); ?></span></div>
<div class="proplist"><span>Property type</span> <span class="propval"><?php echo get_the_term_list( $post->ID, 'property', '', ' ', '' ); ?></span></div>
<div class="proplist"><span>Area</span> <span class="propval"> <?php echo get_the_term_list( $post->ID, 'area', '', ' ', '' ); ?></span></div>
</div>
<div class="entry">
<?php wpe_excerpt('wpe_excerptlength_archive', ''); ?>
<a class="morer" href="<?php the_permalink() ?>">Check this</a>
<div class="clear"></div>
</div>
</div>
</div>
<?php endwhile; ?>
<div class="clear"></div>
<?php getpagenavi(); ?>
<?php $wp_query = null; $wp_query = $temp;?>
</div>
This is my main content content sorry how can i rearenge this so it fits my needs
you can use tax_query like by here
$args = get_posts( array(
'post_type' => 'my_post_type',
'tax_query' => array(
array(
'taxonomy' => 'my_taxonomy',
'field' => 'slug',
'terms' => 'webdesign'
)
)
) );
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);
You have to pass the arguments array to you $wp_query->query($args); in which you define the tag name or category name , taxonomy of tag or taxonomy of category
$args = array(
'post_type' => 'your custom post type name',
'paged' => $paged,
'showposts' => 8 ,
'your custom goes here taxonomy' =>"tag name or category name"
);
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);
Generally taxonomy name is what you defined in register_taxonomy('here_goes_taxonomy',array('post_type'), array(...))
All the answers above are great, but you don't necessarily need to pass the tax_query array. Once you have created your custom taxonomy the right way, you can do the following.
<?php
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
$loop = new WP_Query([
'post_type' => 'your_custom_post_type',
'post_status' => 'publish', // fetch only published posts
'posts_per_page' => get_option('posts_per_page') ?: 10, // number of posts to fetch
'paged' => $paged
'your_custom_taxonomy' => 'your_custom_taxonomy_slug_or_name'
]);
// WordPress pagination doesn't work on custom query
// Let's make it work
$temp_query = $wp_query; // store $wp_query in a temporary variable
$wp_query = null; // set it to null
$wp_query = $loop; // store your custom query to $wp_query
if ( $loop->have_posts() ) { // Let's check if we have some posts
while( $loop->have_posts() ){
$loop->the_post();
// add your markup here
}
}
wp_reset_postdata();
$wp_query = null;
$wp_query = $temp_query; // store back the original $wp_query
<?php $cat_terms = get_terms(
array('mobile_category'),
array(
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
'number' => 2 ) );
if( $cat_terms ) : ?>
<?php foreach( $cat_terms as $term ) :
$args = array(
'post_type'=> 'mobile',
'posts_per_page'=> 2,
'post_status'=> 'publish',
'tax_query'=> array(
array(
'taxonomy' => 'mobile_category',
'field' => 'slug',
'terms' => $term->slug,), ),
'ignore_sticky_posts' => true
);
$_posts = new WP_Query( $args );
if( $_posts->have_posts() ) :
while( $_posts->have_posts() ) : $_posts->the_post(); ?>
<span class="color2"><?php echo $term->name ; ?></span>
<?php endwhile;
endif;
wp_reset_postdata();
endforeach; ?>
<?php endif; ?>

Categories