Output custom post meta onto a specific template page - wordpress

I don't understand why its so hard to find an answer to this... But I really hope to because it is one of the things holding me back from releasing my theme..
To output or put the metabox option to use you would use code like this
<?php echo get_post_meta($post->ID, 'MoonRock_page_background_image', true); ?>
This is using wordpresses get_post_meta.
I am having a problem because I am trying to have a user upload a logo inside a custom post type and have that option outputted not on that page/post, but onto a template file, specifically the home template file I made.
There is a banner with an unordered list of 6 logo spots, I am trying to display the images the user uploads inside the clients post type.
Here is the UL code with some various PHP
<!-- Press Banner -->
<div id="press-banner">
<div class="span11 center nofloat clearfix">
<h2>some of our clients</h2>
<ul>
<li><a class="press-banner-logo-fastco" src="#" href="#" target="_blank"><img src=" <?php echo $text; ?>"/></a></li>
<li><a class="press-banner-logo-time" href="#" target="_blank"><img src="<?php echo get_post_meta($post->ID, '_moon_logo_image', true); ?>"/></a></li>
<li><a class="press-banner-logo-forbes" href="#" target="_blank"><img src="<?php moon_opts_show('press-banner-three',TEMPLATE_URI .'/images/logo.png'); ?>"/></a></li>
<li><a class="press-banner-logo-techcrunch" href="#" target="_blank"><img src="<?php moon_opts_show('press-banner-four',TEMPLATE_URI .'/images/logo.png'); ?>"/></a></li>
<li><a class="press-banner-logo-pandodaily" href="#" target="_blank"><img src="<?php moon_opts_show('press-banner-five',TEMPLATE_URI .'/images/logo.png'); ?>"/></a></li>
<li><a class="press-banner-logo-betabeat" href="#" target="_blank"><img src="<?php moon_opts_show('press-banner-six',TEMPLATE_URI .'/images/logo.png'); ?>"/></a></li>
</ul>
</div>
</div>
<!-- End Press Banner -->
This code actually works, but it works with the nhp options framework I am using... Only significant differance I noticed was instead of get_post_meta were using a custom function..
Here is the code for that function...
/**
* This is used to echo and option value from the options array
*/
function moon_opts_show($opt_name, $default = null){
global $moon_Options;
$option = $moon_Options->get( $opt_name, $default );
if( ! is_array( $option ) ){
echo $option;
}
}
I dont understand it, I kind of do, but I still wouldnt know how to use that inside my metabox code, I wouldnt know what variables to get and stuff..
So I am a little lost between if that is the solution to output the image into my homepage template, or if maybe I need to use WP_Query I have a few options in mind but its giving me a hard time.. So I ask for a little help.. please oh please im begging hehe...
EDIT: I am going to add the original question into simple terms...
Image post meta WILL out put onto the specific post, I have a clients post type, and a file called single-clients.php, when I create a new post all the meta data changes the content on that specific page... Obviously if I have a client post named WILL SMITH the featured image will apply to only WILL SMITH, and that goes for meta info, such as text and images.
What I would like to learn/acheive is outputting the image meta from WILL SMITH onto the homepage, I already see that I would need unique IDS and such, boy I am confused..

Ok I found the solution and as I guessed it was an easy one.
By putting it inside a WP_Query or a loop I guess you would call it, the meta data outputted onto my homepage template
<?php $loop = new WP_Query( array( 'post_type' => 'clients', 'posts_per_page' => 6 ) );
while ( $loop->have_posts() ) : $loop->the_post();
the_post_thumbnail();
echo get_post_meta($post->ID, '_moon_logo_image', true);
endwhile;
?>
Inside the endwhile I can add anything I want from my clients posts, or portfolio posts.. etc I am sure 99% of themers and any wordpress expert knows that, but I am working my way up to the expert level.. gotta learn sometime..

Related

configure parent/child relationship between 2 Custom Post Type only in child

