How to get_field() in posts loop in WordPress? - wordpress

I am trying to create a function for a shortcode to loop through an 'articles' custom post type. This is essentially a downloadable file uploaded by users and is a custom post type without the content field/editor box in the post editor. This custom post type also has a 'download_link' ACF field attached to it which is the location of the file for downloads. This is the href attribute for each post in the feed. However, the field is not generating any output in the function below. However the title is being populated. I know that all the article posts have a value provided for this particular field. Am I missing something? Thanks.
// CREATE A FEED OF ARTICLES WITH LINKS
function articles_download_feed(){
$articles_query = new WP_Query(array('post_type' => 'article',
'post_status' => 'any'));
if( $articles_query->have_posts() ){
$output = '<ul>';
while( $articles_query->have_posts() ){
$articles_query->the_post();
$articleID = get_the_ID();
$title = get_the_title($articleID);
if(get_field('download_link', $articleID)){
$download = get_field('download_link', $articleID, true);
} else {
$download = 'ABC';
}
$output .= '<li><a target="_blank" href="'.$download.'">'.$title.'</a></li>';
}
$output .= '</ul>';
wp_reset_postdata();
return $output;
}
} add_shortcode('articles_feed', 'articles_download_feed');

I managed to fix this issue myself.
I set up the 'dile_dl_link' custom field for 'article' type posts via the add_post_meta() WP function, instead of via Advanced Custom Fields (below is the segment of code to create a new article):
// CREATE NEW ARTICLE LINK FROM GRAVITY FORM SUBMISSION
function article_upload_createlink($entry, $form){
// Fields
$post_title = ucwords($entry[8]);
$post_status = 'publish';
$post_excerpt = $entry[9];
$post_type = 'article';
$dl_link = $entry[3];
// Post being created
$new_article = array(
'post_title' => $post_title,
'post_status' => $post_status,
'post_excerpt' => $post_excerpt,
'post_type' => $post_type,
);
// ID for post
$theId = wp_insert_post($new_article);
// Update download_link
add_post_meta($theId, 'file_dl_link', $dl_link);
} add_action( 'gform_after_submission_7', 'article_upload_createlink', 10, 2 );
The function in question as presented in the question has been changed as seen below:
// CREATE A FEED OF ARTICLES WITH LINKS
function articles_download_feed(){
$articles_query = new WP_Query(array('post_type' => 'article', 'post_status' => 'any'));
if( $articles_query->have_posts() ){
$output = '';
while( $articles_query->have_posts() ){
$articles_query->the_post();
$articleID = get_the_ID();
$title = get_the_title();
$excerpt = get_the_excerpt();
$get_dllink = get_post_meta($articleID, 'file_dl_link', true);
$output .= '<div class="wpb_wrapper">
<div class="vc_empty_space" style="height: 24px">
<span class="vc_empty_space_inner"></span>
</div>
<div class="wpb_text_column ">
<div class="wpb_wrapper">
<h4>'.$title.'</h4>
<p>'.$excerpt.'</p></div>
</div>
<div class="w-btn-wrapper align_left">
<a target="_blank" class="w-btn style_raised color_primary icon_none" href="'.$get_dllink.'"><span class="w-btn-label">DOWNLOAD</span><span class="ripple-container"></span></a>
</div>
</div>
<div class="w-separator type_default size_medium thick_1 style_solid color_border cont_none">
<span class="w-separator-h"></span>
</div>';
}
wp_reset_postdata();
return $output;
}
}
add_shortcode('articles_feed', 'articles_download_feed');

Related

Best way to display group of elements in wordpress

