I have created a custom post type named bateau in functions.php. I also created a page where all the posts that belong to the bateau custom post type are displayed, showing some of the most important fields. Finally, when I click on one of those posts, a link sends me to the related custom post page, i.e. a particular type of boat.
The custom post bateau has its own custom fields, a thumbnail, and also its custom taxonomies.
I want to retrieve and display, when I'm on the page of a particular boat, not only the most important but all its custom fields, its thumbnail and its taxonomy.
So, in the functions.php, I have written this filter :
add_filter('the_content','add_text');
function add_text($text) {
global $post;
$text = "";
if($post->post_type == 'bateau') {
$text.= "<h1 class=\"bateau-entry-title\">".get_post_meta( $post->ID, 'bateau_nom', true )."</h1>";
return $text;
}
}
It works fine, provided that I don't write plain HTML text within closing and opening PHP tags, i.e. it only works if I wrap all the HTML in a PHP text var. If I don't do so, the content is also displayed at the beginning of the header, not once, but twice. Strange, isn't it ?
If I add this line :
$text.= "<img class=\"thumb\" src=\"the_post_thumbnail();
the thumbnail displays properly in the "article"...but, guess what, also at the beginning of the header, not once, but twice !!! I just can't find why the thumbnail behaves like this. Anyone can help please ?
In Wordpress, the page you called:
when I'm on the page of a particular boat
is called single. instead of putting a filter on the_content using
add_filter('the_content','add_text');
you would better add whatever you want to the loop. To do so, you may create a file called single-bateau.php (this is a protocol, so you need to name it exactly as this) in your current theme's root directory and there you may have your loop:
if(have_posts()){
while(have_posts()){
the_post();
the_title();
if( has_post_thumbnail() ){
the_post_thumbnail( $thumb_size );
}
the_taxonomies();
the_category();
the_tags();
}
}
I don't think you need the img tag if you're using the_post_thumbnail().
Try doing something like:
if ( has_post_thumbnail() )
the_post_thumbnail();
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 am working on a WordPress theme. I use a plugin to display pictures on a page (for each post 1 picture), Whenever one of this pictures is clicked the following code registers this and opens a lightbox with the content of the post in it:
<?php
if($_REQUEST['popup']!=''){
$postObj = get_post( $_REQUEST['pid'] );
echo '<div class="ostContent">'.$postObj->post_content.'</div>';
exit;
?>
This all works fine.
Now the problem is that all content get displayed nicely. but for some reason shortcodes don't work. and also when I use a widget in post plugin to display a widget in the post, it doesn't get displayed.
First I tought I needed to enable shortcodes. So I changed this:
echo '<div class="ostContent">'.$postObj->post_content.'</div>';
with this:
echo '<div class="ostContent">'.do_shortcode( $postObj->post_content ).'</div>';
But still nothing. So now I have no idea what to change to make the lightbox show widgets
Hope anyone knows the solution!
EDIT: when I open the post outside the lightbox (by just going to the single page) the shortcode get used like it should be. so somehow the code above don't recognize the shortcode or...
According to a sample here: http://codex.wordpress.org/Function_Reference/do_shortcode
It looks like you need to change: echo '<div class="ostContent">'.do_shortcode( $postObj->post_content ).'</div>';
to:
echo '<div class="ostContent">'.do_shortcode([shortcode_in_brackets]).'</div>';
This should actually display the code. I am assuming that you have defined the actual text in the widget in which the shortcode applies.
Otherwise in the way you currently do it the PHP fires before the post_content even has a value.
I think I understand your problem.
The following should work, assuming the posts in question have a post_type of post:
<?php
// Check for existence of 'popup' & 'pid' query vars
if ( $_REQUEST['popup'] && $_REQUEST['pid'] ) {
// Select single post by ID (using value of the 'pid' query var)
$query = new WP_Query( array ( 'p' => $_REQUEST['pid'] ) );
// Check that the query has returned something
if ($query->have_posts()) {
/* Loop through query until we run out of posts
(should only happen once in this case!) */
while ($query->have_posts()) {
// Setup post, so we can use the_content() and stuff
the_post();
echo '<div class="ostContent">';
/* The part we've been waiting for! the_content() will
display your post content as expected */
the_content();
echo '</div>';
}
}
}
?>
WP_Query is the way to go the majority of the time when needing to retrieve posts: http://codex.wordpress.org/Class_Reference/WP_Query
Your code was retrieving and displaying data almost directly from the WordPress posts table, giving WordPress no chance to apply any of its internal actions and filters (e.g. automatic paragraphs from double line breaks, shortcode execution).
I am trying something out on wp. I want to display custom post types title on page post type.
here is my code.
function get_post_type_title(){
if('movie_ronny' == get_post_type()){ // if post type is movie_ronny, get post title
$movie = get_the_title(); //hold the post title in $movie.
}
if(is_page() ){ // if viewing page, display movie_ronny title
echo $movie;
}
}
add_filter('wp_head','get_post_type_title');
Above code does not display the title of movie post-type when viewing page. Any help is highly appeciated.
Your post type will most likely be shown on its own single-movie_ronny template. In which case of course you wouldn't need a conditional -
if('movie_ronny' == get_post_type()){...}
The other reason why this question is a bit strange is that your loop query must define within it's arguments what type of post, page or custom post type it is querying. So you would also then know which post type you are messing with and not need the first conditional.
And no matter what your situation is:
if(is_page() ){ // will only return true for pages. not posts or CPT's
Also,
get_the_title($id); // needs a post ID outside of the loop.
Well, there are two ways this function will fail (i.e. do nothing):
If the first if block is ignored then the second if block will not do anything (I believe it will actually show a notice, because $movie would not be defined).
If is_page() does not evaluate to true then nothing will be done in any case.
If you know anything about programming then this should be obvious. So, either you have no idea what you are doing, or I am misunderstanding something here.
Try this one:
<?php
$post_type = get_post_type_object( get_post_type($post) );
echo $post_type->label ;
?>
I would like to add a feature to a WordPress page to display a short list of people, like the one seen here - http://www.blenderbox.com/company/team
I have done this on other sites by styling lists with css. This time however, it's for a client and I can't trust them to copy and paste a <li>, editing the name, job title and img name without messing it up.
What is the best way to go about creating this so that it can be easily reduced/added to in the WordPress dashboard? Basically, so that the user simply clicks "add person" then fills in some fields before updating the page.
I'm new to WordPress (though I understand how it works) but I am competent coding so hopefully an informative nudge in the right direction would be sufficient.
Thanks, Ben.
Use categories. Create a new category called Person or Team or something to that effect. Then use the Post Title for their Name - any other information can be put in the post and then add the image to the Featured Image.
Create a new template for this page by copying over another template file and giving it a header like this to name it.
<?
/*
Template Name: About Us / Team / Whatever
*/
?>
And in the page call only the information you want with something like this:
//Set up the Loop
<?php
global $post;
$args = array (
'category' => 4,
'order' => 'ASC');
$teamposts = get_posts( $args );
foreach( $teamposts as $post ) : setup_postdata($post); ?>
//Call the information you want - put them in <li> if you need
<?php the_title();?>
<?php the_excerpt();?> //OR the_content
<?php the_post_thumbnail('new-image-size');?>
//Close the loop
<?php endforeach; ?>
You can then go on to call anything else you want or run another loop on the same page to call other information.
To get the right sized thumbnail you have called 'new-image-size' - to set this up add a line like this to your functions.php file.
// Post Thumbnails
add_theme_support( 'post-thumbnails' );
// name of the thumbnail, width, height, crop mode
add_image_size( 'front-page-services', 450, 270, true );
I use stackoverflow very often and just found out in google about this one, Nice :D
Well,
i want to save my every post i write from wordpress to my 3rd (OEXChangeble) website aswell at the same time, so i need to send the info to my website, developing a plugin iguess
I need basically the permalink (and i would be able to extract the rest of the params from my wesite), but better if i can get title, tags, permalink and description(or some of it)
i understand by my google research that all i need to do is add something like
<?php
//header of plugin
function myFunctionThatSendsMyWebsite($url){
// procedure containing a file_get_contents('myqwebsite?url=') request to my website
}
add_action('page_post', 'myFunctionThatSendsMyWebsite', $permalink));
?>
I have problems thoug finding the name of variables i have missing (marked by ???). I know that $post contains all objet, how to extract the info from it (if there is), or if it's complicated, it would be enought for me with permalink
Any tip?
Thanks!
according to this link, this should work, the post id will be sent to this function automatically and you can get anything you want from a post id.
function myFunctionToSendPost($post_ID) {
$post = get_post($post_ID);
$title = $post->post_title;
$content = $post->post_content;
$permalink = get_permalink($post_ID);
...
sendToYourServer($params);
return $post_ID;
}
add_action('publish_post', 'myFunctionToSendPost');
BTW, this function is called when a post is published, you can change it to happen when saved by
add_action('save_post', 'myFunctionToSendPost');
add these lines to your theme's functions.php file.
1) While this doesn't take advantage of the save_post feature, you can even use this code to display blog posts on a completely separate web site, as long as it’s on the same server and you have filesystem access to the WordPress directory on the original site. Simply modify the require() in the first block on this page to use the full path to your WordPress installation:
<?php
// Include WordPress
define('WP_USE_THEMES', false);
require('/var/www/example.com/wordpress/wp-load.php');
query_posts('showposts=1');
?>
position your post with a while loop:
<?php while (have_posts()): the_post(); ?>
<?php endwhile; ?>
if you want to specify which parts of the post to display use this code:
<?php while (have_posts()): the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<p>Read more...</p>
<?php endwhile; ?>
2) How about using the rss feeds from your wordpress blog?
The following code will display a list of your feed titles with descriptions including a hyperlink to the original WordPress posts:
<?php // Load the XML file into a Simple XML object
$filename = http://feeds.feedburner.com/<em>yourfeedname</em>";
$feed = simplexml_load_file($filename);
// Iterate through the list and create the unordered list
echo "<ul>";
foreach ($feed->channel->item as $item) {
echo "<li><a href='" . $item->link . "'>" . $item->title . "</a></li>";
}
echo "</ul>";
echo "<li>" . $item->description . "</li>";
?>
3) Feedburner has a free feature called BuzzBoost where you can get your posts to show in a regular HTML website that once activated you can simply copy the script that they provide into your HTML where you want the list to appear. From your feedburner account you can adjust some elements like whether the Blog title should appear or not, the format of the date, etc...
You can also style the output using regular CSS within you websites existing CSS.
Check it out Feedburner here:
https://www.google.com/accounts/ServiceLogin?service=feedburner&continue=http%3A%2F%2Ffeedburner.google.com%2Ffb%2Fa%2Fmyfeeds&gsessionid=5q8jqacBluH1-AnXp08ZFw
Are you trying to save a copy of the post somewhere else when they first publish the post? Or on every page view?
You can trigger it when the post is saved by writing a plugin that implements the save_post hook:
http://codex.wordpress.org/Plugin_API/Action_Reference
To do it on every page, you'd probably write a plugin with a filter hook on the page (if it's something you want other people to use) or if it's just one site, you could add it to your theme.
But... Are you sure you want to do this? It seems like your keepyourlinks site might be better implemented as an update service: http://codex.wordpress.org/Update_Services