I'd like to include a file to the end of the main content (the_content) but using my code below it is being added before the content. Any ideas?
Thanks in advance
function add_listing_dashboard_content( $content ) {
global $page_id_dashboard;
$page_id_dashboard = get_field('dashboard_page', 'option');
if (is_page($page_id_dashboard) && is_user_logged_in() ) :
$content .include(plugin_dir_path(__FILE__) . '../templates/partials/dashboard-dashboard.php');
endif;
return $content;
}
add_action('the_content', 'add_listing_dashboard_content');
This will be the correct way of doing it:
function add_listing_dashboard_content( $content ) {
global $post;
ob_start();
include(plugin_dir_path(__FILE__) . '../templates/partials/dashboard-dashboard.php');
$append = ob_get_clean();
$page_id_dashboard = get_field('dashboard_page', 'option');
if (is_page($page_id_dashboard) && is_user_logged_in() ) :
$content.$append;
endif;
return $content;
}
add_action('the_content', 'add_listing_dashboard_content');
Check if it works.
NOTE: I do not know if your get_field is retrieving correctly a page_id, instead for testing purposes remove the if and check if field is being included, then work on the conditions.
Related
I have some fields (Wordpress) and sometimes one of them is empty. I want to display the content of 'the_excerpt' when the 'short_description' is not filled in.
here is what I came with:
if (empty(the_field('short_description'))) {
the_excerpt();
} else {
the_field('short_description');
}
Unfortunately it displays both short_description and the except after that. What is wrong here? Do I miss something? For me the code looks good.
To Check if value exists first use get_field() function instead of the_field()
Please have a look on example which shows how to check if a value exists before displaying it.
<?php if( get_field('short_description') ): ?>
<?php the_field('short_description'); ?>
<?php else: ?>
<?php the_excerpt(); ?>
<?php endif; ?>
Or you can user in another way like :
$isValue = get_field( "short_description" );
if( $isValue ) {
echo $isValue ;
} else {
the_excerpt();
}
WooCommerce: Show Custom Short Description When Empty
add_action( 'woocommerce_single_product_summary', 'bbloomer_echo_short_desc_if_empty',
21 );
function bbloomer_echo_short_desc_if_empty() {
global $post;
if ( empty ( $post->post_excerpt ) ) {
$post_excerpt = '<p class="default-short-desc">';
$post_excerpt .= 'This is the default, global, short description.<br>It will
show if <b>no short description has been entered!</b>';
$post_excerpt .= '</p>';
echo $post_excerpt;
}
}
This is a Right
I want to hide a certain paragraph from the SINGLE POST in WordPress via PHP content filter.
I developed this filtering script, which works like a sunshine:
function hideDaStuff( $content ) {
$paragraphs = explode( '</p>', $content );
foreach ($paragraphs as $index => $paragraph) {
if(6 == $index){
$paragraphs[$index] = '<p>Hidden for you ma friend.</p>';
}else{
$paragraphs[$index] .= '</p>';
}
}
return implode( '', $paragraphs );
}
add_filter( 'the_content', 'hideDaStuffForDaStranger' );
function hideDaStuffForDaStranger( $content ) {
if ( is_single() && ! is_user_logged_in() ) {
$content = hideDaStuff( $content );
}
return $content;
}
The problem is that WP cache caches the page whatever happens. So if a logged in user visits the page, then the content will show for everybody, and viceversa.
How can I make this specific part cache-independent, while keeping an efficient cache?
Using latest WP version and WP cache.
Thank you.
Put only single condition
function hideDaStuffForDaStranger( $content ) {
if (! is_user_logged_in() ) {
$content = hideDaStuff( $content );
}
return $content;
}
Actually no need of these filters. Use this format
if(is_user_logged_in()){ ?>
<p>oggedin data</p>
<?php }
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
the following code in my function.php can almost do the job nice and clean to all single pages. the problem is I want it to be filtered for a specific category ID:
function wp_add_to_content( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
if ( $GLOBALS['post']->ID == get_the_ID() ) {
$content .= 'Your new content here';
}
}
return $content;
}
add_filter('the_content', 'wp_add_to_content');
You don't need to use get_the_category and examine the results - Wordpress already has a function to check if your post is in a category: has_category. The advantage of this over get_the_category is that
you don't need the code to loop through all results to comparing the id (this is the part that is wrong in the other answer - it will only work if your post has one single category)
You can use not just the ID, but also the slug or name
You can check for multiple categories
The code you need is very simple, you just need to change one line!
function wp_add_to_content( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
/* pass an array with the IDs/names/slugs of the categories to check for, e.g. */
if ( has_category( array(12) ) )
$content .= 'Your new content here';
}
return $content;
}
add_filter('the_content', 'wp_add_to_content');
UPDATE:
Even though it wasn't in your question, if you actually want to include the content on all posts as well, you just need to do this:
function wp_add_to_content( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
$content .= 'Your new content here';
if ( has_category( array(12) ) )
$content .= '<br>Your new content here';
}
return $content;
}
add_filter('the_content', 'wp_add_to_content');
Modified your code idealy this condition is never making any sense because it always getting the id. $GLOBALS['post']->ID == get_the_ID()
function wp_add_to_content( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
if ( $GLOBALS['post']->ID == get_the_ID() ) {
$content .= 'Your new content here';
}
//getting the current post
global $post;
$category_detail=get_the_category( $post->ID );
if(!empty($category_detail))
{
$catId = $category_detail[0]->term_id;
// instead of 2 put your category id
if($catId == 2 && $catId != 0 )
{
$content .= 'Your new content here';
}
}
}
return $content;
}
add_filter('the_content', 'wp_add_to_content');
Note : above code is for only default post not for custom taxonomy for custom taxonomy
i want add a new field "title" on comment of wordpress, after insert the new input field in a default form of wordpress i added this in my function.php for save the title when new comment are submitted.
this is the code i use for save title:
function add_comment_meta_values($idcommento) {
global $post;
$idcommento= get_comment_ID();
$tipodipost= get_post_type($post->ID);
if( get_post_type($post->ID) == 'service') {
if(isset($_POST['title_svz']) ) {
$title= wp_filter_nohtml_kses($_POST['title_svz']);
add_comment_meta( $idcommento , 'title_svz', $title, false);
}}
}
add_action ('comment_post', 'add_comment_meta_values', 1);
this code work only when remove the condition :
if( get_post_type($post->ID) == 'service') {}
and i don't understand why, i have already tried this condition in comment.php or in a footer with simple function like this
function test_function() {
if( get_post_type($post->ID) == 'service') { echo 'done'; }
}
add_action( 'wp_footer', 'test_function' );
and it's work, so i don't understand why don't work in my primary code, any idea ?
SOLVED MYSELF
THIS IS THE NEW CODE:
function add_comment_meta_values($idcomment) {
$comment_full = get_comment( $idcomment );
$idpost = $comment_full->comment_post_ID;
$typepost= get_post_type($idpost);
if( $typepost == 'service') {
if(isset($_POST['title_svz']) ) {
$title= wp_filter_nohtml_kses($_POST['title_svz']);
add_comment_meta( $idcomment , 'title_svz', $title, false);
} }
}
add_action ('comment_post', 'add_comment_meta_values', 10, 1);
Sometimes in Wordpress, depending on the Context, the global $post may give you unexpected result. Thus something like $post->ID may not point to the appropriate ID you are looking for. You may as well try inspecting the $post object to see if it is what you had expected; like so:
<?php
function add_comment_meta_values($idcommento) {
global $post;
// TRY DUMPING THE $post VARIABLE TO SEE WHAT YOU GET
var_dump($post->ID);
var_dump($post->post_type);
var_dump($post);
exit;
$idcommento = get_comment_ID();
$tipodipost = get_post_type($post->ID);
// ALTHOUGH THIS IS ESSENTIALLY THE SAME AS WHAT YOU DID
if( $post->post_type == 'service') {
if(isset($_POST['title_svz']) ) {
$title= wp_filter_nohtml_kses($_POST['title_svz']);
add_comment_meta( $idcommento , 'title_svz', $title, false);
}}
}
add_action ('comment_post', 'add_comment_meta_values', 1);
After the Inspection, you'd sure know where and what was not OK in your Code...