I've just started working on a project with wordpress. And I wonder what is the best practice to show a group of columns like this (highlighted in red)
in the page (post). I have a requirement that person who creating a post can insert default columns with different text and header.
I tried shortcode but it seems too complex. I want something like form with the input fields which will generate the colums. Thanks
I solve this problem with custom post types and a shortcode.
Plugins (optional - you could hand code everything)
Custom Post Types and Custom Fields creator - WCK
Intuitive Custom Post Order
Create your custom post type and fields.
Use Intuitive Custom Post Order to allow users to reorder the posts.
Customize this shortcode template to match your code and requirements:
add_shortcode('cpt-list', 'jpro_cpt_list');
function jpro_cpt_list() {
$args = array (
'post_type' => array( 'cpt' ),
'posts_per_page' => '-1',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$query = new WP_Query( $args );
$cpt_list = '<div class="jpro-cpt cpt-list container">';
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$post = get_post($id);
$postID = $post->ID;
$image = get_the_post_thumbnail( $postID, 'medium', array( 'class' => 'aligncenter' ));
$name = get_the_title($postID);
$bio = apply_filters('the_content', $post->post_content);
if(!empty($image)){
$output_image = '<div class="cpt-list image">'.$image.'</div>';
} else {
$output_image = '';
}
if(!empty($name)){
$output_name ='<h3 class="cpt-name cpt-list">'.$name.'</h3>';
} else {
$output_name = '';
}
if(!empty($bio)){
$output_bio ='<div class="cpt-bio cpt-list">'.$bio.'</div>';
} else {
$output_bio = '';
}
$cpt_list .= '<section class="jpro-cpt cpt-list columnClass">';
$cpt_list .= $output_image;
$cpt_list .= $output_name;
$cpt_list .= $output_bio;
$cpt_list .= '</section>';
}
} else {
$cpt_list .='<p>Ooops! No cpt Found.</p>';
}
$cpt_list .='</div>';
wp_reset_postdata();
return $cpt_list;
}

Wordpress - Custom plugin to return related posts by category

I'm learning how to create custom plugins for Wordpress, I am trying to get related posts by category.
The problem is, I am returning all posts regardless of it's category whether it's the same category or not.
I've done a var_dump on the $categoriesIds[] and it is pulling the right category for each post.
I'm guessing something is not right with the WP_Query?
Can someone point out what is missing with the code?
function Add_related_posts($content) {
// If it's not a singular post, return the content
if (!is_singular('post')) {
return $content;
}
// Get post categories
$categories = get_the_terms(get_the_ID(), 'category');
$categoriesIds = [];
foreach ($categories as $category) {
$categoriesIds[] = $category->term_id;
}
$loop = new WP_Query(array(
'category_in' => $categoriesIds,
'posts_per_page' => 4,
'post_not_in' => array(get_the_ID()),
'orderby' => 'rand'
));
// If there are posts
if ($loop->have_posts()) {
$content .= 'RELATED POSTS:<br><ul>';
while ($loop->have_posts()) {
$loop->the_post();
$content .= '<li>' . get_the_title() . '</li>';
}
}
$content .= '</ul>';
// Restore data
wp_reset_query();
return $content;
}
Categories are pulled as expected. But in argument in WP_Query, you got one problem.
It should be category__in, NOT category_in.
Try this:
'category__in' => $categoriesIds,
See documentation: http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

WP query looping through custom post type working on homepage but not on search page

I've got two custom post types called Artists and Paintings. Both the post types have a custom field called artist name created using Advanced Custom Fields plugin. I need to be able to match the custom fields from both these post types, with each other, in order to display more information.
Pasting below only the args and loop from the query. Will post more of the code if it is necessary.
<?php
$artist_name = get_field('artist');
$args = array(
'post_type' => 'artists',
'meta_value' => $artist_name
);
$query_artist = new WP_Query( $args );
if ( $query_artist->have_posts() ) {
while ( $query_artist->have_posts() ) {
$query_artist->the_post(); ?>
<p class="artist-name"><?php the_title(); ?></p>
<?php }
} else {
echo 'Artist not found';
}
wp_reset_postdata(); ?>
This code works properly when in the template file for the homepage but always prints out 'Artist not found' when in the search results page. I copied this code directly from the homepage template so spelling mistakes are not the problem. Been breaking my head over this for a long time. Will anyone reading this have a clue on what's happening?
Thanks.
Ok, so I've managed to finally get what I want to work but I'm not sure why the below code worked and my original didn't since both of them are similar methods to do a new query.
Here's the code if anyone else had the same problem:
<?php
// Permalink for artist
$artist_name = get_field('artist');
global $post;
$posts = get_posts( array( 'post_type' => 'artists', 'meta_value' => $artist_name ) );
if( $posts ):
foreach( $posts as $post ) :
setup_postdata($post); ?>
<p class="artist-name"><?php the_title(); ?></p>
<?php endforeach;
wp_reset_postdata();
endif; ?>
I think that wordpress doesn't include custom types in the search automaticaly.
You can use a plugin like https://wordpress.org/plugins/advanced-custom-post-search/ or write your own function in the functions.php
function rc_add_cpts_to_search($query) {
// Check to verify it's search page
if( is_search() ) {
// Get post types
$post_types = get_post_types(array('public' => true, 'exclude_from_search' => false), 'objects');
$searchable_types = array();
// Add available post types
if( $post_types ) {
foreach( $post_types as $type) {
$searchable_types[] = $type->name;
}
}
$query->set( 'post_type', $searchable_types );
}
return $query;
}
add_action( 'pre_get_posts', 'rc_add_cpts_to_search' );
Example from http://www.remicorson.com/include-all-your-wordpress-custom-post-types-in-search/

