Wordpress displaying custom post types and their fields - custom-fields

I have set up a Custom Post Type called 'RELEASES' - think music cd release.
This post type has fields named 'release_artist', 'release_title', 'release_date', 'release_artwork' and 'release_tracklisting' for entering all of the relevant music cd information.
I am having real trouble actually displaying this information in my Wordpress template.
I have really only had luck outputting a list of the titles and none of the other data.
Any idea what I put in the LOOP to display all of the information? Preferably each in its own LIST item so I can style each separately?
Any thoughts greatly appreciated.

You can use get_post_meta to pull in your fields as needed. Inside your loop, you can start with the following:
<?php
$release_artist = get_post_meta($post->ID, 'release_artist', true);
$release_title = get_post_meta($post->ID, 'release_title', true);
?>
<ul>
<li class="release_artist">
<?php echo $release_artist; ?>
</li>
<li class="release_title">
<?php echo $release_title; ?>
</li>
</ul>

Are those custom fields? If yes, try what codex.wordpress.org is saying. Better yet, try ACF plugin.
-- edit
If you want to display parts of your pages on other ones (eg. on your home), you need to use query_posts. This is fairly simple function. For your loop, try something like this:
<?php
global $wp_query;
query_posts(array(
'post_type' => 'releases'
));
while(have_posts()) : the_post(); ?>
<?php $key = get_post_meta($post->ID, 'Opis nazwy'); ?>
<li <?php post_class(); ?>><?php if($key) { echo $key[0]; } else { the_title(); }; ?></li>
<?php
endwhile;
wp_reset_query();
?>
$key is a single value, here set to release_artists. It's purely for testing. If it works - feel free to define your own variables.

You should use:
<?php the_field('field_name') ?>
inside your loop. Hope it helps!

From most of the documentation I've seen online, query_posts shouldn't be the go-to function for creating custom queries and loops. The following code snippet might be a good starting point. You should be able to use this inside or outside of the main loop of your themes template files.
$args = array(
'post_type' => 'release', //remember this is-case sensitive
'posts_per_page' => -1,
);
$releaseQuery = new WP_Query( $args );
if ( $releaseQuery->have_posts() ) :
while ( $releaseQuery->have_posts() ) :
$releaseQuery->the_post();
// Fetching the post ID for demonstration and for use later
$c_id = get_the_ID();
// After running the_post(), alot of the Wordpress functions (not all) can now be used without supplying the post ID.
echo get_the_title();
// You could also have used get_the_title($c_id);
// Then:
echo get_post_meta($c_id, 'release_title', true);
echo get_post_meta($c_id, 'release_artist', true);
endwhile;
endif;
// Return to the current page's main query
wp_reset_query();
// This should now display the page's title
the_title();

About your ID's question:
Every item, like posts and pages in WordPress have an "ID", but they are not normally shown in the lists of them. There are a number of plugins that will add an "ID" column in your admin. Search Wordpress.org for "show ids" and pick one you like. Install it. Activate it. You'll see the ids.
https://wordpress.org/plugins/search.php?q=show+id

Related

Pull in previous and next post featured images into single.php

