I want to show google ad on my wordpress attachment page which is child page to post on wordpress. I just want to show this ad only child post/page not on parent post. i want to show in the end of content so i tried this code
function wpdev_before_after($content) {
global $post;
if ($post->post_parent)
$aftercontent = 'Ad code here';
$fullcontent =$content . $aftercontent;
return $fullcontent;
}
add_filter('the_content','wpdev_before_after');
Above code show ad on both pages parent and child, I also tried this code in this way
function wpdev_before_after($content) {
global $post;
if ($post->post_parent) {
$aftercontent = 'code here';
$fullcontent =$content . $aftercontent;
}
return $fullcontent;
}
add_filter('the_content','wpdev_before_after');
If i use this code on parent page/post content not show . please help me, i hope i you understand what i want to say.
try this:
function wpdev_before_after($content) {
global $post;
if(is_singular() && $post->post_parent > 0) {
$aftercontent = 'code here';
$fullcontent =$content . $aftercontent;
return $fullcontent;
}
else
return $content;
}
add_filter('the_content','wpdev_before_after', 9999);
Thanks to #Tarun Mahashwari for help me to get working code.
Here is working code for posts and child posts.
function wpdev_before_after($content) { global $post; if ($post->post_parent) { $aftercontent = 'code here'; $fullcontent =$content . $aftercontent; return $fullcontent; } else return $content; } add_filter('the_content','wpdev_before_after');
May be this code work for pages and child pages provided by #Tarun Mahashwari
function wpdev_before_after($content) {
global $post;
if(is_page() && $post->post_parent > 0) {
$aftercontent = 'code here';
$fullcontent =$content . $aftercontent;
return $fullcontent;
}
else
return $content;
}
add_filter('the_content','wpdev_before_after', 9999);
Related
I'm learning PHP and WordPress development, so I assumed that maybe here I'll find the answer or a tip.
I've restriced the_content based on user role. After the end of the_content I'd like to display button whitch is unique to the specific post. So here is the code which display that:
function displaycontent($content) {
if(is_singular( 'custom_post_type' )) {
$aftercontent = 'I Want To Add Code Here';
$fullcontent = $content . $aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'displaycontent');
And I'd like to insert code below into the underlined place above:
<?php
$post = $wp_query->post;
$group_id = get_field( 'link_number' , $post->ID );
if( $group_id ) {
echo do_shortcode( '[checkout_button class="button" level="' . $group_id . '" text="Order"]' );
}
?>
How can I do that?
It's probably better to create a custom shortcode for this. If you change how the_content works, it will be global, everywhere.
Note:
This code is completely untested and put together after 5 min of Googling, so if somehting is wrong, feel free to comment and I'll amend it. It should be fairly close and is mostly for explaining the concept instead of a pure copy/paste solution
Register a new shortcode:
add_shortcode('my_awesome_content', 'my_awesome_content_func');
Create the callback function:
Here we've added $atts which will contain our attribute (the post id):
function my_awesome_content_func($atts = [])
{
$postId = $atts['postid'] ?? null;
if ($postId === null) {
// We got no id so let's bail
return null;
}
$post = get_post($postId);
if (!$post) {
// We didn't find any post with the id so again, let's bail
return null;
}
$group_id = get_field( 'link_number' , $post->ID );
$content = $post->content;
if( $group_id ) {
$content .= do_shortcode( '[checkout_button class="button" level="' . $group_id . '" text="Order"]' );
}
return $content;
}
Usage:
Now you should be able to call it like this:
echo do_shortcode('[my_awesome_content postid="' . $post->ID . '"]');
You can just embed you code below in the above filter with some modifications:
function displaycontent($content) {
if (is_singular('custom_post_type')) {
$post_id = get_queried_object_id();
$group_id = get_field('link_number', $post_id);
$aftercontent = '';
if ($group_id)
$aftercontent = do_shortcode('[checkout_button class="button" level="'.$group_id. '" text="Order"]');
$fullcontent = $content.$aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'displaycontent');
OK, thanks everyone, I Got it! Here is the code if anyone would have same problem:
function displaycontent($content) {
if(is_singular( 'custom_post_type' )) {
$group_id = get_field('link_number');
$aftercontent = do_shortcode( '[checkout_button class="button" level="' . $group_id . '" text="Order"]' );
$fullcontent = $beforecontent . $content . $aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'displaycontent');
I was trying to make my metabox work but for some reason whenever I put some text and try to save the data inside the textarea it wont save.
add_action("admin_init", "custom_product_metabox");
function custom_product_metabox(){
add_meta_box("custom_product_metabox_01", "Product Description", "custom_product_metabox_field", "portfolio_page", "normal", "low");
}
function custom_product_metabox_field(){
global $page;
$data = get_post_custom($page->ID);
$val = isset($data['custom_product_input']) ? esc_attr($data['custom_product_input'][0]) : 'no value';
echo '<textarea rows="5" cols="220" name="custom_product_input" id="custom_product_input" value="'.$val.'"></textarea>';
}
add_action("save_post", "save_detail");
function save_detail(){
global $page;
if(define('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return $page->ID;
}
update_post_meta($page->ID, "custom_product_input", $_POST["custom_product_input"]);
}
This is actually a code for a portfolio page where I embedded inside the functions.php. Any idea how can I make it work and save the data?
Thanks!
Your save method looks wrong. Try something like
function custom_product_metabox_field($post){
//global $page; remove this line also
$data = get_post_custom($post->ID);
$val = !empty(get_post_meta( $post->ID, 'custom_product_input', true )) ?
get_post_meta( $post->ID, 'custom_product_input', true ) : 'no value';
echo '<textarea rows="5" cols="220" name="custom_product_input" id="custom_product_input" value="'.$val.'"></textarea>';
}
add_action("save_post", "save_detail", 10, 3 );
function save_detail($post_id, $post, $update){
//global $page;// remove this line
if(define('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return $post_id;
}
update_post_meta($post_id, "custom_product_input", $_POST["custom_product_input"]);
}
I have a custom meta field that I would like to insert in the_content automatically so that my AMP plugin can render the custom field value in the same way as the_content.
Currently I am using this code to display it:
<?php $video_value = get_post_meta( get_the_ID(), '_video', true ); ?>
<?php if ( ! empty( $video_value ) ) {?>
<div class="video-container"><?php echo $video_value; ?></div>
<?php } else { ?>
<?php the_post_thumbnail(); ?>
<?php } ?>
But I would like the $video_value to be inserted automatically before the_content.
You can use the the_content filter to do this. You can read more about it on the WordPress developer site.
But the code could be looking something like this:
function my_custom_content_filter($content){
global $post;
$video_value = get_post_meta($post->ID, '_video', true);
if($video_value){
return '<div class="video-container">' . $video_value . '</div>' . $content;
}else{
return get_the_post_thumbnail($post->ID) . $content;
}
}
add_filter('the_content', 'my_custom_content_filter');
And you can add this code in you functions.php file.
Note This filter only works on the_content() and not get_the_content()
You can do something like this -
and add the conditions that you want -
You might need to access Global $post to get the meta value
function custom_weird_name_before_after($content) {
if(is_page() || is_single() || $yourOwnConditions == true) {
$beforecontent = 'This line will go before the content - populate with whatever.';
$aftercontent = 'This will come after the content - makes sense right?';
$fullcontent = $beforecontent . $content . $aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'custom_weird_name_before_after');
You can add this in functions.php
This is my first try at php.
Trying to create a custom shortcode in wordpress to call custom metadata and put it inside a page content. It worked fine without the
function wpsl_staff() {
echo '<div class="staff">' .wpsl_get_staff(). '</div>';
function wpsl_get_staff() {
and with the shortcode being
add_shortcode( 'wpsl_staff', 'wpsl_get_staff' );
The output was correct but the output appeared at the top of the page above all other content and when I inspected in the browser it had no div or class id and just appeared as text. How do I wrap this in a div and give id so it sits properly on the content page (It should display between 2 images and under another shortcode) I have managed to give it a class id previously and get it to appear in a div but the output still appears at the top of the page above the images and other shortcode.
<?php
function wpsl_get_staff() {
global $post;
$queried_object = get_queried_object();{
$Manager = get_post_meta( $queried_object->ID, 'wpsl_Manager', true );
if (!empty( $Manager )) return 'Manager : ' .$Manager . "<br>";
else {}
$Assitant = get_post_meta( $queried_object->ID, 'wpsl_Assistant_Manager', true );
if (!empty( $Assistant )) return 'Assistant Manager : ' .$assistant . "<br>";
else {}
$Agent = get_post_meta( $queried_object->ID, 'wpsl_Agent', true );
if (!empty( $Agent )) return 'Agent : ' .$Agent . "<br>";
else {}
}
}
function wpsl_staff() {
echo '<div class="staff">' .wpsl_get_staff(). '</div>';
}
add_shortcode( 'wpsl_staff', 'wpsl_dio_staff' );
Thanks
Toca
Shortcode functions are supposed to return the result, not directly output it.
function wpsl_staff() {
return '<div class="staff">' .wpsl_get_staff(). '</div>';
}
Edit: You can only return one value from a function - so if your wpsl_get_staff is supposed to return data for all three types, then you need to assemble that data into a string variable first, and then return that at the end of the function - something like this:
function wpsl_get_staff() {
global $post;
$queried_object = get_queried_object();
$output = '';
$Manager = get_post_meta( $queried_object->ID, 'wpsl_Manager', true );
if (!empty( $Manager )) {
$output .= 'Manager : ' .$Manager . "<br>";
}
$Assitant = get_post_meta( $queried_object->ID, 'wpsl_Assistant_Manager', true );
if (!empty( $Assistant )) {
$output .= 'Assistant Manager : ' .$assistant . "<br>";
}
$Agent = get_post_meta( $queried_object->ID, 'wpsl_Agent', true );
if (!empty( $Agent )) {
$output .= 'Agent : ' .$Agent . "<br>";
}
return $output;
}
function awepop_show_views($singular = "view", $plural = "views", $before = "This post has: ")
{
global $post;
$current_views = awepop_get_view_count();
$views_text = $before . $current_views . " ";
if ($current_views == 1) {
$views_text .= $singular;
}
else {
$views_text .= $plural;
}
return $views_text;
}
function awepop_append_to_meta($meta){
return $meta[] = awepop_show_views();
}
add_filter( 'the_meta_key', 'awepop_append_to_meta' );
I am trying to embed post views into post meta. I search alot but couldn't find the appropriate filter for post meta. please advice how can i embed my view count into post meta
Try hooking to init and using update_post_meta. For example, this is how you could increment your post views:
function my_update_postmeta() {
global $post;
update_post_meta( $post->ID, my_meta_key, $my_meta_value + 1 );
}
add_action( 'init', 'my_update_postmeta' );
Note: You would put my_update_postmeta() in your page or post template.
Ref: http://codex.wordpress.org/Function_Reference/update_post_meta