How to add custom post type archive to menu

I have been searching for weeks and I still haven't found a proper solution to this problem.
I am writing a Wordpress Theme. I have a custom post type called Works. I would like to add my Works archive to my menu and have it as well as it's posts highlighted when I access them.
I can access my archive and posts on the following links
Works archive: /works/
Works single post: /works/postname/
My solution so fare have been to name my archive-works.php template file with a template name (Work archive). Then create an empty page using that template and adding the page to the menu. This highlights the archive in the menu but not the single posts.
I can easily solve this with a custom link and some javascript but there must be a better and cleaner way.
You can do a simple trick in your functions.php:
add_filter('nav_menu_css_class', 'current_type_nav_class', 10, 2);
function current_type_nav_class($classes, $item) {
// Get post_type for this post
$post_type = get_query_var('post_type');
// Go to Menus and add a menu class named: {custom-post-type}-menu-item
// This adds a 'current_page_parent' class to the parent menu item
if( in_array( $post_type.'-menu-item', $classes ) )
array_push($classes, 'current_page_parent');
return $classes;
}
In your case, you just have to add a class 'works-menu-item' with that archive menu item by the admin panel;
To add "custom posttype archive link" to menu, please look at the following guide
Open file functions.php , and enter code below
add_action('admin_head-nav-menus.php', 'wpclean_add_metabox_menu_posttype_archive');
function wpclean_add_metabox_menu_posttype_archive() {
add_meta_box('wpclean-metabox-nav-menu-posttype', __('Custom Post Type Archives'), 'wpclean_metabox_menu_posttype_archive', 'nav-menus', 'side', 'default');
}
function wpclean_metabox_menu_posttype_archive() {
$post_types = get_post_types(array('show_in_nav_menus' => true, 'has_archive' => true), 'object');
if ($post_types) :
$items = array();
$loop_index = 999999;
foreach ($post_types as $post_type) {
$item = new stdClass();
$loop_index++;
$item->object_id = $loop_index;
$item->db_id = 0;
$item->object = 'post_type_' . $post_type->query_var;
$item->menu_item_parent = 0;
$item->type = 'custom';
$item->title = $post_type->labels->name;
$item->url = get_post_type_archive_link($post_type->query_var);
$item->target = '';
$item->attr_title = '';
$item->classes = array();
$item->xfn = '';
$items[] = $item;
}
$walker = new Walker_Nav_Menu_Checklist(array());
echo '<div id="posttype-archive" class="posttypediv">';
echo '<div id="tabs-panel-posttype-archive" class="tabs-panel tabs-panel-active">';
echo '<ul id="posttype-archive-checklist" class="categorychecklist form-no-clear">';
echo walk_nav_menu_tree(array_map('wp_setup_nav_menu_item', $items), 0, (object) array('walker' => $walker));
echo '</ul>';
echo '</div>';
echo '</div>';
echo '<p class="button-controls">';
echo '<span class="add-to-menu">';
echo '<input type="submit"' . disabled(1, 0) . ' class="button-secondary submit-add-to-menu right" value="' . __('Add to Menu') . '" name="add-posttype-archive-menu-item" id="submit-posttype-archive" />';
echo '<span class="spinner"></span>';
echo '</span>';
echo '</p>';
endif;
}
Thanks
Thanks to rasmussvanejensen for her/his nice question and thethangtran for the answer, I am still confused why Wordpress has not yet added such a good feature to its code base by default.
By the way I think there is even a better solution than the one provided by thethangtran, as it may break on some situations.
Note 1
According to the Codex, using register_post_type, one can add extra post_types to the installation. There is chance, some one need to change the 'query_var' and thus the provided code will break.
Note 2
In addition, it may not handle the current-menu-item class, which will be used for css customization to show the menu item as active.
Note 3
As another note on the code, there is no need to define the loop_index, item and items variables. they are absolutely useless.
A better solution
So I suggest using this alternative, for those who want a more robust solution to on this:
function prefix_add_metabox_menu_posttype_archive(){
add_meta_box( 'prefix_metabox_menu_posttype_archive', __( 'Archives' ), 'prefix_metabox_menu_posttype_archive', 'nav-menus', 'side', 'default' );
}
add_action( 'admin_head-nav-menus.php', 'prefix_add_metabox_menu_posttype_archive' );
function prefix_metabox_menu_posttype_archive(){
$post_types = get_post_types( array( 'show_in_nav_menus' => true, 'has_archive' => true ), 'object' );
if( $post_types ){
foreach( $post_types as $post_type ){
$post_type->classes = array( $post_type->name );
$post_type->type = $post_type->name;
$post_type->object_id = $post_type->name;
$post_type->title = $post_type->labels->name;
$post_type->object = 'cpt_archive';
}
$walker = new Walker_Nav_Menu_Checklist( array() );?>
<div id="cpt-archive" class="posttypediv">
<div id="tabs-panel-cpt-archive" class="tabs-panel tabs-panel-active">
<ul id="ctp-archive-checklist" class="categorychecklist form-no-clear"><?php
echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $post_types ), 0, (object) array( 'walker' => $walker ) );?>
</ul>
</div>
</div>
<p class="button-controls">
<span class="add-to-menu">
<input type="submit"<?php disabled( $nav_menu_selected_id, 0 ); ?> class="button-secondary submit-add-to-menu" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-ctp-archive-menu-item" id="submit-cpt-archive" />
</span>
</p><?php
}
}
function prefix_cpt_archive_menu_filter( $items, $menu, $args ){
foreach( $items as &$item ){
if( $item->object != 'cpt_archive' ) continue;
$item->url = get_post_type_archive_link( $item->type );
if( get_query_var( 'post_type' ) == $item->type ){
$item->classes []= 'current-menu-item';
$item->current = true;
}
}
return $items;
}
add_filter( 'wp_get_nav_menu_items', 'prefix_cpt_archive_menu_filter', 10, 3 );
Navigate to Appearance > Menus;
Make sure you have the Works' custom post type selected at Screen Options;
Click on the name of your custom post type to expand it and then click on the ‘View all’ tab;
You will see an option for All Works. Check the box next to it and then click on the Add to Menu button;
Your custom post type archive will now appear as a menu item in the right column;
By default, the label will be "All Works". You can change this by writing something different at the Navigation Label;
Click on the Save Menu button to save your changes.
I found a solution on GitHub and it works out of the box by simply adding it to the functions.php without any walkers or extra classes.
This code solves it by comparing the slug of the current post type
with the navigation items, and adds a class accordingly.
Code on GitHub

