I have a wordpress website and there are some page like aboutpage, blogpage, contactpage etc,
so my concern is that i want data like title, paragraph from that specific page (blogpage)
so how do i get this for better understanding i add some reference image
enter image description here
enter image description here
how do i get title and content
Can you refer this https://developer.wordpress.org/reference/functions/get_post/
'42' is post id you can get all like title, content, ...
$post = get_post( 42 );
$output = apply_filters( 'the_content', $post->post_content );
print_r($post);
Related
How do I set an image that was uploaded via Advanced Custom Fields as the featured image of that woocommerce product only on the single product page?
This is how I set it up:
Step 1: I created an ACF image field with return format of 'Image Array'. Here's a screenshot of the settings.
ACF Image field settings
Step 2: I created conditional rules to only display the ACF input field on products for a specific category. That works. In the screen shot below you can see the field on the product page with an image uploaded to it.
ACF Image field on product page with uploaded image from media library
Step 3: Now this is the part I'm struggling with. I need to replace the woocommerce featured image with the uploaded ACF image when a user views the product page.
The screenshot below shows the product page with the woocommerce featured image but I want to replace that with the ACF image I uploaded in Step 2.
Product page on Front-end shows Woo featured image but I want to replace it with ACF image uploaded in Step 2
So I have the snippets plugin enabled where I can add code to swap the product images on the front-end (product page) but I need help with code please.
Any help will be appreciated, thanks!
I'm using OceanWP with WooCommerce.
In your ACF field set image to return ID or modify the function bellow if you need array or url value.
Add the following function to your active theme functions.php file.
function test($html, $post_thumbnail_id ) {
global $product;
$categories = $product->get_category_ids();
//Change 19 to the category id you need
if (in_array('19', $categories)):
//Change get_field to your field
$post_thumbnail_id = get_field('custom_thumb');
$html = wc_get_gallery_image_html( $post_thumbnail_id, true );
endif;
return $html;
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'test',10,2);
The answer from Martin worked great. Here's the final code if anyone needs it. It's modified to fallback onto the default woocommerce featured image if the ACF image field is empty.
function acf_wall_art_feature_image($html, $post_thumbnail_id ) {
global $product;
$categories = $product->get_category_ids();
//Get the category id from admin/products/categories/(category_name) URL
if (in_array('30', $categories)):
//Get_field for ACF field name
$post_thumbnail_id = get_field('wall_art_featured_product_image');
//Fallback to woo featured image if no ACF image is set
if (!$post_thumbnail_id):
return $html;
else:
$html = wc_get_gallery_image_html( $post_thumbnail_id, true );
endif;
endif;
return $html;
}
add_filter( 'woocommerce_single_product_image_thumbnail_html',
'acf_wall_art_feature_image',10,2);
I am interested to know how to add post permalink as TEXT to post excerpt before READ MORE button.
Ex. I have post with title "How to earn Money" with post permalink www.abc.com/how-to-earn-money.
Now issue is described below.
Left side featured image that is ok. Post title will be shown as "how to earn money". Post excerpt will be shown as blah blah blah with READ MORE button
Now I just want to add www.abc.com ( not full url) in the post excerpt before READ MORE button. Can u please tell how to do this?
I am not technical person to understand deeply, so I just want some piece of custom php code to add post permalink.
function replace_excerpt($content) {
return str_replace('[...]',
'<div class="more-link">Continue Reading</div>',
$content
);
}
add_filter('the_excerpt', 'replace_excerpt');
This is to replace excerpt text with url, but this is not working & I want to keep post excerpt as I explained.
You can use a filter to modify the excerpt, try to change your filter name to get_the_excerpt
https://codex.wordpress.org/Plugin_API/Filter_Reference/get_the_excerpt
function modify_excerpt( $excerpt ) {
global $post;
$url = get_post_meta( $post->ID, 'custom_url', true );
$excerpt .= str_replace( $excerpt, '[...]', '<br/>' . $url );
return $excerpt;
}
add_filter( 'get_the_excerpt', 'modify_excerpt' );
I have a related post code and I wants place it just after featured image on single post page. I have tried,
add_action( 'loop_start', 'wcr_related_posts', 10, 0 ) ;
but it doesn't work as I want to show content. It does show content at about proper place but the sidebar is not moving, just content is being shown a bit below and I also want the sidebar to come below due to code that wcr_related_posts generates.
I have not been able to find a hook that actually works that way I want it to.
You can do this using the 'the_content' filter:
add_filter( 'the_content', 'insert_featured_image', 20 );
function insert_featured_image( $content ) {
$content = preg_replace( "/<\/p>/", "</p>" . get_the_post_thumbnail($post->ID, 'post-single'), $content, 1 );
return $content;
}
Get the more details and examples as follow link : https://wordpress.stackexchange.com/questions/61272/how-do-i-add-the-featured-image-to-the-content-after-the-first-paragraph
I’m trying to set a sidebar in the single product page that show all products of the same categories as the product displayed.
That's how I proceed:
1) First I’ve created a sidebar called “Products_of_same_Category” to put in there a widget to show what I needed, then in function.php of my child theme I added the following snippet to execute php code in text widget:
// Enable PHP in widgets
add_filter('widget_text','execute_php',100);
function execute_php($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
2) Then when I saw that the snippet runs ok I added that code to test it:
<?php
$prod=get_the_term_list( $post->ID, 'product_cat');
echo $prod;
?>
And all worked fine, it gave me the exact name of the current category of the product displayed in the single product page.
3) So I've tried another test, deleting the previous code, to view if a shortcode translated in php should works in a widget too (writing at this time the exact category name requested, in this case "towels" - in the code below I substitute it with THE-CATEGORY-I-LIKE):
<?php echo do_shortcode('[product_category category=“THE-CATEGORY-I-LIKE” per_page="20" columns="1" orderby="title" order="desc"]'); ?>`
And all was again well done!
4) Finally I mixed all in this code to show the list of products of same categories but something goes wrong:
<?php $prod=get_the_term_list( $post->ID, 'product_cat', '', '', '' );
echo do_shortcode('[product_category category="'.$prod.'" per_page="20" columns="1" orderby="title" order="desc"]'); ?>
In last case the code doesn't display anything. I don't understand where I made mistakes, the syntax is wrong or the solving approach is illogical?
I really appreciate any help about this.
The problem is how you get the category slug. get_the_term_list will give you a formatted linked list of the categories, so it will display category names, not category slugs, which are different things. "Towels" would be your category name, but the category slug would be "towels". And product_category shortcode expect a slug, not a name.
The correct approach to get the category product slug is the following:
$terms = get_the_terms($post, 'product_cat');
if($terms && ! is_wp_error($terms)) {
foreach($terms as $term) {
echo do_shortcode('[product_category category="'.$term->slug.'" per_page="20" columns="1" orderby="title" order="desc"]');
}
}
This will display the products of all the categories associated to your product. See get_the_terms doc for reference.
In order to remove from the results the current product shown, you can make use of the woocommerce_shortcode_products_query filter. It isn't documented, but you can find it by looking at the product_category shortcode code located in includes/class-wc-shortcodes.php. In the product_category() method you will find the following line:
$return = self::product_loop( $query_args, $atts, 'product_cat' );
Where $query_args is a WP_Query parameters array. In the same class you will find the method product_loop() called here and see the following:
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $query_args, $atts ) );
So the query arguments are filtered - you will be able to work with that to add the desirated behavour. What you want to do is to use the post__not_in parameter to the query like this:
function remove_current_product_from_wc_shortcode($args, $atts) {
if(is_product()) { // check if we're on a single product page
$args['post__not_in'] = array(get_queried_object_id());
}
return $args;
}
add_filter('woocommerce_shortcode_products_query', 'remove_current_product_from_wc_shortcode');
This code should go in your theme functions.php - please not this is untested, so if it doesn't work look at the get_queried_object_id() return if it contain the current product ID.
I'm trying to populate an Advanced Custom Fields' radio field pulling out the data from a custom post type located in the main blog of a multisite install.
For a better understanding I made this simple flow graphic.
So I created a function in order to pull out the data from Main Blog and show as radio items on child site.
The function looks like this and I used this as reference
function getctas($field) {
$field['choices'] = array();
switch_to_blog(1);
$args = array(
'post_type' => 'location_icons',
'posts_per_page' => '-1',
);
$ctas = new WP_Query( $args );
while ( $ctas->have_posts()) {
$ctas->the_post();
$choices = get_field('icon',false);
$choices = explode("\n", $choices);
foreach( $choices as $choice ):
$field['choices'][ $choice ] = '<img src="'.$choice.'"/>';
endforeach;
}
restore_current_blog();
return $field;
}
add_filter('acf/load_field/name=call_to_action_icon', 'getctas');
I get the options listed correctly (options are images), I successfully pulled out the field icon from main blog and put as radio label and value.
The issue I'm having is that once the post is saved when I query it on the child's page template I get the correct images but the title of the post on blog 1 repeated. The ideal would be to have:
Image
Child Blog Post's Title
Child Blog Post's Desc
And what I'm, instead getting is:
Correct Image
CTP Title that contains the image on blog 1
No description
Correct Image
Same title as previous one
No description
And so on.
If any of you need more clarifications to help me solve this I'd be pleased to explain further.
Please declare global $switched; before switching to the main blog , it seems wp is not switching it back to the current blog if it does not work after declaring the global variable, Please try this
get current blog id before switching to main blog $current_site =get_current_blog_id();
once you are done. switch it back using
switch_to_blog( $current_site );
$GLOBALS['_wp_switched_stack'] = array();
$GLOBALS['switched'] = FALSE;