Featured custom post type slider - php

I have a custom-post-type and I want to display a featured slider in my homepage with some of those custom posts.
I want to create a metabox to select if it's a featured post or not and only then display it.
This is the code that I have, which is not working as to add a metabox.
<?php function sm_custom_meta() {
add_meta_box( 'sm_meta', __( 'Featured Posts', 'sm-textdomain' ), 'sm_meta_callback', 'post' );
}
function sm_meta_callback( $post ) {
$featured = get_post_meta( $post->ID );
?>
<p>
<div class="sm-row-content">
<label for="meta-checkbox">
<input type="checkbox" name="meta-checkbox" id="meta-checkbox" value="yes" <?php if ( isset ( $featured['meta-checkbox'] ) ) checked( $featured['meta-checkbox'][0], 'yes' ); ?> />
<?php _e( 'Featured this post', 'sm-textdomain' )?>
</label>
</div>
</p>
<?php
}
add_action( 'add_meta_boxes', 'sm_custom_meta' );
?>
I'm following the steps in this post and it's not working http://smallenvelop.com/how-to-create-featured-posts-in-wordpress/
I don't want to use any plugins.
I've changed the sm-textdomain to my theme name, and the 'post' to my slug-post-type.
Can you figure out what I'm doing wrong and help out?
Thanks.

check if you have other arrays or queries in the page, and since you mentioned it's a CPT, check if the query is looking in the post_type=>slug

Related

How to create a custom post type for a video with a link

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 class="portfolio-section__item __js_masonry-item>
<a class="project-preview project-preview--elastic" data-fancybox href="<?php echo $video_link ?>">
<span class="project-preview__image">
<img src="<? the_post_thumbnail_url() ?>" alt="<?php the_title(); ?>">
<span class="hover-button">
<svg width="17" height="19">
<use xlink:href="#triangle"></use>
</svg>
</span>
</span>
<span class="project-preview__bottom">
<span class="project-preview__title"><?php the_title(); ?></span>
<span class="project-preview__icon">
<svg width="24" height="23">
<use xlink:href="#link-arrow2"></use>
</svg>
</span>
</span>
</a>
</li>
<?php endwhile;
$case->reset_postdata(); ?>
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/
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

WordPress/WooCommerce - What is the hook for custom category bulk edit?

I have added bulk action in the WooCommerce product category edit. I just want to know what the hook is when I click on Apply to do some actions on selected categories. (Specifically, I want to update all selected categories by updating the wp_termmeta table and set the example value to 1 or 0) The only thing that I want is the name of the hook when clicking the Apply button in /wp-admin/edit-tags.php?taxonomy=product_cat&post_type=product.
Just copy & paste into your functions.php. Enjoy
// Note: change all occurrences of "custom_field" with the key of your custom field add_action( 'woocommerce_product_bulk_edit_start', 'bbloomer_custom_field_bulk_edit_input' ); function bbloomer_custom_field_bulk_edit_input() { ?> <div class="inline-edit-group"> <label class="alignleft"> <span class="title"><?php _e( 'Custom Field', 'woocommerce' ); ?></span> <span class="input-text-wrap"> <input type="text" name="custom_field" class="text" value=""> </span> </label> </div> <?php } add_action( 'woocommerce_product_bulk_edit_save', 'bbloomer_custom_field_bulk_edit_save' ); function bbloomer_custom_field_bulk_edit_save( $product ) { $post_id = $product->get_id(); if ( isset( $_REQUEST['custom_field'] ) ) { $custom_field = $_REQUEST['custom_field']; update_post_meta( $post_id, 'custom_field', wc_clean( $custom_field ) ); } }

Wordpress PHP: Combining posts with custom post types on category archive

