Wordpress: function to replace content for custom post status - wordpress

I created custom post status "blocked" via register_post_status (like this register_post_status not showing post_status in status dropdown).
Now I want to replace content on posts with this status via functions.php.
I tried something like this
function block_by_status ($post_object) {
global $wp_query;
if( 'blocked' == $wp_query->post->post_status ) {
return 'This post is blocked';
}
}
add_action('the_post','block_by_status');
But it doesn't work, please somebody help?

So for the content you can do something as simple as:
add_filter('the_content', 'block_by_status');
and to hide the title you can use:
add_filter( 'the_title', 'block_by_status', 10, 2 );
If you want to go further than that, I suggest editing the template in the theme folder.
For pages you need to edit page.php and for posts you need to edit posts.php or singular.php, depends on the theme you are using.
So you can do something like this:
In the functions.php file:
function block_by_status ($post_object) {
global $wp_query;
if( 'blocked' == $wp_query->post->post_status ) {
return true;
}
}
and then in the files I referenced above (Depends on your theme):
<?php if (!block_by_status()) { ?>
<?php while (have_posts()) : the_post(); ?>
<h1><?php the_title() ?></h1>
<?php the_content() ?>
<?php get_sidebar() ?>
<?php endwhile; ?>
<?php } else {
echo 'This post is blocked';
} ?>
So you use your function as a boolean and just hide what you dont want users to see.

Related

How To Add WORDPRESS SHOW CONTENT OF STATIC POSTS PAGE ABOVE POSTS LIST

is there any way to add content to my WordPress static blog page.
i tried to install plugin it not working.
Some Other Sources
Method 1: Paste the below code in function.php
// Add content of page called “blog” to the page that contains the list of blog posts
add_action ( ‘__before_loop’, ‘add_blog_page_content_before_post_list’);
function add_blog_page_content_before_post_list() {
if ( is_home() ) {
$post = get_page_by_path( ‘/blog’ );
echo wpautop($post->post_content);
}
}
Source link – https://presscustomizr.com/snippet/enter-text-posts-static-blog-page/
Method 2:
Assuming you’ve set a custom page for the posts in the WordPress backend (Settings > Reading), you just need to add a few lines of code to your index.php file in your theme. Like so:
//grab the id of the page set in the backend
$posts_page_id = get_option(‘page_for_posts’);
//grab the post object related to that id
$posts_page = get_post($posts_page_id);
//display the content if any
if( $posts_page->post_content ){
echo wpautop( $posts_page->post_content ); //uses wpautop to automatically add paragraphs
}
Source Link – Adding static content to Wordpress posts page?
Method 3:
global $post;
$page_for_posts_id = get_option('page_for_posts');
if ( $page_for_posts_id ) :
$post = get_page($page_for_posts_id);
setup_postdata($post);
?>
<div id="post-<?php the_ID(); ?>">
<header>
<h1><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div>
<?php the_content(); ?>
<?php edit_post_link('Edit', '', '', $page_for_posts_id); ?>
</div>
</div>
<?php
rewind_posts();
endif;
Source link: https://www.thatweblook.co.uk/tutorial-wordpress-show-content-of-static-posts-page-above-posts-list/?unapproved=11768&moderation-hash=f5c2e41fcea7e044a9e46798f94703dd#comment-11768

ACF fields on WooCommerce Product not displaying

I am customizing an old theme called AmazeTheme which was not specifically made with WooCommerce support. And an woocommerce.php was at theme's root. However with help of plugins like Simply Show Hooks and Show current template I am able to see where to put which info.
But I am having an weird problem. The ACF fields I have added are not visible at all. I even tried to assign the field group inside product loop section.
I also tried the following method.
add_action( 'woocommerce_after_single_product_summary', 'view_acf_field_for_single_product', 10 );
function view_acf_field_for_single_product(){
if (function_exists('the_field')){
the_field('shop_support');
}
}
And inside the loop of single-product.php
<?php while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'single-product' );
$dump = get_fields();
?>
<?php endwhile; // end of the loop. ?>
And then did var_dump($dump) at desired place of the file.
This site's php version is 5.6.40 and WooCommerce version is 3.4.8
WP version is 4.9.18
I have looked up many solutions. Also tried the WooCommerce Hooks but still no clue why ACF not showing.
Can you try this code:
add_action( 'woocommerce_after_single_product_summary', 'view_acf_field_for_single_product', 10 );
function view_acf_field_for_single_product(){
global $post;
if (function_exists('the_field')){
the_field('shop_support', $post->ID);
}
}
Not tested.
For some weird reasons, this approach worked for me (path/to/theme/woocommerce/single-product.php):
<?php if (get_field_objects()):
foreach ( get_field_objects() as $field_id => $field ) :
$value = trim($field['value']);
if (!empty($value)) :
?>
<div class="product_field" id="field-<?php echo $field_id; ?>">
<?php the_field($field_id); ?>
</div>
<?php endif; ?>
<?php endforeach; // end of the loop. ?>
<?php endif; ?>
Also the following one for Category (path/to/theme/woocommerce/archive-product.php) pages:
<?php
$extra_info = get_field_object( 'category_details', get_queried_object() );
if ( !empty($extra_info) ) :
?>
<div class="category_field" id="field-<?php echo $extra_info['key']; ?>">
<?php echo $extra_info['value']; ?>
</div>
<?php endif; ?>
Even at these locations, get_fields() did not work.