I would like to create a relationship between 2 Custom Post Type avoiding that the user has to go to both to create the link.
In absolute terms, I would like the user to go to only one Custom Post Type (writing) which remains fairly basic: A title field, a text field, an image field, a link field for documents or url, and finally a selector field to select its parent (created with ACF).
In the hierarchy of the site, these posts created are at the end of the race, therefore, are only post children. They will display with "single.php".
The site obviously contains different menus which also have different sub-menus, a front-page.php for the home page, for the main menus we have the archive pages and for the sub-menus of the single pages.
The other Custom Post Types are the site menus which each contain their respective submenus. The user doesn't have to go here, hence I would like to create a parent/child relationship only by going into the child Custom Post Type.
For example, in my home page (front-page.php), I have a "Company" part if I click on it, I arrive on "archive.creb.php" where there is "Office", Secretariat" and "Management", if I click on "Office" I arrive on "single.creb.php", in this part, there is "the team" and "the calendar", if I click on "the team", I arrive at "single.php".
The team page (just like the "calendar" page) is an article written by the user.
So I'm looking for a way so that, when the user creates an article, he can choose where it will be (with the ACF selector field), and therefore choose its parent AND in addition, that the link of the article appears in this parent.
I know how to do this kind of operation by configuring a relational field in the 2 Custom Post Types concerned with ACF, but not with just one.
I did some research, especially on the ACF doc and I found this:
https://www.advancedcustomfields.com/resources/relationship/
https://www.advancedcustomfields.com/resources/querying-relationship-fields/
So I tried to adapt the ACF example with my code, but it's not easy because it's not quite the same... By schematizing the ACF example to mine, it gives this:
In both cases there is indeed a selection field creation, except that in the ACF example its selector chooses the child and I choose the parent. So I tried to reverse, using the code from single.location.php in my single.creb.php.... But it doesn't work.. my single.creb.php page shows a link loop to himself
So... I hope I was clear in my question! Hoping for an answer
Here are some codes :
functions.php
function custom_post_type(){
$arg = [
'public'=> true,
'label'=> 'CREB',
'show_in_rest' => true,
'supports'=>array('title', 'thumbnail', 'excerpt'),
'has_archive'=>true,
'menu_icon'=> 'dashicons-groups'
];
register_post_type('creb', $arg);
$arg = [
'public'=> true,
'label'=> 'redaction',
'show_in_rest' => true,
'supports'=>array('title', 'thumbnail', 'excerpt'),
'has_archive'=>true,
'menu_icon'=> 'dashicons-book-alt'
];
register_post_type('redaction', $arg);
}
add_action('init', 'custom_post_type');
single-creb.php
<?php get_header(); ?>
<!-- single creb -->
<main id="" class="">
<div class="">
<h1 class="">single CREB</h1>
<?php
if(have_posts()){
while(have_posts()){
the_post();
the_title();
$redac = get_field('textesousmenu');
if($redac){
echo $redac;
};
};
};
?>
</div>
</main>
<?php get_footer(); ?>
single.php
<?php get_header(); ?>
<!-- single creb -->
<main id="" class="">
<div class="">
<h1 class="">single</h1>
<?php
if(have_posts()){
while(have_posts()){
the_post();
the_title();
$redac = get_field('texte');
if($redac){
echo $redac;
};
$img = get_field('img');
if($img){
echo wp_get_attachment_image($img, 'modale');
}
if (have_rows('liendocurl')){?>
<ul class="">
<?php while (have_rows('liendocurl')){
the_row();
$nom = get_sub_field('nomlien');
$docurl = get_sub_field('urllien');?>
<li>
<a href="<?php echo $docurl; ?>" target="_blank">
<?php echo $nom ?>
</a>
</li>
<?php } ?>
</ul>
<?php }
$lien = get_field('relation');
if($lien){?>
<ul>
<?php
$name = get_the_title($lien->ID);
$link = get_permalink($lien->ID);
?>
<li>
<a href="<?php echo $link;?>">
<?php echo $name; ?>
</a>
</li>
</ul><?php
}
};
};
?>
</div>
</main>
<?php get_footer(); ?>
thank you in advance

Page jump on load when implementing lightbox functionality

I'm building a custom Wordpress theme locally and using Advanced Custom Fields for much of the content alongside custom post types.
I'm currently building the gallery section and want to use lightbox - the two size images are custom size featured images.
Before I add the lightbox anchor image my code looks like this:
<?php
$thumbnail_id = get_post_thumbnail_id();
$thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'featured-image-shop', true );
?>
<a href="<?php the_permalink(); ?>">
<img src="<?php echo $thumbnail_url[0]; ?>" alt="<?php the_title();?>">
</a>
However - when I then add in the lightbox images - the lightbox functionality works fine - but makes the page jump slightly on page load. The code looks like this:
<?php
$thumbnail_id = get_post_thumbnail_id();
$thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'featured-image-shop', true );
?>
<a href="<?php echo $thumbnail_url[0];?>" data-lightbox="gallery-images" data-title="<?php the_title();?>">
<img class="gallerypics" src="<?php echo $thumbnail_url[0];?>" alt="<?php the_title();?>">
</a>
Any ideas why this happens? I thought maybe it could be a javascript issue but even if I simply add an image in the anchor without the data-lightbox attribute it still jumps.
Perhaps someone has come across this issue before?
It's ok - just the act of writing this down gave an the answer so I thought I'd share for anyone who may have the same issue. I needed to add
html { overflow-x:auto; overflow-y:scroll; }
This forces the scrollbar to be there before the content is fully loaded which is why it was jumping before!

