on my wordpress page I have a gallery which displays featured images.
I would like to add HTML to a specific item, using the wordpress functions file.
If I run the following code:
add_filter( 'post_thumbnail_html', 'add_featured_image_html', 10, 1 );
function add_featured_image_html( $html ) {
return $html .= '<p>'.get_the_ID().' is the ID</p>';
}
My frontend does display the post ID's underneath my thumbnails. In this case, I would like to target Post_ID 19 (btw this is a custom post type).
If I extend my code to:
$id = get_the_ID();
if ($id == 19) {
add_filter( 'post_thumbnail_html', 'add_featured_image_html', 10, 1 );
function add_featured_image_html( $html ) {
return $html .= '<p>'.get_the_ID().' is the ID</p>';
}
}
Nothing is displayed.
Is there someone that could help me out? thanks a million!
The second parameter of this filter is the post id. https://developer.wordpress.org/reference/hooks/post_thumbnail_html/
This isn't tested yet, because I'm on my phone... But this should work.
add_filter( 'post_thumbnail_html', 'add_featured_image_html', 10, 2 );
function add_featured_image_html( $html , $id) {
if ($id == 19){
return $html .= '<p>'.get_the_ID().' is the ID</p>';}
else {return $html}
}
Related
I have a CPT called quotes with ACFs that I want to add to my RSS feed.
My rss url is example.com/feed/?post_type=quotes
When I use is_feed() this code works. But it doesn't work when I try to limit it to only the quotes CPT using: is_feed('quotes')
function add_fields_to_rss ($content) {
if(is_feed('quotes')) {
$post_id = get_the_ID();
$output = '<div><p>' . get_field('the_quote', $post_id) . '</p>';
$output .= '<h3 style="text-align: right;">' . get_field('quoted', $post_id) . '</h3>';
$output .= '</div>';
$content = $content.$output;
}
return $content;
}
add_filter('the_content','add_fields_to_rss');
Is there anything else I need to do so that this function will work with my quotes CPT only?
if I understand currently you want to add all custom posts types to the rss feed?
If so the following should work:
add_filter( 'request', 'myfeed_request' );
function myfeed_request( $qv ) {
if ( isset( $qv['feed'] ) ) {
// gett all custom posts types
$qv['post_type'] = get_post_types();
}
return $qv;
}
Tell me if this is what you're looking for
EDIT 1: Aswering your updated question and comment
The following is for adding specific customs posts types to the rss feed
<?php add_filter( 'request', 'multiple_CPT_to_rss' );
function multiple_CPT_to_rss( $qv ) {
if ( isset( $qv['feed'] ) && !isset( $qv['post_type'] ) ) {
$qv['post_type'] = array( 'post', 'my_CPT_1', 'my_CPT_2' );
}
return $qv;
}; ?>
CPT creates default rss links in the form of My rss url is example.com/feed/?post_type=my_post_type
If seems that doesn't work with is_feed('my_post_type')
I'm guessing to make it work you need to create the feed with add_feed()
But I didn't test that because I went another way.
I want to add a comment to individual products in the cart page. I am new to woocommerce wordpress plugin so I have no idea how to do.
I have done some research. In that tutorial, I found that I can use woocommerce_add_cart_item_data hook this way:
add_filter( 'woocommerce_add_cart_item_data', 'add_comment', 10, 3 );
function add_comment( $cart_item_data, $product_id, $variation_id ) {
$cart_item_data['comment'] = 'This is comment';
return $cart_item_data;
}
but this does not work in my case.
I attach the Cart page image so you can understand.
I hope you guys understand what I want?
Thank you.
Below code will add a custom text to an item in cart:
You need to create a custom field "comment" for the product to use it here.
add_filter( 'woocommerce_add_cart_item_data', 'add_comment', 10, 3 );
function add_comment( $cart_item_data, $product_id, $variation_id ) {
$cart_item_data['comment'] = 'This is comment';
return $cart_item_data;
}
Add a Custom text/comment before Cart table in Cart Page:
add_action( 'woocommerce_before_cart_table', 'add_comment' );
function add_comment()
{
echo '<div class="woocommerce-info">This is comment</div>';
}
Add a Custom text/comment after Cart table in Cart Page:
add_action( 'woocommerce_after_cart_table', 'add_comment' );
function add_comment() {
echo '<div class="woocommerce-info">This is comment</div>';
}
HOW TO ADD AN INPUT FIELD TO WOOCOMMERCE CART ITEMS & Let users update input fields in the cart
https://pluginrepublic.com/how-to-add-an-input-field-to-woocommerce-cart-items/
Refer this link for a working solution to similar request:
https://stackoverflow.com/a/21818028/12291365
If you are okay with using the plugin, then this plugin will do the trick:
https://wordpress.org/plugins/wc-fields-factory/
Use code snippet below for adding custom values for each cart item.
Please add your logic for attaching values for each item
// Add custom value into cart item
add_filter('woocommerce_add_cart_item_data','sub_add_item_data',1,2);
if(!function_exists('sub_add_item_data'))
{
function sub_add_item_data($cart_item_data,$product_id)
{
// use condition for adding custom value here
$new_value = array(
'sub_custom_value' => 'custom value',
);
return array_merge($cart_item_data,$new_value);
}
}
// Extract custom values
add_filter('woocommerce_get_cart_item_from_session', 'sub_get_cart_items_from_session', 1, 3 );
if(!function_exists('sub_get_cart_items_from_session'))
{
function sub_get_cart_items_from_session($item,$values,$key)
{
if (array_key_exists( 'sub_custom_value', $values ) )
{
$item['sub_custom_value'] = $values['sub_custom_value'];
}
return $item;
}
}
// display in cart and checkout
add_filter('woocommerce_checkout_cart_item_quantity','sub_add_user_custom_option_from_session_into_cart',1,3);
add_filter('woocommerce_cart_item_price','sub_add_user_custom_option_from_session_into_cart',1,3);
if(!function_exists('sub_add_user_custom_option_from_session_into_cart'))
{
function sub_add_user_custom_option_from_session_into_cart($product_name, $values, $cart_item_key )
{
if(isset( $values['sub_custom_value'] ) && '' != $values['sub_custom_value'] )
{
$return_string = $product_name . "</a><dl class='variation'>";
$return_string .= "<table class='sub_options_table' id='" . $values['product_id'] . "'>";
$return_string .= "<tr><td>" . $values['sub_custom_value'] . "</td></tr>";
$return_string .= "</table></dl>";
return $return_string;
}
else
{
return $product_name;
}
}
}
Is it possible to use a post meta value instead of the title as permalink?
I tried different methods, but i did'nt work as i would like
As for now the structure is: www.website../property/post-title
i achived this up to now.. www.website../property/post-meta/post-title
what i would like is: www.website../property/post-meta
my code is:
function property_query_vars( $qv ) {
$qv[] = 'meta';
return $qv;
}
add_filter( 'query_vars', 'property_query_vars' );
add_filter( 'post_type_link', 'property_post_type_link2', 10, 3);
function property_post_type_link2($permalink, $post, $leavename) {
if ($post->post_type == 'property') {
$meta = get_post_meta($post->ID, '_boP_prop-id', true);
$postname = $post->post_name;
if (isset($meta) && !empty($meta))
$permalink = home_url( " property/" . $meta . "/");
}
return $permalink;
}
function mycustomname_rewrites_init2(){
add_rewrite_rule('property/([^/]*)/?$',
'index.php?post_type=property&meta=$matches[1]',
'top'
);
}
add_action('init', 'mycustomname_rewrites_init2');
this shows the permalink as i wanted but, only the archive page is shown, click on the permalink returns me to archive page.
I have a wordpress website with customs posts and metas boxes.
One of these manage some pictures and I want to force the title of the post.
It should be the same as the name of the media uploaded.
Is it possible ? And how ?
Thank's
Manipulate the post title with a filter.
function change_post_title( $title, $id = null ) {
if ( is_single() ) {
$title = 'new title';
}
return $title;
}
add_filter( 'the_title', 'change_post_title', 10, 2 );
Thanks all for your help.
I have used a filter but this one :
function modify_post_title($data){
if($data['post_type'] == 'fcfm_photos' && isset($_POST['image'])) {
$data['post_title'] = substr($_POST['image'], -(strpos(strrev($_POST['image']),'/')));
}
return $data;
}
add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 1 );
And when I save the post, the post title change automatically. This is what i want.
This is a self Q&A.
How do you modify the text/html that appears in the output of a wp_nav_menu? For example, I wanted to add the featured image for pages and categories.
You see examples of doing this with a custom walker, but the code is very complex to do for small changes. Surely there is a way to do it with a filter?
This is the code I came up with thanks to some help from a Wordpress StackOverflow answer that I can't find anymore (please comment with a link if you find it).
First you need to add the filter to the specific menu (you could add it to all menus if you want - just use the add_filter line by itself).
// Add filter to specific menus
add_filter('wp_nav_menu_args', 'add_filter_to_menus');
function add_filter_to_menus($args) {
// You can test agasint things like $args['menu'], $args['menu_id'] or $args['theme_location']
if( $args['theme_location'] == 'header_menu') {
add_filter( 'wp_setup_nav_menu_item', 'filter_menu_items' );
}
return $args;
}
Then you need to build out the code to get the post or category ID from the $item object passed to the filter. It's not as easy as you'd expect, as $item doesn't contain the underlying post/category ID, just the menu item ID. So I use the URL's to do a reverse lookup of the IDs.
This won't work for tags used in a menu, or custom taxonomys. I only needed it for categories, so this is all I built.
// Filter menu
function filter_menu_items($item) {
if( $item->type == 'taxonomy') {
// For category menu items
$cat_base = get_option('category_base');
if( empty($cat_base) ) {
$cat_base = 'category';
}
// Get the path to the category (excluding the home and category base parts of the URL)
$cat_path = str_replace(home_url().'/'.$cat_base, '', $item->url);
// Get category and image ID
$cat = get_category_by_path($cat_path, true);
$thumb_id = get_term_meta($cat->term_id, '_term_image_id', true); // I'm using the 'Simple Term Meta' plugin to store an attachment ID as the featured image
} else {
// Get post and image ID
$post_id = url_to_postid( $item->url );
$thumb_id = get_post_thumbnail_id( $post_id );
}
if( !empty($thumb_id) ) {
// Make the title just be the featured image.
$item->title = wp_get_attachment_image( $thumb_id, 'poster');
}
return $item;
}
And then you want to remove the filter that you applied at the beginning, so that the next menu processed doesn't use the same HTML as defined above in filter_menu_items().
// Remove filters
add_filter('wp_nav_menu_items','remove_filter_from_menus', 10, 2);
function remove_filter_from_menus( $nav, $args ) {
remove_filter( 'wp_setup_nav_menu_item', 'filter_menu_items' );
return $nav;
}
Modified Drew Baker answer. It works without plugins, also if there is no category with current slug it checks for woocommerce product category ('product_cat').
functions.php
// Add filter to specific menus
add_filter('wp_nav_menu_args', 'add_filter_to_menus');
function add_filter_to_menus($args) {
// You can test agasint things like $args['menu'], $args['menu_id'] or $args['theme_location']
if( $args['theme_location'] == 'menu-header') {
add_filter( 'wp_setup_nav_menu_item', 'filter_menu_items' );
}
return $args;
}
// Filter menu
function filter_menu_items($item) {
if( $item->type == 'taxonomy') {
// Get category and image ID
$slug = pathinfo( $item->url, PATHINFO_BASENAME );
$cat = get_term_by( 'slug', $slug, 'category' );
// If there is no standard category try getting product category
if( !$cat ) {
$cat = get_term_by( 'slug', $slug, 'product_cat' );
}
$thumb_id = get_term_meta($cat->term_id, 'thumbnail_id', true);
} else {
// Get post and image ID
$post_id = url_to_postid( $item->url );
$thumb_id = get_post_thumbnail_id( $post_id );
}
if( !empty($thumb_id) ) {
// Make the title just be the featured image.
$item->title = wp_get_attachment_image( $thumb_id, 'poster');
// Display image + title example
// $item->title = wp_get_attachment_image( $thumb_id, 'poster').$item->title;
}
return $item;
}
// Remove filters
add_filter('wp_nav_menu_items','remove_filter_from_menus', 10, 2);
function remove_filter_from_menus( $nav, $args ) {
remove_filter( 'wp_setup_nav_menu_item', 'filter_menu_items' );
return $nav;
}