WordPress - Display Custom Field on Post - Pull Post Title and URL - wordpress

Okay, I have the code pulling the custom fields URL and title of the url. Now I can't seem to get it to show the second featured blog. Here is the working code.
<?php $related = get_post_meta($post->ID, "Featured-Blog", $single=true);
$related=explode(',',$related);
$args = array_merge( array('post__in' => $related, $wp_query->query ) );
query_posts($args);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="<?php the_ID(); ?>">
<p class="caption"><?php the_title(); ?></p>
</div>
<?php endwhile; else: ?>
<p>no related</p>
<?php endif; wp_reset_query();?>
This code example here produces two results, which is almost what I want. Which is caused by the foreach I believe. I do not want to use the code below, but I need to find a way to add the foreach I think to get it to list all of the featured-blogs if I have more than one.
<?php
$custom_fields = get_post_custom($post_id); //Current post id
$my_custom_field = $custom_fields['Featured-Blog']; //key name
foreach ( $my_custom_field as $key => $url )
echo $key ="<a href='".$url."'>TEST</a><br /><br /><br/>";
?>
Here is a screenshot showing my Custom Fields if it helps at all, and the results they are showing on the site. screenshot

Example 1: You are using the actual word 'TITLE' for the link use <?php the_title() ?> instead
Example 2: You are not building link at all. Your href attribute is empty. Cut echo $featured_blog1 and paste it to href attribute to end up like so:
Example 3: Same as 2
Also you can delete dose instructions or put them inside <?php ?> code so they are not visible to viewers.
Hope this helps.
If you need more info just ask. ;)

Related

Wordpress outputting same video url's

I'm trying to make a row layout where people can add an mp4 with ACF. So far so good, but when I try to add multiple video sections in the same post it outputs the same video in every player, even though they are different in the backend.
Does anyone know what I'm doing wrong?
Row layout:
<?php if (get_row_layout() == 'video') : ?>
<?php get_template_part('template-parts/sections/section', 'video'); ?>
<?php endif; ?>
Video section part
<div class="section section-type-video flex">
<?php
$video_mp4 = get_field('video_file'); // MP4 Field Name
$video_poster = get_field('video_poster_image'); // Poster Image Field Name
// Build the Shortcode
$attr = array(
'mp4' => $video_mp4,
'poster' => $video_poster,
'preload' => 'auto'
);
echo wp_video_shortcode($attr);
?>
</div>
Many thanks in advance!
now i have made a Flexible Content field called "video-section"
and created a layout called "video-main' which contains a URL field called
"video-url"
<?php if( have_rows('video-section') ): ?>
<?php while( have_rows('video-section') ): the_row(); ?>
<?PHP
//get the URL
if( get_row_layout() == 'video-main' ): ?>
<?php echo the_sub_field('video-url'); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
I used this code to echo all different URLs that included in the post that I was created for testing try it if that what you want to do
To answer my own question, I was targetting the wrong fields.
Needed to target sub_fields instead of get_field.
In ACF I set the return value of my subfield to array instead of url, to finish it I passed the url variable in the video shortcode array as an attribute.
Correct code below:
<div class="section section-type-video flex">
<?php
$video_mp4 = get_sub_field('video_file'); // MP4 Field Name
$video_poster = get_sub_field('video_poster_image'); // Poster Image Field Name
$video_url = $video_mp4['url'];
// Build the Shortcode
$attr = array(
'mp4' => $video_mp4,
'src' => $video_url,
'poster' => $video_poster,
'preload' => 'auto'
);
echo wp_video_shortcode($attr); ?>
</div>
please check the ACF Documentation for get_row_layout() how to display it
Click here! to see it

ACF Post Object empty return

I have custom post type called "Projects" and inside single-project.php view want to show Posts related to Project.
I have tried to solve that problem by using ACF's Post Object but got nothing from The Loop.
I searched for the solution on Stackoverflow and AFC support page and I could not find a problem.
Maybe this 'Post Object' option is not even created for something like this. I don't know.
Code inside single-project.php
if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post );
?>
<div>
<h3><?php the_title(); ?></h3>
</div>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
ACF configuration
https://i.imgur.com/FNnUbiw.jpg
New post configuration
https://i.imgur.com/IHQqR0P.jpg
Custom post type output
https://i.imgur.com/YFugBZl.jpg
In the place of question marks, I want to show Posts related to that Project.
Since you have allowed multiple values for the field, obtained value will be in array. And since you have set to return value as post object, the value obtained will be the array of objects. After array is obtained, you can loop through those to show related projects.
<div class="related-projects">
<?php
$projects = get_field( 'povezani_projekt' );
global $post;
?>
<?php if ( ! empty( $projects ) ) : ?>
<h3>Related Projects</h3>
<?php foreach ( $projects as $post ) : ?>
<?php setup_postdata( $post ); ?>
<h4><?php the_title(); ?></h4>
<?php wp_reset_postdata(); ?>
<?php endforeach; ?>
<?php endif; ?>
</div><!-- .related-projects -->