My previous and next posts are set on an infinite loop so there is always a previous and next post. I.E. for the newest article the "next" post would be go to the oldest post. And if we're viewing the oldest post, the "previous" post would go to the newest post.
I need to pull in the featured images of the next and previous posts to be the background for the pagination navigation.
I've got the images pulling in when there truly is a next or previous post. However, when the infinite loop is necessary, i.e. next being the oldest post, or previous being the newest post, my code breaks down.
My code is below. I believe the issue is that I'm not actually capturing the ID of what I need in $first_id and $last_id. I've tried using get_queried_object_id() $first->post->ID and various combinations to get the post ID of what is being pulled in since .previous-title is working correctly.
<div class="post_nav">
<?php if( get_adjacent_post(false, '', true) ) :
$previous_post = get_previous_post(); ?>
<div class="previous-img"><?php echo get_the_post_thumbnail( $previous_post->ID );?></div>
<p class="previous-title"><?php previous_post_link('Previous Story<br> %link','%title');?</p>
<?php else:
$first = new WP_Query('posts_per_page=1&order=DESC'); $first->the_post();
$first_id = $first->ID; ?>
<div class="previous-img"><?php echo get_the_post_thumbnail( $first_id );?></div>
<?php echo '<p class="previous-title">Previous Story<br>' . the_title() . '</p>';
wp_reset_query(); ?>
<?php endif; ?>
<?php if( get_adjacent_post(false, '', false) ) :
$next_post = get_next_post(); ?>
<div class="next-img"><?php echo get_the_post_thumbnail( $next_post->ID ); ?></div>
<p class="next-title"><?php next_post_link('Next Story<br> %link','%title');?></p>
<?php else:
$last = new WP_Query('posts_per_page=1&order=ASC'); $last->the_post();
$last_id = $last->ID; ?>
<div class="next-img"><?php echo get_the_post_thumbnail( $last_id );?></div>
<?php echo '<p class="next-title">Next Story<br>' . the_title() . '</p>';
wp_reset_query(); ?>
<?php endif; ?>
You're right, $first and $last are not posts, but queries. You could either use get_the_ID() or grab the ID directly.
$last_id = get_the_ID();
( since you've made it the active loop )
or
$last_id = $last->posts[0]->ID;
which means you wouldn't need to set up the loop for $last, in which case you'd also need to get the permalink and title via functions taking the ID as a parameter. This is a minor optimization, if you get confused here go with the first option.
Also, your next-title and previous-title in the else paths seem to be switched with each other. Or perhaps the entire code blocks are.

Escape Html Text in ZF2

I have a text like this:
<p><strong>Lorem</strong> ipsur </p>
To see a text without html tags in Zend Framework 2 how can I do?
I tried to put:
<?php echo $this->escapehtml($array['text']); ?>
I read that for security is not good to do:
<?php echo $array['descrizione']; ?>
Im not sure if i understand the question, since i dont know the contents of your arrays.
basicaly if you want to scape the contents of a variable, let's say, $input, you have to call, as you mentioned
$this>escapeHtml($input)
Actually, the PhpRenderer includes a selection of helpers you can use for this purpose: EscapeHtml, EscapeHtmlAttr, EscapeJs, EscapeCss, and EscapeUrl.
You can read about this here
Also, if you want more control, you can use Zend\Escaper, that in 2 lines of code allow you to escape html, like this
$escaper = new Zend\Escaper\Escaper('utf-8');
$output = $escaper->escapeHtml($input);
or escape attributes, like this
$escaper = new Zend\Escaper\Escaper('utf-8');
$output = $escaper->escapeHtmlAttr($input);
I recommend you read the 3 links, they are very short and would give you a better undestanding of what you are doing.
At this moment there is no option in escapeHtml to allow certain tags. What you have to do in these specific cases is to modify the returned value:
<?php
$str = $this->escapehtml($array['text']); ?>
$str = preg_replace(
array('#href="(.*)"#', '#<(/?(?:pre|a|b|br|em|u|ul|li|ol|p|strong)(\shref=".*")?/?)>#' ),
array( 'href="\1"', '<\1>' ),
$str
);
echo $str;
You can add/remove tags from the part pre|a|b|br|em|u|ul|li|ol|p|strong to your needs. Also this code will allow anchor tags with href only.

How to know if a link is active in a view script without using Zend\Mvc\Navigation ? ZF2

I'm using Zend Framework 2.
I'd like test if a link is the active page in my view script.
For example :
on my view script : view/application/account/user.phtml
<a href="$this->url("account")"
<?echo **if($this->isActive()**){?> class="active"<?php } ?>link</a>
I don't want to set menu with Zend\Mvc\Navigation
Thank you all.
You could do like this:
<?php $current_url = $this->url(); ?>
<a href="<?php echo $this->url('account'); ?>" <?php if ($current_url == $this->url('account')) { echo 'class="active"'; } ?>>link</a>

How to display a sfWidgetFormSelectRadio item without showing the label

I am using Symfony 1.3.6 on Ubuntu 10.0.4.
I am using the sfWidgetFormSelectRadio to allow a user to select a picture from a list, in a form.
In the action, the pictures are set up like this:
$this->form->setWidget('chosenpic', new sfWidgetFormSelectRadio(array(
'choices' => $this->pictures,
'default' => $this->pictures[count($this->pictures)-1] ))
);
In the template, the widget is displayed like this:
<?php echo $form['chosenpic']->render(array('id'=>'check'.($i+1), 'value'=> ($i+1), 'width' => "80", 'height' => "80")); ?>
This generates the following output:
<ul class="radio_list"><li><input type="radio" width="80" height="80" id="check1" value="1" name="flowers[chosenpic]"> <label for="flowers_chosenpic_0">http://example.com/media/images/48408000/jpg/_48408794_009829449-1.jpg</label></li></ul>
I do no want the the label for appearing, as it messes up the form. Short of manually generating the HTML myself (using the form and widget names, is there a way that I can prevent the widget from displaying a 'label for' ?
You need to provide a custom formatter. In your form's configure function, do the following:
public function configure()
{
$this->setWidget('chosenpic', new sfWidgetFormSelectRadio(array(
'formatter' => array($this, 'pictureRadioFormatterCallback')
)));
}
Then, add a formatter that doesn't render labels to your form:
public function pictureRadioFormatterCallback($widget, $inputs)
{
$rows = array();
foreach ($inputs as $input)
{
$rows[] = $widget->renderContentTag('li', $input['input']);
}
return !$rows ?
'' :
$widget->renderContentTag('ul', implode($widget->getOption('separator'), $rows), array('class' => $widget->getOption('class')));
}
However, ideally, you should be using the labels to display the images, rather than displaying the images outside of the labels. Also, it's typically a bad idea to be configuring widgets inside of an action, can that code go in it's own form?
You can do the above -- or use CSS to hide the label generated by default ... something along the lines of:
ul.radio_list li input label { display: none; }
the form itself probably has an #id to help you better specify the CSS selector better. But if you like adding boilerplate PHP code to your app feel free :)

Symfony: blank page after executing include_component()

i have just this line in a template:
<?php include_component('sfGuardRegister', 'register') ?>
when it's executed i get a blank page. I have debugged include_component() and the flow processing is not entering inside of it, i mean:
function include_component($moduleName, $componentName, $vars = array())
{
die("enter");
echo get_component($moduleName, $componentName, $vars);
}
Any idea?
Have you loaded PartialHelper?
sfContext::getInstance()->getConfiguration()->loadHelper('Partial');
If you are in production environment (index.php) blank pages will show on error. Change to development environment (frontend_dev.php) and it will tell you why there is a problem.
My bet is you haven't enabled sfGuardRegister in setting.yml:
all:
enabled_modules: [.... , sfGuardRegister]

Resources