Accessing post list from different block

Hi there,
I'm trying to implement a slider on the Wordpress (fishpig) homepage. I've created a new phtml template and added a slider block in the xml file. So far so good, markup gets rendered to the homepage where I want it to.
But the loop doesn't run, obviously because $this->getPosts() is referencing a different class than the Fishpig_Wordpress_Block_Post_list.
My question is how can I access the post list from within the core/template slider block? I'm totally new to Magento so I'm just starting to wrap my head around the concept of blocks and how the whole templating system works.
I've tried
$className = Mage::getConfig()->getBlockClassName('Fishpig_Wordpress_Block_Post_list');
$block = new $className();
$block->getPosts();
and
$this->getLayout()->getBlockSingleton('Fishpig_Wordpress_Block_Post_list')->getPosts();
but to no avail. Can someone point me in the right direction?
When you include your new block and template in the XML, change the block type to "wordpress/sidebar_widget_posts". This will allow you to use the getPosts() method.
Alternatively, you could build your own collection of posts directly in your template:
<?php $posts = Mage::getResourceModel('wordpress/post_collection')
->addIsViewableFilter()
->setOrderByPostDate()
->load() ?>
<?php if (count($posts) > 0): ?>
<ul>
<?php foreach($posts as $post): ?>
<li class="item">
<a href="<?php echo $post->getPermalink() ?>">
<?php echo $this->escapeHtml($post->getPostTitle()) ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

wordpress advance custom field plugin showing broken image?

I am using wordpress acf plugin to show some custom images with their description and some text. So first I just made the acf plugin fileds like this and assigned the page to the home page with the conditional tags Location-> Rules-> Page->is equal to-> Home
now when I made my content-page.php to show the image code like this
<?php
if( get_field('image') ):
?><img src="<?php the_field('image'); ?>" alt="" /><?php
endif;
?>
I am getting only a broken image. The firebug is showing image source like this
Kindly help me to solve this. I have already wasted one day behind it. So any help and suggestions will be really appreciable. Thanks
Here is the screen shot for my custom fields setup
Here is the firebug html code which is showing the image source
Had this problem too. This is what I came up with that works:
<?php if (get_field('staff_photo')) {
$imgarray = get_field( 'staff_photo' );
?>
<img src="<?php echo $imgarray['url'] ; ?>" alt="" class="staff-photo" />
<?php } ?>
So, what I did was put get_field('field_name') array into a variable, then took a WAG that the key was 'url', which it was. Seems like the ACF people need to update their documentation.
Hah! Discovered another way -- this way you can pick a size:
<?php
if ( get_field('staff_photo') ) {
$imgarray = get_field( 'staff_photo' );
$size = "thumbnail"; // (thumbnail, medium, large, full or custom size)
echo wp_get_attachment_image( $imgarray['id'], $size );
}
?>
For wp_get_attachemnt_image to work, you have to extract the image id, for which the key is, ta-da! 'id'.

Get excerpt and permalink outside loop in wordpress

I'm trying to get the post excerpt and permalink outside a wordpress loop, pulling the thumbnail and the title work fine but when trying to get the excerpt it doesn't work? And how would I get the permalink? My code so far is below.
Thanks!
<div id="featured">
<?php
$ftid = 104;
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ftid) );
?>
<img src="<?php echo $url; ?>" style="float:left;" />
<div class="featured-info">
<h2 class="post-title"><?php echo get_the_title($ftid); ?></h2>
<?php $my_post = get_post($ftid); echo $my_post->post_excerpt; ?>
</div>
</div>
And maybe if there is a better way of doing this you can point me in the right direction? :)
The permalink should work as is:
$permalink = get_permalink($ftid);
The excerpt though, when accessing the page object directly there actually has to be excerpt content (not just post content). If you didn't manually type in an excerpt on the post then nothing will appear. Inside the loop, the_excerpt() will automatically generate from the content if an excerpt hasn't been manually typed. Did you type a separate excerpt in the WP admin?
Look at wp_get_attachment_url() - you left the $ off the name of the $ftid variable.

Resources