I want to add text "Genre:" to the beginning of this custom field
<?php the_field('im_genre'); ?>
the reason that i dont simply add something like this
<li>Genre:<?php the_field('im_genre'); ?> </li>
is that i dont want to this text be shown when there isnt this field for a post
You can check if the field exists and then display your HTML element.
<?php if( get_field('im_genre') ): ?>
<li>Genre:<?php the_field('im_genre'); ?> </li>
<?php endif; ?>
I believe you can do it by using the "acf/format_value" hook.
add_filter('acf/format_value', 'reFormatValue', 20, 3);
function reFormatValue($value, $post_id, $field) {
$data = [
// list of field keys to format
'field_XXXXXXX' => 'XXX:',
'im_genre' => 'Genre:',
// etc...
];
$keys = array_keys($data);
if (in_array($field['key'], $keys)) {
$value = $keys[$field['key']] . $value;
}
return $value;
}
add this to your functions.php (active theme folder) or any active plugins
Related
I am trying to fetch data from another custom post type books into the readers. I have written the code and the data shows up.
The challenge is that I want to check the echoed data $c[0] against the title of the current page.
Whenever I call the get_the_title() function I am instead getting the title of the other custom post type books. So there is no way I can add an if like so:
if (get_the_title()===$c[0]) {
//The echo code - see echo part of my code
}
When I echo the code, it goes into the top part of the page instead of inside the content area. I am suspecting this is the reason why I can't get the page title. I have tried researching on hooking to the content area and tried but I instead get a bunch of errors.
<?php
$posts = get_posts(array(
'numberposts' => 10,
'post_type' => 'fixtures-results'
));
if($posts)
{
foreach($posts as $post)
{
setup_postdata( $post );
$matches = str_replace("|% ","|%",get_field('fixtures_results'));
preg_match_all("#[A-z0-9 ./'áàâäãåǻăāąấấặắǎȧạàãáćĉċčççďđḑḋḍdéèêëĕěēęėềẽȩẹéĝ
ğġģǧǵḡĥħȟḣḥḩíìîïĭїĩīįǐĪỊ̣iỊǏĨị̣ịıíĵķǩḱḳļľŀĺĺóòôöõðőŏōỏǒǫȯoøồớòóõöñńņňʼnŋǹṅṇ
ŕŗřṙṛśŝşšṡṣşșţťŧțṫṭtúùûüůūűųũŭǔǘǚǜụuưüúŵŷýÿўỹẏỳỵyẙȳẙźżžẑẓḅḟḿṁṃṕṗṽṿḷẁẃẅẉẘ
ẘẋẍÒÓÔÕÖŐǑǪŌȮØÙÚÛǓǗǙǛŨŮŰŲÜỤŪĆĈČÇÀÁÂÃÄÅẠĄĀȦǍÈÉÊËĖĘĚẸẼĒȨḾṀṂŚŜŞŠṠṢŹŻŽẐẒŃŅŇ
ṄṆÑǸḢḤḨĤȞĹĻĽĿŢŤṪṬŖŘṘṚĜĠĢǦǴḠẀẂẄẈŴḊḌḐĎĐÐṔṖĶǨḰḲẊẌṼṾỴỸỲẎŸȲŶÝĴȷḄḞḶŁłÏÎÍÌıİ
ßıs-]+#[^#]+#", $matches, $countries);
$results = [];
foreach ($countries[0] as $c) {
$c = explode("#", $c);
$country_name = $c[0];
$results[$country_name] = [];
$params = explode("%", $c[1]);
foreach ($params as $key) {
if (!empty($key)) {
$test = explode("|", trim($key, '|'));
echo get_the_title($postID);
//echo $c[0] . $test[0] . $test[1] . $test[2].'<br>';
}
}
}
}
wp_reset_postdata();
}
?>
I have tried but can't get past that point. Thanks.
I'm a PHP beginner working with Wordpress, and I'm trying to retrieve data from custom fields (using Custom Field Suite) from a post to be displayed on a different page.
I have tried a few different ways, and found two methods that work. But as I'm a beginner, I wonder if these methods are 'proper'?
This is one solution I found, but it's not exactly elegant:
// Method 1
$current = CFS()->get( 'get_current' ); //retrieves the post ID from a custom field on the page
$custom_fields = get_post_custom($current);
$my_custom_field = $custom_fields['current_title'];
$my_custom_field2 = $custom_fields['current_artist'];
foreach ( $my_custom_field as $key) {
}
foreach ( $my_custom_field2 as $key2) {
}
echo '<h2>'.$key.'</h2>';
echo '<h1>'.$key2.'</h1>';
I tried to re-write it like this, but nothing is displayed - not sure what's wrong with this loop:
// Method 2
$current = CFS()->get( 'get_current' );
$custom_fields = get_post_custom($current);
foreach ( $custom_fields as $key) {
echo '<h2>'.$key['current_title'].'</h2>';
echo '<h1>'.$key['current_artist'].'</h1>';
}
As method 2 wasn't working, I tried something else and found that this also works (I added the loop here based on this answer: https://stackoverflow.com/a/19918170/5483154):
// Method 3
<?php while ( have_posts() ) : the_post();
$current = CFS()->get( 'get_current' );
$currentitle = get_post_meta($current, 'current_title', true);
$artistname = get_post_meta($current, 'current_artist', true);
echo '<h2>'.$currentitle.'</h2>';
echo '<h1>'.$artistname.'</h1>';
endwhile;
wp_reset_query();
?>
Method 3 seems best to me. Is this a good way of approaching this problem? And if anyone would be kind enough to explain what's wrong with method 2, I would also appreciate it. Thanks
Looking at the Custom Field Suite documentation here: http://customfieldsuite.com/api/get.html
It looks like you should be using the 'get' method:
CFS()->get( $field_name, $post_id, $options );
Eg.
$current = CFS()->get( 'get_current' );
echo '<h2>' . CFS()->get( 'current_title', $current ) . '</h2>';
echo '<h1>' . CFS()->get( 'current_artist', $current ) . '</h1>';
Ps. Don't forget to watch the order of your heading tags (<h1> should come before <h2>)
In the case of your "method 2": what if you try rewriting it something like this?
// Method 2
$current = CFS()->get('get_current');
$custom_fields = get_post_custom($current);
foreach ($custom_fields as $field) {
foreach ($field as $key => $value) {
echo $key . ': ' . $value;
}
}
EDIT: I adjusted my parameters to insert the logo directly into the menu, instead of padding a specific menu item. The padding method can easily drive a centered menu off-center. This insertion method should resolve that issue.
I'm working on a theme, and want to create a menu split by a logo. I realize I could just create two menus, but I want this to be as streamlined for the user as possible. I've already been able to get the number of items and target the menu item I want, but I'm not sure how to use my functions.php file to add the class "pad-item" to the <li>.
Here is what I have to find and target the specified item. All it's returning, though, is an index number of top level items.
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object($locations['primary']);
$items = wp_get_nav_menu_items($menu->term_id);
$top_level = 0;
foreach ($items as $val) {
if ($val->menu_item_parent === '0') {
$top_level++;
}
}
$index = round($top_level / 2) - 1;
return $index;
Any help would be greatly appreciated. Thanks.
I was able to figure out the issue, and I wanted to post my solution in case someone else was looking for the same answer.
function main( $items, $args ) {
// Checks to see if the menu passed in is the primary one, and creates the logo item for it
if ( $args->theme_location == 'primary' ) {
$logo_item = '<li class="menu-item">' . get_logo() . '</li>';
}
//Gets the location of the menu element I want to insert the logo before
$index = round( count_top_lvl_items() / 2 ) + 1;
//Gets the menu item I want to insert the logo before
$menu_item = get_menu_item( $index );
$insert_before = '<li id="menu-item-' . $menu_item->ID;
$menu_update = substr_replace( $items, $logo_item, strpos( $items, $insert_before ), 0 );
return $new_menu;
}
//Counts the number of top level items in the menu
function count_top_lvl_items() {
$items = get_menu_items();
$counter = 0;
foreach ( $items as $val ) {
if ( $val->menu_item_parent === '0' ) {
$counter++;
{
return $counter;
}
//Returns the menu item to insert the logo before
function get_menu_item( $index ) {
$items = get_menu_items();
$counter = 0;
foreach ( $items as $val ) {
if ( $val->menu_item_parent === '0' ) {
$counter++;
}
if ( $counter == $index ) {
return $val;
}
}
}
//Returns the logo menu item. I have it separated because my theme allows for varied logos
function get_logo() {
$home = get_option( 'home' );
$logo = get_option( 'logo' );
$logo_item = <<<EOD
<div id="logo">
<a href="$home">
<img src="$logo" id="logo-img" alt=""/>
</a>
</div>
EOD;
return $logo_item;
}
function get_menu_items() {
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations['primary'] );
$items = wp_get_nav_menu_items( $menu );
return $items;
}
Please feel free to tell me if I missed something, or if this could be done in a different way.
Thanks!
Your function name might be used by other plugins or themes and may cause a problem.
I suggest you either change the function name or use function_exists within if else statement.
Here is the manual > http://php.net/manual/bg/function.function-exists.php
Quick suggestion:
`
if(!function_exists('main'){
function main(){
$do_stuff = 'Currently doing';
return $do_stuff;
}
}
`
I am using the theme Page Builder Framework, then when you customize the appearance you can choose a centered menu where the logo is in the middle and the menu is split.
I'm using Wordpress custom post field for my products in Woocommerce.
I've created a custom field called 'phone_model' and it will contain strings like 'iPhone 6 Plus' etc.
In my child theme functions.php I've added this function:
function wc_phone_model(){
$custom_fields = get_post_custom($post_id);
$phone_model = $custom_fields['phone_model'];
if (is_array($phone_model)) {
foreach ( $phone_model as $key => $value ) {
echo $value;
}
}
}
It's working as expected. It will show the given value in the custom post field.
In my Woocommerce title loop title.php, I've added this line:
<div class="phone_model_wrap"><span class="phone_model"><?php wc_phone_model() ?></span></div>
How do I make this show only if there is something to show from the custom post field?
Thanks in advance guys!
Cheers Kenn
I know there is a stigma against echoing html, but in this case, I can't think of a better way. I'd recommend something like this:
function wc_phone_model(){
$custom_fields = get_post_custom($post_id);
$phone_model = $custom_fields['phone_model'];
if (is_array($phone_model)) {
echo '<div class="phone_model_wrap"><span class="phone_model">';
foreach ( $phone_model as $key => $value ) {
echo $value;
}
echo '</span></div>';
}
}
Then in your title.php, its just a matter of calling the function.
<?php wc_phone_model() ?>
If someone has a clean solution without echoing html though, I'd be interested in seeing it!
#kunruh
I have faced another problem with your solution.
I have made a new function to be used elsewhere with another class etc.
function wc_phone_model_single_product(){
$custom_fields = get_post_custom($post_id);
$phone_model = $custom_fields['phone_model'];
if (is_array($phone_model)) {
echo '<div class="phone_model_wrap"><span class="phone_model_single_product">Til ';
foreach ( $phone_model as $key => $value ) {
echo $value;
}
echo '</span></div>';
}
}
The "Til" will be echoed even if there is nothing in $phone_model.
How do I avoid this?
Cheers Kenn
I am using the cms perch to retrieve data using various functions. I am retrieving product items from various sub pages. The code I have so far is working as it should and can be seen here: http://www.ww.thirdfloordesign.net/products/70mm-couplers/
$raw = perch_pages_navigation(array(
'from-path'=>'*',
'skip-template'=>true
));
if (count($raw)) {
$items = $raw;
} else {
// add else statement to output warning 'no products found in this category' or something
}
foreach($items as $item) {
$product['ID'] = $item['pageNavText'];
$product['title'] = $item['pageTitle'];
$product['path'] = $item['pagePath'];
$product['parent'] = $item['pageSubpagePath'];
PerchSystem::set_vars(array(
'product-id'=>$product['ID'],
'product-title'=>$product['title'],
'product-path'=>$product['path'],
'product-parent'=>$product['parent']
));
$product['image'] = perch_content_custom('Image', array(
'page'=>$product['path'],
'template'=>'product/_image.html',
'sort'=>'_order',
'count'=>1
), true);
PerchSystem::set_var('product-image', $product['image']);
$items[] = perch_content_custom('Product', array(
'page'=>$product['path'],
'template'=>'product/_product.html'
), true);
}
Where the data needs to be outputted I am using the following code and it seems to be working as it should except it is also echoing an array which can be seen on the page. Is it because I am using duplicate $variables / array names? Solutions please
<?php
if (!empty($items)) {
?>
<ul class="product-grid">
<?php
foreach($items as $item) {
echo $item;
}
?>
</ul>
<?php
} else {
?>
<p class="alert alert-warning"><i class="icon-warning-sign"></i>No products found!</p>
<?php } ?>