Drupal, Ubercart, products templates (prices templates)

I want to change the template of my Ubercart products. More in detail, I want to add the label "From..." in front of each price. But I cannot find the template to do it.
I'm also using theme developer module and this is what I get:
zen_uc_product_price <
phptemplate_uc_product_price <
theme_uc_product_price
But I cannot find such files.
thanks
You can also make a node-product.tpl.php file, if so this is the way to get prices:
<?php
$context = array(
'type' => 'product',
'revision' => 'altered', // using altered to get the bare price with no themeing
'field' => 'sell_price',
'subject' => array('node' => $node)
);
$dp = uc_price($node->sell_price, $context);
$context['field'] = 'list_price';
$lp = uc_price($node->list_price, $context);
?>
<div class="price clear-block <?php if($dp != $lp) print 'discounted'; ?>">
<?php print 'From: ' . $node->content['display_price']['#value']; ?>
<?php //print $node->content['list_price']['#value']; ?>
</div>
This became a bit more than it should be:
Use:
content['display_price']['#value']; ?>
Unless you want to theme discounted pricing :-)
Just copied out from one of my projects.
Last: you can probably use theme_uc_product_price:
you add a function in template.php (Pasting in default implementation from uc_product.module
function zen_uc_product_price($price, $context, $options = array()) {
$output = '<div class="product-info '. implode(' ', (array)$context['class']) .'">';
$output .= uc_price($price, $context, $options);
$output .= '</div>';
return $output;
}
Inspect the $context variable for when to add the "From" part.
To add a prefix or suffix to the price, you can go to
/admin/store/settings/store/edit/format

Resources