I'd like to have the possibility to define some posts in wordpress where the full post is shown and not an excerpt.
There are various solutions like:
Use a hidden html to declare this and code into the theme to either use the_content or the_excerpt
Hardcode into the theme (if postid == xx then the_content else the_excerpt)
Use post meta data and add "if" into theme to check for them
Create a plugin which adds the functionality automatically and also a checkbox "Always show full" into the post-editor.
The first one is easy but ugly, 2nd one should be doable with some googling but the 3rd one seems to be the most appealing to me but I have no idea how to achieve this.
Since usually in the template all i have is somewhere the_excerpt method from wordpress. I therefore should somehow inject some code there and check if the checkbox for the current post is set and then just use the_content instead. Is this possible at all or do I need to modify the theme anyway?
Thanks for your inputs.
I would use custom fields, it's more flexible, so you don't need to hard code the page ids.
<?php
$show_full_content = get_post_meta($post->ID, 'show_full_content', true);
if ($show_full_content == 'yes') {
the_content();
} else {
the_excerpt();
}
?>
You might be interested in ACF, it can make it more user friendly.
Related
I am new to ACF and am trying to understand how to create entire sections of content.
I can't seem to find any answers to this, though I am perhaps not phrasing the question correctly because of my limited understanding.
I have created my first website using ACF and Gutenberg where I have a separate php template for each block of content, which is then registered as a Gutenberg block. This works perfectly in that the content blocks can contain entire blocks of html/php and are always available but only appear on the page when they are chosen, making it very user friendly.
What I am struggling to understand is how you can insert an entire section WITHOUT Gutenberg - say using a shortcode.
At the moment if I have a structure like so:
<section class="reusable-block">
<div class="title-box">
<?php if( get_field('main_heading') ): ?>
<h1 class="hero-heading"><?php the_field('main_heading'); ?></h1>
<?php endif; ?>
</div>
</section>
If I use the shortcode [acf field="main_heading"] It will obviously output the contents of that field but how do I enter a shortcode that would enter the entire section on a needs basis across any page?
If I hard code it into the page template then the fields are always visible on the WordPress backend page, regardless fo wether they are being used, though if that's the way it has to be then I'll work with.
Unfortunately I need to use ACF with a page builder for a current project (I do not have a choice on this). This is the reason I cannot use Gutenberg and register blocks and will need to use shortcodes.
In short, is there a way to create an entire ACF block that would then contain html and acf fields, and even better that could be built in a separate PHP file as you would when using this in conjunction with Gutenberg.
I have watched about 3 courses online and read lots of resources, but this one thing still seems to allude me.
You already mentioned one possibility yourself: creating a custom shortcode for this.
Therefore, you either need to create a new plugin, or, append this to your (child) theme's functions.php:
function shortcodeHeadingBlock($attr, $content = null) {
if (! class_exists('ACF') ) { // will not work without ACF
return '';
}
$opts = shortcode_atts([
'field' => 'main_heading',
], $attr);
ob_start();
?>
<section class="reusable-block">
<div class="title-box">
<?php if( get_field($opts['field']) ): ?>
<h1 class="hero-heading"><?php the_field($opts['field']); ?></h1>
<?php endif; ?>
</div>
</section>
<?php
return ob_get_clean();
}
function setupShortcodes() {
add_shortcode('headingblock', 'shortcodeHeadingBlock');
}
add_action('plugins_loaded', 'setupShortcodes');
After this, you can use the shortcode
[headingblock]
anywhere across your page where shortcodes are interpreted. The shortcode first checks if the ACF-class is available which implies that the ACF functions should be available, too.
The shortcode also has an optional field attribute that defaults to 'main_heading', meaning you can also do
[headingblock field="another_acf_field_name"].
It then returns your code template and inserts the desired ACF field.
Just in case your .title-box CSS isn't available in the admin interface, you might have to enqueue it separately using the admin_enqueue_scripts hook. Regarding CSS, you will also have to make sure the full selector for .title-box doesn't contain additional classes/objects that might no longer apply when the shortcode places it into other places on the page.
I work on a website with a custom post type, described in the functions.php file of a child theme.
I created a function, still within the functions file, that describes the beahavior of a page for this custom content : basically, to achieve that, I use get_the_terms() and get_post_meta(), within a function named add_thecontent(). It's called with a filter : add_filter('the_content','add_thecontent');
It works fine : I can retrieve the name, the custom taxonomies, the several fields...etc.
I also work on another website, still with a custom post type (a different one), with a different theme. I use the same mechanism as stated above (child theme + functions file + content called within a filter) but when I want to display a preview of my custom post, the engine calls the file content-single.php of the main theme. This file calls the_category() and that leads to warnings in my page, beacuse I don't have a "regular" category for my custom post, only custom taxonomies.
I tried to put a content-single.php in my child theme in order to redefine its beahavior, but I get errors. And I think it's not a proper way to proceed.
What I want is to force WP engine to display a custom post as I defined in the functions.php. How can I achieve that ? Thanks and sorry for my english
I would recommend you to install and debug using Which Template Plugin once you install this, you can know which file is being called to parse your custom post.
Also, take a look at the worpdress hierarchy page here
If you created a custom post, the you can create an individual page for that custom post as single-$posttype.php and you would be able to control the flow of the file.
Good luck!
Solved. In the parent theme, content-single.php included the following code :
<span class="cat-name"><?php $category = get_the_category(); echo $category[0]->cat_name; ?></span>
So I created a content-single.php in my child theme, and I made a test on the type of post. If the post is not a custom post I created, display normally its category. Else, do nothing, because I handle the taxonomies myself.
<?php if($post->post_type != 'myCustomPost') {;?>
<span class="cat-name"><?php $category = get_the_category(); echo $category[0]->cat_name; ?></span>
<?php } ?>
Where can I find the file where the_content() gets the value from post_content in the database?
I wanted to do the concept but with another content from the database,
So far, I've added a new column to the database ex. post_content2
How can I get its value by using something like
<?php the_content2(); ?>
I tried looking at post-template.php and found the function but not how it gets the value from the database post_content.
From the comments above, and a more complete answer:
Do not modify core WP tables. Either use post_meta functions, or add a new table.
the_content() is just a convenience function that directly accesses the posts table, content column. You can do the same thing at any time by getting the value from the database, and passing it through apply_filters('the_content', $my_custom_content);. If you want to write your own the_content2() function, it could / should be done like so (using the post_meta method mentioned above):
function the_content2() {
// Global in the current post so we can get the ID
global $post;
// Load your custom content
$my_content = get_post_meta($post->ID, 'my_custom_content');
// Echo it out (like the_content()) applying 'the_content' filters
echo apply_filters('the_content', $my_content);
}
If you want to figure out how to SAVE this content, then you can read this writeup:
How to Create Custom Meta Boxes in WordPress
edit
It's worth noting that this functionality is already available in a few different plugins you can install. In my experience Custom Field Suite is the most reliable and easy to use (but that's just my opinion).
I've made a template for specific pages of our website that display similar content, yet are different form the rest of the pages of the website - here's an example. I have several custom fields that I use to include all of the items you see in the boxes that float to the left and right.
My problem is, I also have pricing that's included from a custom field - and because I want it at the bottom of the page, it actually appears outside the content to work (see the bottom of my example given above). Is there a way I can call the content and tell it to display these custom fields at after the content is displayed?
Obviously the following doesn't work, but just to give you an idea:
<?php the_content(get_post_meta($post->ID, "Pricing_Mexico", true); ?>
Additionally, there are actually a lot of custom fields that need to be displayed, so if it were possible to include them in bulk (including the markup which includes divs and what-not) that would be preferred. Thanks!
You could filter the_content() so in your functions file:
function contentFilter($content){
// Output the current content
echo $content;
// Output the meta field
echo get_post_meta($post->ID, "Pricing_Mexico", true);
}
add_filter('the_content', 'contentFilter');
You could echo as many meta fields as you need inside the function.
I'm using a theme framework and am trying very hard to resist the temptation of editing core files. I want to add the functionality of post formats, but I need to be able to remove certain elements for specific post formats.
function thesis_teaser_headline($post_count, $post_image) {
thesis_hook_before_teaser_headline($post_count); #hook
if ($post_image['show'] && $post_image['y'] == 'before-headline')
echo $post_image['output'];
echo '<h2 class="entry-title">' . get_the_title() . "</h2>\n";
if ($post_image['show'] && $post_image['y'] == 'after-headline')
echo $post_image['output'];
thesis_hook_after_teaser_headline($post_count); #hook
}
What would be the most efficient way to go about removing headline data for a post format such as 'link' (for example)? This function is being called to generate the content for the teasers from the homepage loop. I could just make an entire custom loop, but it won't tie in with the Thesis backend which makes it much less flexible.
Thanks!
I'm not familiar with Thesis, but I see it is calling hooks, so you could possibly put one of your functions on these, that in case you don't want any content from original thesis_teaser_headline() would turn on output buffering (ob_start()) on before hook, and clean it (ob_end_clean() or $content = ob_get_clean() if you want to alter its content, not completely replace) on after hook.
You can use the /* */ control to make the function in comment. So it will not be executed. Or put just the section you want to delete between them...
What about tweaking the html with jquery? Check on the post classes (the list of css classes that's on the div that wraps each post) to see if you can select what you need from there.
On top of all, I recommend to avoid using that framework, it's pure nightmare when you need to tweak it.