I have a site with Wordpress posts and a custom post type (molinks). I have a post loop (plugin driven) that displays both posts and molinks in the same way. The problem I am trying to fix is that when the user clicks on the category tags, to see the category archive page, only the posts with that category show up (not the molinks).
To fix this I have used the following code, however it only works if there is a post with the category. i.e. if a post and a molink have category A then both will show on the category A archive page. But if only the molink has category A (and not a post) then the molink with the category does not show. In this case the template 'none' is called (line 2nd from bottom).
Any ideas? Thanks
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
$args = array(
'post_type' => array('molink', 'post'),
'category__in' => get_queried_object_id(),
'showposts' => 100
);
$custom_posts = new WP_Query( $args );
if ( $custom_posts->have_posts() ):
while ( $custom_posts->have_posts() ) : $custom_posts->the_post();?><br />
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><br />
<?php the_title(); ?></a>, <br />
<?php the_time('d M Y'); ?><br />
<?php endwhile; else: ?><br />
<p><?php _e('No posts.'); ?></p><br />
<br />
<?php endif; ?> <br />
<?php
endwhile;
?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
The main problem you are having is that you are executing the second $customer_posts query only if the original WordPress query (that only checks the regular post_type) has results.
So what happens is it will be never called when you are only having molink in your category A.
The best way around it, in my opinion, is to use the pre_get_posts WordPress hook, which you can find more details about Here.
Fires after the query variable object is created, but before the actual query is run.
You should create a code that would look something like the following example:
// Somewhere around functions.php
function add_molinks_to_post_archives ( $query ) {
// Only apply when is main category, inside inside a category page
if ( is_category() && $query->is_main_query() ) {
$query->set( 'post_type', array( 'post', 'molink' ) );
}
}
add_action ( 'pre_get_posts', 'add_molinks_to_post_archives' );
And then, you can just skip the custom code inside the loop itself.

Show Custom Field From Custom Post Type In Template

Been learning how to add custom post types and custom fields manually without plugins by pulling pieces of code from here and there and adding them into functions.php. Here is how I added the custom field to custom post type
function add_ads_meta_boxes() {
add_meta_box("ads_contact_meta", "URL", "add_custom_ads_meta_box", "ads", "normal", "low");
}
function add_custom_ads_meta_box()
{
global $post;
$custom = get_post_custom( $post->ID );
?>
<style>.width99 {width:99%;}</style>
<p>
<input type="text" name="link-url" value="<?= #$custom["link-url"][0] ?>" class="width99" />
</p>
<?php
}
function save_ads_custom_fields(){
global $post;
if ( $post )
{
update_post_meta($post->ID, "link-url", #$_POST["link-url"]);
}
}
add_action( 'admin_init', 'add_ads_meta_boxes' );
add_action( 'save_post', 'save_ads_custom_fields' );
I can see the field in the post type, and it saves the added info. Now, here is how I'm adding it to the site
<div class="customadwrap">
<?php $args = array( 'post_type' => 'ads');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<a href="<?php echo get_post_meta($post->ID, 'link-url', true); ?>">
<?php the_title(); ?>
</a>
<?php
endwhile;
?>
</div>
Title shows up no problem, but the url entered is not. How do I make the fref to work?
In your second bit of code, replace $post->ID with get_the_ID().

Woocommerce different T&C page for different products

Is it possible to add different terms and condition pages for different products at woocommerce checkout ?
If a order has some specific product, I need to show different T&C page.
In check out we show T&C page link like image
From the admin can't do that so I'm going to override the plugin payment.php file in my template and do the hack for below part
<?php if ( wc_get_page_id( 'terms' ) > 0 && apply_filters( 'woocommerce_checkout_show_terms', true ) ) : ?>
<p class="form-row terms">
//If condition here
<label for="terms" class="checkbox"><?php printf( __( 'I’ve read and accept the terms & conditions', 'woocommerce' ), esc_url( wc_get_page_permalink( 'terms' ) ) ); ?></label>
<input type="checkbox" class="input-checkbox" name="terms" <?php checked( apply_filters( 'woocommerce_terms_is_checked_default', isset( $_POST['terms'] ) ), true ); ?> id="terms" />
</p>
<?php endif; ?>
Is there any hook for do this in functions.php file ?
Use the following code in functions.php
add_filter('woocommerce_get_terms_page_permalink', 'replace_permalink');
function replace_permalink($permalink){
// do any change to $permalink here depending on your condition or business case
return $permalink;
}
Hope this helps

Categories