showing the ten post thumbnail

I want to show latest ten post of my security category with date and post title and the post thumbnail.
To show the pic I have faced with problem. According to this article
http://www.wpbeginner.com/beginners-guide/how-to-add-featured-image-or-post-thumbnails-in-wordpress/
when we want to showing the thumbnail first of all we should have to copy the following code to the function.php file
add_theme_support( 'post-thumbnails' );
and using the
<?php the_post_thumbnail(); ?>
To show pic of the post, I use this code into the loop but it doesn’t work. my code is here:
<?php query_posts('securitysoft=CATEGORYNAME&showposts=10');
while ( have_posts() ) : the_post(); ?>
<br/>
<?php
php the_post_thumbnail();
?>
<br/>
<?php the_time(__('j/F/ Y','kubrick')) ?>
<br/>
<?php the_title();?>
<?php endwhile; ?>
The main problem is that the picture does not show. To prove this I am taking post Id of the post that has thumbnail(292) like this :
<?php if ( has_post_thumbnail(292))
{
echo "<script type='text/javascript'>alert('yes')</script>";
has_post_thumbnail(292);
}
else
{
echo "<script type='text/javascript'>alert('no')</script>";
has_post_thumbnail(292);
}
?>
note:292 is the post id
The result of above code is no.
I have replaced php the_post_thumbnail(); with each one of following code but does not work:
get_the_post_thumbnail($post->ID);
echo get_the_post_thumbnail($post->ID);
get_the_post_thumbnail($post_id, 'thumbnail');
echo get_the_post_thumbnail($post_id, 'thumbnail');
<?php echo get_the_post_thumbnail($post_id, 'thumbnail', array('class' => 'alignleft')); ?>
And this is my function.php :
<?php
add_theme_support( 'post-thumbnails' );
if ( function_exists('register_sidebar') )
register_sidebar();
?>
do you set the feature image!! remember if you don't set the feature image. the image won't display. because this command that you have used only take the thumbnail. to set the feature image go to the feature image panel and click on the Set featured image link.

Display latest post from every post type

I displayed all the post from all the post type in the page. Now i want to display all the latest post from all the post type. I want only one post from all the post type.
To display all the post from all post type i used the following code.
<?php query_posts(array('post_type'=>array('a','b','c'), 'posts_per_page' => 2)); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="issue-content">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail();} ?>
<?php get_template_part( 'content-issues', 'page' ); ?>
<?php comments_template( '', true ); ?>
</div><!--issue-content-->
<?php endwhile; // end of the loop. ?>
i tried with the following code
<?php query_posts(array('post_type'=>array('a','b','c'), 'posts_per_page' => 1)); ?>
i cant get from all the post type. How can i get the latest post from all the post type.
You should use multiple query_posts()
query_posts('post_type=a&posts_per_page=1');
query_posts('post_type=b&posts_per_page=1');
query_posts('post_type=c&posts_per_page=1');
You can also use, get_posts(); function for getting all posts from every post types.

Dynamically display posts by one author AND from one category on the same page?

I'm trying to edit my author.php wordpress template so that it shows posts by any one author, but only from one particular category. So far, I've been trying the query_posts function which fetches the category okay, but not the author. Depending on which way I do it, so far the posts either don't display at all or all posts in that category appear regardless of the author.
This is the appropriate code which I've seen quoted by a wordpress.org admin, but it doesn't work for me and I can't find any other examples. Any ideas why that doesn't work? Thanks for your help in advance.
//Gets author info to display on page and for use in query
<?php
$curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
?>
//Queries by category and author and starts the loop
<?php
query_posts('category_name=blog&author=$curauth->ID;');
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
//HTML for each post
<?php endwhile; else: ?>
<?php echo "<p>". $curauth->display_name ."hasn't written any articles yet.</p>"; ?>
<?php endif; ?>
============ ALSO TRIED ============
<?php
new WP_Query( array( 'category_name' => 'blog', 'author' => $curauth->ID ) );
?>
This doesn't work either, however it does filter the posts by author, just not by category! What am I doing wrong?
Thanks!
This task can be done using pre_get_posts filter. By this way it's also possible to filter for author in addition than for category:
// functions.php
add_action( 'pre_get_posts', 'wpcf_filter_author_posts' );
function wpcf_filter_author_posts( $query ){
// We're not on admin panel and this is the main query
if ( !is_admin() && $query->is_main_query() ) {
// We're displaying an author post lists
// Here you can set also a specific author by id or slug
if( $query->is_author() ){
// Here only the category ID or IDs from which retrieve the posts
$query->set( 'category__in', array ( 2 ) );
}
}
}
I just had this same issue, which I solved using a check for if(in_category('blog')) after the loop started, like this:
if ( have_posts() ) : while ( have_posts() ) : the_post();
if(in_category('blog')) {
?>
<!-- Loop HTML -->
<?php } endwhile; else: ?>
<p><?php _e('No posts by this author.'); ?></p>
<?php endif; ?>
Of course the $curauth check would come before this.

Resources