I would like to create a metabox in the section of edit post menu. Like Tags for exemple, BUT instead of inserting tags I would like to put a link (ex. http://www.google.com) + name.
This link should go right to the end of my post content as a hyperlink with "Source: Name (hyperlinked by specified link)
Here is an example of what I am needing:
Title
This post is for example
Source: Google
Any help would be much appreciated. Thanks guys.
You can use the Custom Fields in Wordpress posts to add extre information to a post, then in your theme you can just grab the values. Here is what I use on my blog:
$custom_fields = get_post_custom($post_id);
if(!empty($custom_fields['article_source_url'][0]) && !empty($custom_fields['article_source_title'][0])){ ?>
<strong>Source</strong>: <?php echo($custom_fields['article_source_title'][0]); ?>
<?php }
Then in your wordpress post just add a Source URL and Title in the custom fields box:
You can use WP's Custom Fields to denote specific parameters for each page/post.
Then, in the template, just call them like this:
<?php
$custom_fields = get_post_custom();
var_dump($custom_fields);
?>
Note that they are arrays, so in case you have a field named URL, you'd need to call it like
echo $custom_fields['URL'][0];
This is done like this so you can have multiple values for the same field.
Also, in order to see the Custom Fields box, you need to go to the Screen Options (top right of the edit page) and enable them.
Related
I want to display all my custom post type project images on a template page, such as single-project.php, but the url doesn't display like I want it to.
It should be 'sitename/projects/' but it displays like 'sitename/projects/slug-of-post'.
Is there a way to modify the 'slug-of-post' ?
OR should I be creating an archive-projects instead to achieve that url since it does display 'sitename/project/' ? or is that wrong to use it like that?
If you want to display the images on a page through using a page template you will be able to choose the page slug as you please.
Then what you want is a page template file like this: https://developer.wordpress.org/themes/template-files-section/page-template-files/#creating-custom-page-templates-for-global-use
This will have something like this in the file header:
<?php /* Template Name: Example Template */ ?>
Alternatively, yes, have an archive-projects.php to display an archive (collection of all content) of the post type.
Make sure your custom post type has an archive.
See has_archive in the documentation of register_post_type():
https://developer.wordpress.org/reference/functions/register_post_type/
This can also be changed for already registered post types (eg if a plugin registered the post type)
On a child page x, i want to output a custom field from the parent page
<?php the_field('imagecategory'); ?>
The child page can also have this field (but i dont want to grab that)
so what i want is:
On the child page, grab the parent page id. And for that specific page show the output of the code
<?php the_field('imagecategory'); ?>
I asume i need to grab the parent id with $post->post_parent;, but i got no idea how to tell the code to grab that custom field for that specific page :(
Aparently you are using Advanced Custom Fields (ACF) plugin right? If yes, you must to get your custom page where you set your custom field before to show it on a different page, something like this:
<?php $mypage = get_page_by_title('my page'); ?>
<p><?php the_field('custom_field', $mypage); ?></p>
I hope it helps.
I need to get the value of a custom field (_subtitle) from the post meta and have it saved to the beginning of the post text (post_content).
How can I achieve this? (I tried the update() function but lost the post content before)
You need to use get_post_meta() it might look something like that if you want to place it within a template.
<?php
$subs = get_post_meta(get_the_ID(),'_subtitle',true);
$post_content = get_the_content();
echo $subs.$post_content;
?>
This would be the gist of it, you could also filter the_content(); and place the code within the functions.php
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.
I need to display multiple styles of posts; News1 and News2. The problem i am having is that i need to display each post in a different style. IE for posts that are in News1 they need to have date and share buttons but for News2 i don't need this.
I have a custom field set up to section News2 off called "Archive Category" and can use the get_post_meta for this.
My problem is just the conditional statements that should be in the posts file.
If anyone can help it would be greatly appreciated.
Thanks
In single.php, get the custom field value, then according to the value, call different template part:
get_template_part( 'prefix', 'other-parts-of-the-file-except-extension' );
In the template part files, layout the single post/page differently.
To elaborate, the actual layout code shall be written in template part files. The single.php only checks the custom field value, and call different template part file according to the value.
Pseudo-code would be (in single.php or single-post.php or single-customposttype.php, whichever you're using):
<php
$value=get the custom field value; // replace this with your function to get the value
if($value=='News 1')
get_template_part('template', 'news1'); // then you'll put layout in template-news1.php
else
get_template_part('template', 'news2'); // layout in template-news2.php
?>