Wordpress outputting same video url's - wordpress

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

Related

Display ACF image field inside foreach loop

I have created a custom taxonomy for my custom post type.
Each term only needs to display its name (which works with $term->name;) and an image. For the image I am using ACF (as I usually do for custom fields on a page or post). It is set up and I can select an image when I add a new term in the dashboard, but how do I display it in a foreach loop? The usual get_field() isn't showing anything.
<ul class="taxonomy-terms">
<?php
$terms = get_terms( array(
'taxonomy' => 'my_taxonomy',
'hide_empty' => false,
) );
?>
<?php foreach ( $terms as $term) { ?>
<li>
<img src="<?php the_field('image'); ?>" />
<?php echo $term->name; ?>
</li>
<?php } ?>
</ul>
The issue being with the_field('image');
The image field is set to return the image url.
Maybe you should add your taxonomy as a parameter when calling the image, try :
<?php if( get_field('image') ): ?>
<img src="<?php the_field('image', $term); ?>" />
<?php endif; ?>
It's better to test the Image Field as you don't wan't to show an empty <img> tag.
Also please check that you didn't change the field name in the Image Field settings.
You can read about that here : https://www.advancedcustomfields.com/resources/image/
And here :
https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
Let's hope this works,
Have a good day.
You should pass the current term of the loop as the second parameter of the ACF function:
<?php the_field('image', $term); ?>
See here:
https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

How to make a page template for displaying custom type posts

I just started WP, and made a table with many rows arrived at WP house. But don't know how to show. basic content well shown but custom fields. learnt that a page have to be created to deal them in order to retrieve custom typed posts.
the following is from my content-movie.php under twentyfourteenchild:
/* translators: %s: Name of current post */
the_content();
// handling movie stuff starts
$p_id = $post->ID;
$ar_fields = array( 'studio','director','starring','grade');
.
.
foreach $ar_fields as $field
$some_field = get_post_custom_values($field, $p_id);
do some thing dealing $some_field...
end for
===================
In order to make a regular page to populate movie-typed custom posts, do I have to put such codes in archive-movie, page-movie single-movie etc ?
I guess, somewhere it can be dealt instead of putting same scripts in many files.
I really want someone to help me go right direction.
firstly create a custom page template by doing the following:
Copy the regular page.php and rename it to page-movies.php.
In the page header add the following php code:
/**
* Template Name: Movie Page
*/
Now the page template should be available in the backend, select the Movie Page as your page template.
Now replace the regular page query with your query, here is an example below:
<?php $args = array( 'numberposts' => 7, 'order'=> 'ASC', 'post_type' => 'movies');
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<div class="item">
<div class="item-content">
<?php if ( has_post_thumbnail() ) : ?>
<div class="thumbnails"> <a class="ajax" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'projects-thumbnail' ); ?>
</a>
<div class="copy">
<h3><a href="<?php the_permalink(); ?> ">
<?php the_title(); ?>
</a></h3>
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
This should do the trick...

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

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. ;)

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.

Load page content and blog post index content (WordPress)

I have a question about Wordpress' template structure and querying posts.
I have my templates setup where the (for example) archive-$posttype.php is built like:
get_header();
$args = 'page_id=18'; //Client Uses page
query_posts($args); the_post();
get_template_part( 'sub-header' );
// Reset Query
wp_reset_query();
?>
<div class="content">
<?php get_template_part( 'loop' ); ?>...
I do this to set my default $post variable for my sub-header.php file which prints out content from that page:
<div id="subheader">
<h1><?php echo get_post_meta($post->ID, 'header_title', true)?></h1>
<?php echo get_post_meta($post->ID, 'header_description', true)?>...
However, using this method on the home.php template, doesn't work:
get_header();
$temp_query = $wp_query;
$page_id = 119; //Client Uses page
#$post = get_page( $page_id );
$args = array('page_id' => $page_id);
$post = new WP_Query( $args ); the_post();
get_template_part( 'sub-header' );
wp_reset_postdata();
?>
<div class="content">
<?php get_template_part( 'loop' ); ?>
<?php get_sidebar( 'news' ); ?>
</div><!--.content -->
<?php get_footer(); ?>
I'm curious why this works on one template and not on the home template. AND, am I going about this the wrong way? What's the correct way to have page content in the sub-header template that in most cases is ACTUALLY related to that current page the user is on.
Thanks!
Not sure I understand your problem exactly but if you are doing what I think, having a chunk of text from a specific page above another loop that pulls in posts of some sort or another I would use the template naming structure.
<?php /* Template Name: My Template */ ?>
This will allow you to use the standard loop to get content from whatever page the clients sets the template to (avoiding the static ids you are using).
Your home page bug could be due to it not being set as the posts page in the reading settings. As I understand it if a page is not set as the posts page it will act like a normal page unless you rewrite the query specifically.
"What's the correct way to have page content in the sub-header template that in most cases is ACTUALLY related to that current page the user is on."
Rather than trying to tie together two unrelated pages/posts, it might be easier to use a custom field tool. My current favorite is Advanced Custom Fields.
With ACF, you can add supplemental fields (image, wysiwyg, file upload, 14 total field types) to your posts and pages, then easily pull the custom data into your template. It's very well documented and extremely easy to use.
So, I changed the way my sub-header.php template works. Basically some basic checking on what type of page/post was being set/called then dynamically pulled the relevant page info.
<?php
if (is_page()) :
$header_title = get_post_meta($post->ID, 'header_title', true);
$video_id = get_post_meta($post->ID, 'youtube_video_id', true);
$thumbnail = get_the_post_thumbnail($post->ID, 'post-thumbnail', array('class'=>'visual-element'));
$description = get_post_meta($post->ID, 'header_description', true);
elseif (is_home()) :
$page_id = 119; // News page
$page = get_page( $page_id );
$header_title = get_post_meta($page->ID, 'header_title', true);
$video_id = get_post_meta($page->ID, 'youtube_video_id', true);
$thumbnail = get_the_post_thumbnail($page->ID, 'post-thumbnail', array('class'=>'visual-element'));
$description = get_post_meta($page->ID, 'header_description', true);
elseif (is_archive()) :
$page_id = 18; // Client Uses page
$page = get_page( $page_id );
$header_title = get_post_meta($page->ID, 'header_title', true);
$video_id = get_post_meta($page->ID, 'youtube_video_id', true);
$thumbnail = get_the_post_thumbnail($page->ID, 'post-thumbnail', array('class'=>'visual-element'));
$description = get_post_meta($page->ID, 'header_description', true);
endif;
?>
<div id="subheader">
<h1><?php echo $header_title; ?></h1>
<?php if ( $video_id ) : ?>
<iframe class="visual-element" width="300" height="200" src="http://www.youtube.com/embed/<?php echo $video_id;?>?rel=0" frameborder="0" allowfullscreen></iframe>
<?php elseif ($thumbnail) : ?>
<?php echo $thumbnail; ?>
<?php endif; ?>
<?php echo $description; ?>
</div><!-- #subheader -->

Resources