WordPress.org

Ready to get started?Download WordPress

Forums

Display Posts Shortcode
Not working with custom fields (12 posts)

  1. ebenezer357
    Member
    Posted 1 year ago #

    Hi, im using "Display Posts shortcode" to display my posts using a shortcode. This is working perfectly, but when i place more_fields('myfield') it places the result at the top of the page and in the place where i put it only shows up a "1".

    Does anyone has a clue why this is happening?

    This is the code that im using, by the way im not including the more_fields here because i took it off since i got tired of it after spending 2 days on it with no success.

    [large amount of code moderated - please use the pastebin - http://codex.wordpress.org/Forum_Welcome#Posting_Code ]

    where should i place this code? Again, im using MORE FIELDS which creates custom fields, when i put the more_fields('field') in the OUTPUT variable it displays it at the top.

    Someone please help!

    http://wordpress.org/extend/plugins/display-posts-shortcode/

  2. alchymyth
    Forum Moderator
    Posted 1 year ago #

    anything in a shortcode must not echo or print but only return the results.

    what does more_fields('field') do?
    where is this defined?
    is there an alternative which would 'return' the result?

  3. ebenezer357
    Member
    Posted 1 year ago #

    Hi alchymyth, this is the code of display_posts_shortcode.php file. i added a variable called 'autor' which is being called as 'include_autor" on the shortcode. It works but diaplays the 'autor' of each book at the top of the page below the header and then it puts a '1' where it should be displaying the autor.. Here is the code:

    <?php
    /**
     * Plugin Name: Display Posts Shortcode
     * Plugin URI: http://www.billerickson.net/shortcode-to-display-posts/
     * Description: Display a listing of posts using the [display-posts] shortcode
     * Version: 2.3
     * Author: Bill Erickson
     * Author URI: http://www.billerickson.net
     *
     * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
     * General Public License version 2, as published by the Free Software Foundation.  You may NOT assume
     * that you can use any other version of the GPL.
     *
     * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
     * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     *
     * @package Display Posts
     * @version 2.2
     * @author Bill Erickson <bill@billerickson.net>
     * @copyright Copyright (c) 2011, Bill Erickson
     * @link http://www.billerickson.net/shortcode-to-display-posts/
     * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
     */
    
    /**
     * To Customize, use the following filters:
     *
     * <code>display_posts_shortcode_args</code>
     * For customizing the $args passed to WP_Query
     *
     * <code>display_posts_shortcode_output</code>
     * For customizing the output of individual posts.
     * Example: https://gist.github.com/1175575#file_display_posts_shortcode_output.php
     *
     * <code>display_posts_shortcode_wrapper_open</code>
     * display_posts_shortcode_wrapper_close
     * For customizing the outer markup of the whole listing. By default it is a <ul> but
     * can be changed to <ol> or <div> using the 'wrapper' attribute, or by using this filter.
     * Example: https://gist.github.com/1270278
     */ 
    
    // Create the shortcode
    add_shortcode( 'display-posts', 'be_display_posts_shortcode' );
    function be_display_posts_shortcode( $atts ) {
    
    	// Original Attributes, for filters
    	$original_atts = $atts;
    
    	// Pull in shortcode attributes and set defaults
    	$atts = shortcode_atts( array(
    		'author'              => '',
    		'category'            => '',
    		'date_format'         => '(n/j/Y)',
    		'id'                  => false,
    		'ignore_sticky_posts' => false,
    		'image_size'          => false,
    		'include_content'     => false,
    		'include_date'        => false,
    		'include_excerpt'     => false,
    		'meta_key'            => '',
    		'no_posts_message'    => '',
    		'offset'              => 0,
    		'order'               => 'DESC',
    		'orderby'             => 'date',
    		'post_parent'         => false,
    		'post_status'         => 'publish',
    		'post_type'           => 'post',
    		'posts_per_page'      => '10',
    		'tag'                 => '',
    		'tax_operator'        => 'IN',
    		'tax_term'            => false,
    		'taxonomy'            => false,
    		'wrapper'             => 'ul',
    		'include_autor'       => 'false',
    
    	), $atts );
    
    	$author = sanitize_text_field( $atts['author'] );
    	$category = sanitize_text_field( $atts['category'] );
    	$date_format = sanitize_text_field( $atts['date_format'] );
    	$id = $atts['id']; // Sanitized later as an array of integers
    	$ignore_sticky_posts = (bool) $atts['ignore_sticky_posts'];
    	$image_size = sanitize_key( $atts['image_size'] );
    	$include_content = (bool)$atts['include_content'];
    	$include_date = (bool)$atts['include_date'];
    	$include_excerpt = (bool)$atts['include_excerpt'];
    	$meta_key = sanitize_text_field( $atts['meta_key'] );
    	$no_posts_message = sanitize_text_field( $atts['no_posts_message'] );
    	$offset = intval( $atts['offset'] );
    	$order = sanitize_key( $atts['order'] );
    	$orderby = sanitize_key( $atts['orderby'] );
    	$post_parent = $atts['post_parent']; // Validated later, after check for 'current'
    	$post_status = $atts['post_status']; // Validated later as one of a few values
    	$post_type = sanitize_text_field( $atts['post_type'] );
    	$posts_per_page = intval( $atts['posts_per_page'] );
    	$tag = sanitize_text_field( $atts['tag'] );
    	$tax_operator = $atts['tax_operator']; // Validated later as one of a few values
    	$tax_term = sanitize_text_field( $atts['tax_term'] );
    	$taxonomy = sanitize_key( $atts['taxonomy'] );
    	$wrapper = sanitize_text_field( $atts['wrapper'] );
    	$include_autor = (bool)$atts['include_autor'];
    
    // Set up initial query for post
    	$args = array(
    		'category_name'       => $category,
    		'order'               => $order,
    		'orderby'             => $orderby,
    		'post_type'           => explode( ',', $post_type ),
    		'posts_per_page'      => $posts_per_page,
    		'tag'                 => $tag,
    	);
    
    	// Ignore Sticky Posts
    	if( $ignore_sticky_posts )
    		$args['ignore_sticky_posts'] = true;
    
    	// Meta key (for ordering)
    	if( !empty( $meta_key ) )
    		$args['meta_key'] = $meta_key;
    
    	// If Post IDs
    	if( $id ) {
    		$posts_in = array_map( 'intval', explode( ',', $id ) );
    		$args['post__in'] = $posts_in;
    	}
    
    	// Post Author
    	if( !empty( $author ) )
    		$args['author_name'] = $author;
    
    	// Offset
    	if( !empty( $offset ) )
    		$args['offset'] = $offset;
    
    	// Post Status
    	$post_status = explode( ', ', $post_status );
    	$validated = array();
    	$available = array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash', 'any' );
    	foreach ( $post_status as $unvalidated )
    		if ( in_array( $unvalidated, $available ) )
    			$validated[] = $unvalidated;
    	if( !empty( $validated ) )
    		$args['post_status'] = $validated;
    
    	// If taxonomy attributes, create a taxonomy query
    	if ( !empty( $taxonomy ) && !empty( $tax_term ) ) {
    
    		// Term string to array
    		$tax_term = explode( ', ', $tax_term );
    
    		// Validate operator
    		if( !in_array( $tax_operator, array( 'IN', 'NOT IN', 'AND' ) ) )
    			$tax_operator = 'IN';
    
    		$tax_args = array(
    			'tax_query' => array(
    				array(
    					'taxonomy' => $taxonomy,
    					'field'    => 'slug',
    					'terms'    => $tax_term,
    					'operator' => $tax_operator
    				)
    			)
    		);
    
    		// Check for multiple taxonomy queries
    		$count = 2;
    		$more_tax_queries = false;
    		while(
    			isset( $original_atts['taxonomy_' . $count] ) && !empty( $original_atts['taxonomy_' . $count] ) &&
    			isset( $original_atts['tax_' . $count . '_term'] ) && !empty( $original_atts['tax_' . $count . '_term'] )
    		):
    
    			// Sanitize values
    			$more_tax_queries = true;
    			$taxonomy = sanitize_key( $original_atts['taxonomy_' . $count] );
    	 		$terms = explode( ', ', sanitize_text_field( $original_atts['tax_' . $count . '_term'] ) );
    	 		$tax_operator = isset( $original_atts['tax_' . $count . '_operator'] ) ? $original_atts['tax_' . $count . '_operator'] : 'IN';
    	 		$tax_operator = in_array( $tax_operator, array( 'IN', 'NOT IN', 'AND' ) ) ? $tax_operator : 'IN';
    
    	 		$tax_args['tax_query'][] = array(
    	 			'taxonomy' => $taxonomy,
    	 			'field' => 'slug',
    	 			'terms' => $terms,
    	 			'operator' => $tax_operator
    	 		);
    
    			$count++;
    
    		endwhile;
    
    		if( $more_tax_queries ):
    			$tax_relation = 'AND';
    			if( isset( $original_atts['tax_relation'] ) && in_array( $original_atts['tax_relation'], array( 'AND', 'OR' ) ) )
    				$tax_relation = $original_atts['tax_relation'];
    			$args['tax_query']['relation'] = $tax_relation;
    		endif;
    
    		$args = array_merge( $args, $tax_args );
    	}
    
    	// If post parent attribute, set up parent
    	if( $post_parent ) {
    		if( 'current' == $post_parent ) {
    			global $post;
    			$post_parent = $post->ID;
    		}
    		$args['post_parent'] = intval( $post_parent );
    	}
    
    	// Set up html elements used to wrap the posts.
    	// Default is ul/li, but can also be ol/li and div/div
    	$wrapper_options = array( 'ul', 'ol', 'div' );
    	if( ! in_array( $wrapper, $wrapper_options ) )
    		$wrapper = 'ul';
    	$inner_wrapper = 'div' == $wrapper ? 'div' : 'li';
    
    	$listing = new WP_Query( apply_filters( 'display_posts_shortcode_args', $args, $original_atts ) );
    	if ( ! $listing->have_posts() )
    		return apply_filters( 'display_posts_shortcode_no_results', wpautop( $no_posts_message ) );
    
    	$inner = '';
    	while ( $listing->have_posts() ): $listing->the_post(); global $post;
    
    		$image = $date = $excerpt = $content = '';
    
    		$title = '<a href="' . apply_filters( 'the_permalink', get_permalink() ) . '">' . apply_filters( 'the_title', get_the_title() ) . '</a>';
    
    		if ( $image_size && has_post_thumbnail() )
    			$image = '<a href="' . get_permalink() . '">' . get_the_post_thumbnail( $post->ID, $image_size ) . '</a> ';
    
    		if ( $include_date )
    			$date = ' <span class="date">' . get_the_date( $date_format ) . '</span>';
    
    		if ( $include_excerpt )
    			$excerpt = ' <span class="excerpt">' . get_the_excerpt() . '</span>';
    
    		if( $include_content )
    			$content = '<div class="content">' . apply_filters( 'the_content', get_the_content() ) . '</div>';
    
    		if( $include_autor )
    			$autor = '<div class="content">' . more_fields('autor') . '</div>';
    
    		$class = array( 'listing-item' );
    		$class = apply_filters( 'display_posts_shortcode_post_class', $class, $post, $listing );
    		$output = '<' . $inner_wrapper . ' class="' . implode( ' ', $class ) . '">' . $image . $title . $autor . $date . $excerpt . $content . '</' . $inner_wrapper . '>';
    
    		// If post is set to private, only show to logged in users
    		if( 'private' == get_post_status( $post->ID ) && !current_user_can( 'read_private_posts' ) )
    			$output = '';
    
    		$inner .= apply_filters( 'display_posts_shortcode_output', $output, $original_atts, $image, $title, $date, $excerpt, $inner_wrapper, $content, $class );
    
    	endwhile; wp_reset_postdata();
    
    	$open = apply_filters( 'display_posts_shortcode_wrapper_open', '<' . $wrapper . ' class="display-posts-listing">', $original_atts );
    	$close = apply_filters( 'display_posts_shortcode_wrapper_close', '</' . $wrapper . '>', $original_atts );
    	$return = $open . $inner . $close;
    
    	return $return;
    }

    Hope you can help me PLEASE , thanks in advance!

  4. alchymyth
    Forum Moderator
    Posted 1 year ago #

    this is the line you might need to edit:

    $autor = '<div class="content">' . more_fields('autor') . '</div>';

    where is the function more_fields() defined?
    this has to be a function which returns the result - it must not be a function which echos or prints the result.

  5. ebenezer357
    Member
    Posted 1 year ago #

    so, where or how can i make a function for this? should i create the function inside this php file?

    should i write on this php file this:

    function autor()
    {
    more_fields('autor');
    }

  6. alchymyth
    Forum Moderator
    Posted 1 year ago #

    so, where or how can i make a function for this? should i create the function inside this php file?

    no

    this function is from your other plugin - some 'more fields' plugin ??

    find out where the function gets defined in the plugin files, and find out what it does; and find out if there is an equivalent function which would return the result.

    the function, the way you are using it now, seems to be unsuitable for what you are trying to do.

    http://php.net/manual/en/function.return.php
    http://php.net/manual/en/function.echo.php

    http://codex.wordpress.org/Shortcode_API

  7. ebenezer357
    Member
    Posted 1 year ago #

    Well, this si the code form the more fields plugin:

    <?php
    /*
    Plugin Name: More Fields
    Version: 2.1
    Author URI: http://more-plugins.se/
    Plugin URI: http://more-plugins.se/plugins/more-fields/
    Description:  Add more input boxes to use on the write/edit page.
    Author: Henrik Melin, Kal Ström
    License: GPL2
    
    	USAGE:
    
    	See http://more-plugins.se/plugins/more-fields/
    
    	Copyright (C) 2010  Henrik Melin, Kal Ström
    
    	This program is free software; you can redistribute it and/or
    	modify it under the terms of the GNU General Public License
    	as published by the Free Software Foundation; either version 2
    	of the License, or (at your option) any later version.
    
    	This program is distributed in the hope that it will be useful,
    	but WITHOUT ANY WARRANTY; without even the implied warranty of
    	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    	GNU General Public License for more details.
    
    	You should have received a copy of the GNU General Public License
    	along with this program; if not, write to the Free Software
    	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    
    */
    // Reset More Fields
    if (0) update_option('more_fields', array());
    
    // Plugin settings
    $settings = array(
    	'name' => 'More Fields',
    	'option_key' => 'more_fields',
    	'fields' => array(),
    	'default' => array(),
    	'file' => __FILE__,
    );
    
    // Always on components
    if (!defined('MORE_PLUGINS_DEV')) include('more-plugins/more-plugins.php');
    else include(ABSPATH . '/wp-content/plugins/more-plugins.php');
    include('more-fields-object.php');
    include('more-fields-field-types.php');
    include('more-fields-rewrite-object.php');
    include('more-fields-template-functions.php');
    
    $more_fields = new more_fields_object($settings);
    
    // Load admin components
    if (is_admin()) {
    	if (!defined('MORE_PLUGINS_DEV')) include('more-plugins/more-plugins-admin.php');
    	else include(ABSPATH . '/wp-content/plugins/more-plugins-admin.php');
    
    	include('more-fields-settings-object.php');
    	$more_fields_settings = new more_fields_admin($settings);
    }
    
    ?>

    there is something happening that where ever i put more_fields('autor') on the display_posts_shortcode.php it displays it all the way at the top. I hacked the code and created a table in order to change how it looks and i typed more_fields('autor') inside on of the cells in the table and it showed up my custom field exactly where i put it; so i guess it's something wrong with the display_posts_shortcode.php that puts this all the way to the top.

    Do have any idea why this is happening?

  8. ebenezer357
    Member
    Posted 1 year ago #

    the thing is that the only way to display my custom field is by typing more_fields('custom_field') but no matter where i put it it shows up at the freaking top of the page.
    This is so frustrating! I have spent 4 days on this issue!

  9. alchymyth
    Forum Moderator
    Posted 1 year ago #

    there is one way to do it: using output buffering (extreme measures only because the more-fields plugin has no provisions to return the output);

    try and change this line:

    $autor = '<div class="content">' . more_fields('autor') . '</div>';

    to:

    ob_start(); more_fields('autor'); $author_field = ob_get_contents(); ob_end_clean();
    $autor = '<div class="content">' . $author_field . '</div>';
  10. ebenezer357
    Member
    Posted 1 year ago #

    Wow alchymyth! You are great!!!!! i can't believe you actually solved my issue.
    After 4 days on this you found my solution. It works perfect.

    ob_start(); more_fields('autor'); $author_field = ob_get_contents(); ob_end_clean();
    $autor = '<div class="content">' . $author_field . '</div>';

    works perfect!!!!
    I have a last question just in case you can help me out on this area for the last time:

    I want to add a link to this autor field that i just did. For example:

    book 1 has two authors: jose and juan so it's displaying them using the "output buffering" tip that you just gave me. I want my clients that when they click on juan they are directed to http://www.mysite.com/juan and when they click on jose they are directed to http://www.mysite.com/jose by a link..
    I saw once that i could do something like http://www.mysite.com/%autor% or something like this.

    Is this possible to do? i want to do this because im displaying the authors of these books and i want my clients to also be able to click on the author and see their biography.

    Please help me out this last time if you can. If you can't just tell me and i'll understand don't worry.

    Thanks a lot in advance!

  11. alchymyth
    Forum Moderator
    Posted 1 year ago #

    the problem with your last idea is that if the names do not have matching author archives/pages, then the client would get a 404 error;

    if it is quite sure that the name is linked to the author's information like http://www.mysite.com/juan, you can use (untested):

    ob_start(); more_fields('autor'); $author_field = ob_get_contents(); ob_end_clean();
    $autor = '<div class="content"><a href="' . get_bloginfo('url') . '/' . $author_field . '">' . $author_field . '</a></div>';
  12. ebenezer357
    Member
    Posted 1 year ago #

    Well, i have actually created a page under each of the authors, so the prefix of the url is the same, i mean: http://www.mysite.com what changes is the /juan or /jose

    i hoped there was something like http://www.mysite.com/%autor% or something like this.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic