I have custom post types for videos on my site. They are displayed on the pages in the form of cards. One post is a picture, a title for a video card, and a link. When you click on the card, a popup with a video should open. I display video cards using the WP_Query loop. My problem is that I don't know how to link to the video. The post does not have a single page. I need to somehow specify a link to it when creating a post and display it. How can i do this?
<?php $video_link = get_field('video_link'); ?>
<?php
$case = new WP_Query( array(
'post_type' => 'videos',
'paged' => -1,
'order' => 'DESC',
) );
while ( $case->have_posts() ) : $case->the_post(); ?>
<?php $cur_terms = get_the_terms( $case->post->ID, 'categories' ); ?>
<li project-preview project-preview--elastic" data-fancybox href="<?php echo $video_link ?>">
<span >
<img src="<? the_post_thumbnail_url() ?>" alt="<?php the_title(); ?>">
<span >
<svg width="17" height="19">
<use xlink:href="#triangle"></use>
</svg>
</span>
</span>
<span >
<span ><?php the_title(); ?></span>
<span >
<svg width="24" height="23">
<use xlink:href="#link-arrow2"></use>
</svg>
</span>
</span>
</a>
</li>
<?php endwhile;
$case->reset_postdata(); ?>
CodePudding user response:
I would probably simply add the link in the content of the custom post type, from what I see you are not using it. Or you could also add it as an excerpt.
Or like Cornel said, use the built in custom fields. More info here wordpress.org/support/article/custom-fields
Last solution is to use ACF plugin (Advanced Custom Fields) and just create a custom field for the link of the video.
Hope this helps
CodePudding user response:
The built-in custom fields method would be the quickest and simplest, but not the most elegant.
Here is a more elegant method of adding a custom metabox and a field.
// Hook a new metabox
add_action( 'add_meta_boxes', 'add_custom_box' );
function add_custom_box() {
$screens = [ 'videos' ];
foreach ( $screens as $screen ) {
add_meta_box(
'unique_box_identifier', // Unique ID
'Custom Meta Box Title', // Box title
'custom_box_html', // Content callback, this calls the markup function below
$screen // Post type
);
}
}
// Field Markup
function custom_box_html( $post ) {
$value = get_post_meta( $post->ID, 'video_link', true );
?>
<label for="video_link">Link URL: </label>
<input type="url" name="video_link" id="video_link" value="<?php if (isset($value)) { echo $value; } ?>" />
<?php
}
// Save the metadata with the post
add_action( 'save_post', 'save_postdata' );
function save_postdata( $post_id ) {
if ( array_key_exists( 'video_link', $_POST ) ) {
update_post_meta(
$post_id,
'video_link',
$_POST['video_link']
);
}
}
Then to get the value for use in your template, just call like this:
$value = get_post_meta( $post->ID, 'video_link', true );
echo $value;
Source Docs:
- https://developer.wordpress.org/plugins/metadata/custom-meta-boxes/
- https://developer.wordpress.org/reference/functions/get_post_meta/
CodePudding user response:
you'll have to use wp_get_attachment_url(get_post_meta( get_the_ID(), "key_of_video", true))
how do you store the video in the post type?