I am trying to create a custom block using ACF. I have followed their details here: https://www.advancedcustomfields.com/resources/blocks/
I am having an issue displaying the content from my relationship field though...
I have setup the custom post type
I have created the ACF custom field which links to the block
I have added content to my custom posts.
It displays 2 posts.. but none of the data.
Attached is my code and reviewing the ACF site I cannot work out what I am doing wrong. Please help.
<?php
/**
* Client Slider Template.
*
* #param array $block The block settings and attributes.
* #param string $content The block inner HTML (empty).
* #param bool $is_preview True during AJAX preview.
* #param (int|string) $post_id The post ID this block is saved to.
*/
?>
<?php
$featured_posts = get_field('clients');
if( $featured_posts ): ?>
<div class="owl-carousel owl-theme work-snippets">
<?php foreach( $featured_posts as $post ):
setup_postdata($post); ?>
<div>
<?php
$image = get_field('client_logo');
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
<p><strong><?php the_field( 'project_title' ); ?></strong></p>
<p><?php the_field( 'project_overview' ); ?></p>
</div>
<!-- Close Work Snippet -->
<?php endforeach; ?>
</div>
<!-- Work Snippets -->
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Related
I need help with filtering the next_post_link(), and previous_post_link() functions in WordPressss. I would like it to only show next/previous post with the same field answer as the current field.
I have a custom field set on the post type that is a $post_object.
The admin creates content for the post and chooses a category from a post_object drop down field.
These next_post_link(), and previous_post_link() buttons are going on top of my /single-featured_project/ (single-post_type) page.
Thank you for all your help :),
Happy Coding
<?php
/**
* The template for displaying featured project single posts.
*
* #package understrap
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
get_header();
?>
<?php
the_post();
$cat = '';
$cat = get_the_category();
if (is_array($cat))
$cat = $cat[0];
?>
<section class="featured-project-header" style="background-color: #13293C; padding-top: 6rem; padding-bottom: 1rem;">
<div class="container">
<div class="box" style="display: flex; align-items:center;">
<img style="max-height: 22px;" src="/wp-content/uploads/2018/09/Symbol-6-–-1#2x.png">
<span class="featured-project-title"><h1 class="mb-0"> <?php the_title();?></h1></span>
</div>
<h3 style="margin-left: 55px;" class="featured-project-location"><?php echo do_shortcode('[acf field="location"]'); ?></h3>
<!-- Displays Project Category to Filter By -->
<?php
$post_object = get_field('project_category');
if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post );
?>
<?php the_title(); ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<!-- Ends Project Category to Filter By -->
<div class="float-right"><?php next_post_link(); ?></div>
<div class="float-left"><?php previous_post_link(); ?></div>
</div>
</section>
I've created a Field group called 'Team Members' and set the field type as repeatable with three sub fields; Image, Position and Name.
The rule for this group is to show within a custom widget I've created. When I drag the widget in the Appearance > Widgets area of WordPress I can see it is indeed there and can add members / images to my widget.
Where I've hit a brick wall is trying to output this data on the front end through my widget template.
This is my code:
<?php if( have_rows('team_members') ): ?>
<ul class="slides">
<?php while( have_rows('team_members') ): the_row();
// vars
$image = get_sub_field('image');
$content = get_sub_field('name');
$link = get_sub_field('position');
?>
<li class="slide">
<?php if( $link ): ?>
<a href="<?php echo $link; ?>">
<?php endif; ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
<?php if( $link ): ?>
</a>
<?php endif; ?>
<?php echo $content; ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Where am I going wrong?
To get values from your custom widget via ACF the syntax is:
if ( have_rows( 'team_members', 'widget_' . $widget_id ) ) :
while have_rows( 'team_members', 'widget_' . $widget_id ) ) : the_row();
// get sub fields ...
endwhile;
endif;
I'm trying to create a page that pulls up a custom post type (team) and the corresponding advanced custom fields and displays them on the template team page. I'm doing this in Genesis by merging one piece of code made for Wordpress (non-genesis) following this tutorial.
I made some progress but I got stuck at the advanced custom fields. For example, <?php the_title(); ?> actually calls the PAGE title, instead of the Custom Post's Title. And the other fields (position, telephone, etc) aren't working - they're not being called at all. I'm sure it's an issue in how I merged this code with the Genesis.
<?php
/**
* This file adds the city team template to any Genesis 2.0+ Theme.
*
* #author Jim Thornton
* #package InboundFound
* #subpackage Customizations
*/
/*
Template Name: Team
*/
?>
<?php
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'your_custom_loop' );
function your_custom_loop() {
// Get 'team' posts
$team_posts = get_posts( array(
'post_type' => 'team',
'posts_per_page' => -1, // Unlimited posts
'orderby' => 'title', // Order alphabetically by name
) );
if ( $team_posts ):
?>
<section class="row profiles">
<div class="intro">
<h2>Meet The Team</h2>
<p class="lead"></p>
</div>
<?php
foreach ( $team_posts as $post ):
setup_postdata($post);
// Resize and CDNize thumbnails using Automattic Photon service
$thumb_src = null;
if ( has_post_thumbnail($post->ID) ) {
$src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'team-thumb' );
$thumb_src = $src[0];
}
?>
<article class="col-sm-6 profile">
<div class="profile-header">
<?php if ( $thumb_src ): ?>
<img src="<?php echo $thumb_src; ?>" alt="<?php the_title(); ?>, <?php the_field('team_position'); ?>" class="img-circle">
<?php endif; ?>
</div>
<div class="profile-content">
<h3><?php the_title(); ?></h3>
<p class="lead position"><?php the_field('team_position'); ?></p>
<?php the_content(); ?>
</div>
<div class="profile-footer">
<i class="icon-mobile-phone"></i>
<i class="icon-envelope"></i>
<?php if ( $twitter = get_field('team_twitter') ): ?>
<i class="icon-twitter"></i>
<?php endif; ?>
<?php if ( $linkedin = get_field('team_linkedin') ): ?>
<i class="icon-linkedin"></i>
<?php endif; ?>
</div>
</article><!-- /.profile -->
<?php endforeach; ?>
</section><!-- /.row -->
<?php endif;
}
genesis();
"You must pass a reference to the global $post variable, otherwise functions like the_title() don't work properly."
https://codex.wordpress.org/Function_Reference/setup_postdata#Parameters
I currently have a setup where i have the following files being called in order.
in my header.php, i am calling <?php get_theme_part( 'front', 'current-front' ); ?>
The contents of that file are
<?php query_posts("showposts=1&cat=6"); ?>
<?php
/**
* The template for displaying list of recent posts on the front page.
*
* #package WordPress
* #subpackage xxx
* #since xxx 1.0
*/
if ( !have_posts() )
return;
?>
<div class="front-current-print">
<div class="current-print">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_theme_part( 'current-print' ); ?>
<?php endwhile; ?>
</div>
</div>
That file is calling current-print.php, which has this code:
<?php
?><article <?php post_class( 'compact bg-secondary' ); ?>>
<?php the_post_thumbnail( 'current-print' ); ?>
<h5 class="title">
<a class="inverse" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h5>
<div class="entry-summary"><?php the_excerpt(); ?></div>
<?php the_post_episode(); ?>
</article>
This code allows me to have a thumbnail for a specific post from a specific category.
This works fine on the front page, but it seems that for all inside pages and posts it will only display the category and the post. I've tried taking out <?php query_posts("showposts=1&cat=6"); ?> and the posts come through fine. Is there any way i can have that script only run in the header and not affect the rest of the site?
You need to use wp_reset_query();:
wp_reset_query() restores the $wp_query and global post data to the original main query. This function should be called after query_posts(), if you must use that function.
See: http://codex.wordpress.org/Function_Reference/wp_reset_query
In your case this would be:
<?php query_posts("showposts=1&cat=6"); ?>
<?php
/**
* The template for displaying list of recent posts on the front page.
*
* #package WordPress
* #subpackage xxx
* #since xxx 1.0
*/
if ( !have_posts() )
return;
?>
<div class="front-current-print">
<div class="current-print">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_theme_part( 'current-print' ); ?>
<?php endwhile; ?>
</div>
</div>
<?php wp_reset_query(); ?>
I'm trying to implement the Co-Authors Plus Plugin. It says that you will need to change some PHP in the theme. It outlines the changes on this Link.
I believe I have found where I would need to change it in my theme. In a file called "post-author-info.php".
This is what it looks like:
<?php
/**
* Post Author Info
*
* #package WP Journal
* #subpackage Include
*/
?>
<div id="authorinfo" class="columns alpha omega marT30 marB20">
<?php echo get_avatar( get_the_author_meta('email'), '80' ); ?>
<h5 class="marB10"><?php _e('By', 'contempo'); ?>: <?php the_author(); ?></h5>
<p><?php the_author_meta('description'); ?></p>
<div class="clear"></div>
</div>
Just adding this line <?php if ( function_exists( 'coauthors' ) ) { coauthors(); } else { the_author(); } ?> seems to give me something that I want. Both "Admin" And "Andrew Gable" are displayed.
Here is the screen shot when changes are applied.
I'm unsure on how to get it to link correctly, and how to handle photos and multiple Bio's.
Thanks
You might wanna post this question at https://wordpress.stackexchange.com/
I had a similar issue and resolved it this way:
global $post;
$author_id=$post->post_author;
foreach( get_coauthors() as $coauthor ): ?>
<div class="entry-author co-author">
<?php echo get_avatar( $coauthor->user_email, '96' ); ?>
<h3 class="author vcard"><span class="fn"><?php echo $coauthor->display_name; ?></span></h3>
<p class="author-bio"><?php echo $coauthor->description; ?></p>
<div class="clear"></div>
</div><!-- .entry-author co-author -->
<?php endforeach;
Be sure to use that global $post;and the foreach-loop.
This function will display an author box containing author image/avatar, author name with link to its archive, and the author's bio. You might want to use your own theme's CSS classes though.