Show post's custom field to category page

Posted in: Newspaper
Post count: 75

Hi,
I have created a simple custom field called personName in a post.
I have installed td-api-plugin, created a page and I use “Block 77” in this.
I want the value of this custom field to show up under the post’s title in that page.

Let’s assume that I create this custom field with key “personName” and I give it a string value.
Then I go to module_77.php file and I add some code to display the this string value:


<?php

class td_module_77 extends td_module {

function __construct($post) {
//run the parrent constructor
parent::__construct($post);
}

function render() {
ob_start();
?>

<div class="<?php echo $this->get_module_classes();?>">
<div class="td-module-image">
<?php echo $this->get_image('td_324x235');?>
<?php if (td_util::get_option('tds_category_module_1') == 'yes') { echo $this->get_category(); }?>
</div>
<?php echo $this->get_title();?>
<?php echo $this->get_excerpt();?>

<?php $personName = get_post_meta($post->ID, 'personName', true);
echo $personName;
?>

<div class="td-module-meta-info">
<?php echo $this->get_author();?>
<?php echo $this->get_date();?>
<?php echo $this->get_comments();?>
</div>

<?php echo $this->get_quotes_on_blocks(); ?>

</div>

<?php return ob_get_clean();
}
}

The result is here

The value is not showing up under the excerpt.

I have tried various ways, like having that string in “Subtitle” and tried to display the post’s subtitle in the page, under each post’s title or excerpt but with no luck.

Any recommendations, please?

Post count: 35449

Hi,

Please check the solution from this similar topic -> https://forum.tagdiv.com/topic/add-additional-fields-to-posts-in-category-grid/

Thank you!

Post count: 75

Hi Calin,
Thank you for your recommendation but, unfortunately, it didn’t help me.

Maybe because your customer creates custom fields with ACF plugin.
In my case, I need to display the value of a simple custom field, without using any plugins.

I tried to add the following line


<?php $personName = get_field( 'personName',$this->post->ID); ?>

as seen in the post proposed me but the page breaks.

I would appreciate it if you could help me a little bit with this.

Thanks in advance.

  • This reply was modified 7 years by lazdim.
Post count: 35449

Hi,

If you refer at this custom fields -> https://www.screencast.com/t/ErHccG9IP then you can be used to add extra metadata to a post not to add extra fields on the post.
Here is the WordPress documentation for Custom Fields -> https://codex.wordpress.org/Using_Custom_Fields

So I add 2 elements in custom fields -> https://www.screencast.com/t/y44OJzJfV7KE I have set post template 1 on this post -> https://www.screencast.com/t/1xTy1WPeXx3 so I need to add the code in loop-single-1.php -> https://www.screencast.com/t/n5oPBeQZkX4 the path is wp-content/themes/Newspaper/loop-single-1.php the results https://www.screencast.com/t/V1Rp7ifzZw

So you need to edit the loop specific to your post template form that post.

Hope this will help you!
Thanks.

Post count: 75

Hi Calin,
Thank you very much for the time you spent and the the information you provided.

I’m really sorry for this but, I already new that 🙂
Maybe my initial question made some misunderstanding, so let me show what I actually need.

I am using Block 77, provided by the td_plugin_API.
I am adding Block 77 in a page, this one

So, that page looks like this: https://www.screencast.com/t/AX4ugqr36t

Then, I open the module_77.php file and I make a veeeeeeery small addition to the code

<?php echo $this->get_excerpt();?>

so I can display the excerpt below the post’s title (category page level).

The result after adding the excerpt is this:https://www.screencast.com/t/QZHazjvlHKkS
Excerpt added bellow post’s title.

Now, as I did with excerpt, I need to display the value of a custom field, just like the one your showed me, but not IN THE POST.

Let’s say I create a custom field and I give it a value like this: https://www.screencast.com/t/av4rjj6z3
You can see that I have a custom field with key “personName” and its value is “Joe Doe”

What I need is to display the “personName” value after the excerpt in the category page where I use Block 77 like this:https://www.screencast.com/t/ceKlsV9sP

Can you please help me with that?

Thank so much in advance!

Post count: 35449

Hi,

Please check the documentation form this article -> https://www.wpbeginner.com/wp-tutorials/wordpress-custom-fields-101-tips-tricks-and-hacks/
example on module -> https://www.screencast.com/t/gJD5AYTYUdxa -> https://www.screencast.com/t/qFG2caaCh3 results -> https://www.screencast.com/t/FerVYBgD9Q
The code <?php echo get_post_meta($this -> post ->ID, 'key', true); ?> just to set it in your 77 block and change the key

Hope this will help you!
Thanks.

Post count: 75

Hi Calin,
Well, the irony in this issue is that I had seen and bookmarked the article you proposed me from wpbeginner.com!
How much stupid I am not seen the solution there?
I have an excuse: because the theme already has some custom fields, I thought it wouldn’t be possible to be so straight forward. Stupid, but true.

Anyway, your solution worked like a charm!

Thank you so much!

Since we arrived so far, will it be an exaggeration to ask how this will become a permalink to the corresponding post?

Thank you once again in advance!

Post count: 35449

Hi,

I don’t quiet understand what you mean with “how this will become a permalink to the corresponding post” this is just a custom field, the permalink is build from the title of post -> https://www.screencast.com/t/FN5FpZhC

Thank you!

Post count: 14

@Calin, thanks your solution is still working on td_module_flex_1.php, wanted to use that on loop 2, but not able to found on which line i can put the code mentioned in your reply of Mar 27, 2019 at 1:27 pm.

If you can help me where i can put this code ?
So will continue to use loop 2 on my category templates, otherwise i have no other choice (means use of flex 1).
Here you can see that i use flex 1 and code is working (under title) https://www.viaggindia.it/offerte/

here i’m using loop 2 : https://www.viaggindia.it/destinazioni/india/viaggi-nord-india/
but not found where to put the code (i know the file path plugins/td-cloud-library/shortcodes/common/tdb_loop_2.php, but where exactly i have to use the code no).
Thanks for yor help.

Post count: 35449

Hi viaggindia,

To edit the loop 2 you and set the code under the post title, you’ll need to edit this file wp-content/plugins/td-cloud-library/includes/tdb_module.php set the code after the $module_title_tag tag is closed -> https://i.imgur.com/KGllKC0.png like this https://i.imgur.com/i6liVfY.png the results will be -> https://i.imgur.com/GgTONrg.jpg
Unfortunately, this files can not be set in a child theme.

I hope this will help you!

Post count: 14

Thanks @Calin !
It works fine only when i put text like you show :-
https://imgur.com/i6liVfY
But if i use code, it crash the site :-
https://www.viaggindia.it/wp-content/uploads/php-error.png

What to do in my case ?
Thanks

Post count: 35449

Hi viaggindia, it is not working because the code need to be a little change, need to be like this -> code -> https://i.imgur.com/pI9s7wa.png -> https://i.imgur.com/lC9xvlJ.png
I hope this will help you!
Thank you!

Post count: 14
Viewing 13 posts - 1 through 13 (of 13 total)
The forum ‘Newspaper’ is closed to new topics and replies.