How to pass PHP template through shortcode

I want to create an overview of my latest blog posts and display this information on a certain part of my homepage. My WordPress theme already has a PHP template in the desired format, which means I would not have to create the actual design/style myself.
As I want this information to be shown in one particular part, I was thinking about creating a shortcode which inserts the information then. I have created shortcodes in WP before, however they always populated simple information.
I am running into the problem now that I do not quite know how I can populate a PHP template via a shortcdode. Before the shortcode function was like similar to this one:
function display_my_shortcode($atts, $content, $tag){
//if called from the 'my_primary_shortcode' shortcode
if($tag == 'my_primary_shortcode'){
return 'This is the primary shortcode';
}
//if called from the 'my_secondary_shortcode' shortcode
else if($tag == 'my_secondary_shortcode'){
return 'This is the secondary shortcode';
}
//default
else{
return 'This is something else';
}
}
add_shortcode('my_primary_shortcode','display_my_shortcode');
add_shortcode('my_secondary_shortcode','display_my_shortcode');
My problem is, that the output of the template is still with php - the template I want to export would look like this:
<div class="blog_holder blog_small_image">
<?php $counter = 0; ?>
<?php if(have_posts()) : while ( have_posts() ) : the_post(); ?>
<?php
if(get_the_category(get_the_ID()) != 'test123')
{
continue;
}
else
{
if($counter < 6){
get_template_part('blog_small_image', 'loop');
$counter++;
}
}
?>
<?php endwhile; ?>
<?php if($qode_options_theme13['pagination'] != "0") : ?>
<?php pagination($wp_query->max_num_pages, $blog_page_range, $paged); ?>
<?php endif; ?>
<?php else: //If no posts are present ?>
<div class="entry">
<p><?php _e('No posts were found.', 'qode'); ?></p>
</div>
<?php endif; ?>
</div>
It should create a list of 5 blog posts of the category 'test123'. How would I include this output into a shortcode though. what would be the "return" value?
Thank you guys so much!

Using wp_get_post_tags instead of the_tags

I'm working in one theme, and i trying to sumit to wordpress theme page, but i have the follow massages:
REQUIRED: This theme doesn't seem to display tags. Modify it to display tags in appropriate locations.
So i found that i don't have the_tags function but i have my own code with the wp_get_post_tags because i make my own html, so i dont know how to fix this problem.
This is my function
function basico_create_link($array_object_terms, $type) {
$link = '';
foreach ($array_object_terms as $object_terms) {
$link.='<a href="';
if ($type == 'category') {
$link.=get_tag_link($object_terms->term_id);
} else if ($type == 'tag') {
$link.= get_category_link($object_terms->term_id);
}
$link.='" > ' . $object_terms->name . '</a>,';
}
return substr($link, 0, -1);
}
And i used this way
basico_create_link(wp_get_post_tags(get_the_ID(), array('fields' => 'all')), 'tag');
Function the_tags() should be somewhere within The Loop. You can put it in your theme's index.php, although I myself would rather opt for single.php (since the tags are needed in blogposts).
Therefore, I start the loop, add some other content as needed and then I put the functions needed to get a blogpost properly displayed. In general, it could go like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article>
<!--some additional html/php content as needed -->
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php the_category(' '); ?> || <?php the_tags(); ?>
<!--et cetera-->

Wordpress custom field if else for showing data

In my theme I have custom field 'ext_url'. Now I want to get post url If I don't add 'ext_url' custom field.
I tried
<?php if(get_post_meta($post->ID, 'ext_url', true)): ?>
<?php else : ?>
<?php the_permalink(); ?>
<?php endif; ?>
When I don't add custom field, it is showing post URL. But, when I add custom field it is showing blank. Any Solutions?
Update: I've found a code
<?php
$url = get_post_meta($post->ID, 'ext_url', true);
if ($url) {
echo "<p><a href='$url'>External URL</a></p>";
}
?>
but, it is working for showing custom field. How can i add else function here? Sorry, I am not experienced in PHP.
If you want it to display the data from the custom field, you should add a line to echo the results.
<?php
$ext_url = get_post_meta( $post->ID, 'ext_url', true );
if ( $ext_url ) {
echo $ext_url;
} else {
the_permalink();
}
?>

Resources