Display custom field from parent page - php

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.

Related

Contao: Frontend - Get theme section of another page - not the current page

I would need to reach a custom section by page id (or PageModel Object), but I don't find a way to get the FrontendTemplate. I would like to use it in a dropdown navigation to echo a custom section of the hovered (parent) page.
If somebody will search for, the answer is:
<?php
$articles = \ArticleModel::findPublishedByPidAndColumn($id, $column_name);
echo static::getArticle($articles[0]); // for ex. the first article
?>

Conditionally display menu for page type in Wordpress

I am relatively unfamiliar with Wordpress and I am creating a custom theme for a client. I would like to either display or remove the main menu depending on the page type. I have researched several options like removing the navigation from header.php and referencing it separately and also making the menu conditional which is preferable.
I have a custom page type in my theme called 'landing page' on which I would like the menu to be never be displayed, though it will be on every other page. Ultimately there will be a lot of these and I would rather I didn't have to intervene.
I would rather not duplicate my header.php file but I can only find reference to displaying the menu conditionally like below by page name or ID which seems ridiculous.
<?php
if (is_page('contact')){
<?php include(TEMPLATEPATH.'/headerA.php'); ?>
}
elseif (is_page('gallery')){
<?php include(TEMPLATEPATH.'/headerB.php'); ?>
}
else {
<?php include(TEMPLATEPATH.'/headerA.php'); ?>
}
?>
Rather than including files as above, I will put the whole thing into my header and just make the navigation conditional. Does anyone know how I should approach this using my custom page type landing page rather than by page name so every page created with that type will never have a menu?
Thanks
Are you talking about a Custom Post Type (CPT) or a page called landing-page?
They are completely different. See http://codex.wordpress.org/Post_Types
In any event, this will work for a custom post type or a page:
if ( !is_singular( 'custom-post-type-name-or-page-slug-here' ) ) {
get_template_part('menu');
}
It says: "If this page is not a single page or a CPT, load the file menu.php from the theme folder."
See also http://codex.wordpress.org/Include_Tags:
The get_template_part() tag includes the file {slug}.php or
{slug}-{name}.php from your current theme's directory, a custom
Include Tags other than header, sidebar, footer.

Custom link and name meta box in wordpress

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.

Adjust Wordpress function to grab author ID of post

I use a point system plugin for Wordpress. By adding this code to the author.php page:
<?php cp_displayPoints($authordata->ID); ?>
It will echo X Points. This is the points of that respective author. When I add the same code to single.php (post page), it echos the logged in user's points, and if not logged in, it returns blank.
How can I alter this code so that it will function properly on the single.php page too? This would mean that it would echo the points of the author of that post.
Just call get_the_author_meta from within the loop.
So, you just need to test if you have a currently signed in user, if not use the post author instead. Something like this.
<?php
if(!$authordata->ID)
cp_displayPoints(get_the_author_meta('ID'));
else
cp_displayPoints($authordata->ID);
?>
EDIT:
To display only the post author's ID, just use
<?php cp_displayPoints(get_the_author_meta('ID')); ?>

Call Wordpress Archive page with multiple category IDs

I'm using custom fields on my template page and I want create a hyperlink to multiple category IDs.
This snippet gets my custom post field and returns category IDs 23 and 19, or whatever series I put in the custom field, ie: 23,9,17:
<?php $featuredpost_cat = get_field('featured_category_id'); ?>
I want to do something like this (I don't know the proper calls and syntax):
<a href="<?php wp link to $featuredpost_cat ?>"My Link Title </a>
I hope this is clear enough.
To category link by category id:
<?php
$featuredpost_cat = get_field('featured_category_id');
$category_link = get_category_link( $featuredpost_cat );
?>
My Link Title
You can get more details about this in the wordpress function reference site.
http://codex.wordpress.org/Function_Reference/get_category_link
Try this:
My Link Title
Im just giving an general solution how you can link to the id hope this helps.

Categories