Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1338966

    Joshua
    Participant

    Hey X,

    I am attempting to have a button on the front page of this site that loads a new url from an ACF within the most recent post from a specific category.

    My thought was to create a custom shortcode that pulled the data from the ACF and could pass that into the button within Cornerstone.

    I am failing at this task…

    My code so far is:

    function impact_flow_link(){
    
    $post_id = "category_events";
    $value = get_field('impactflow', $post_id);
        
    echo $value;
    
    }
    
    add_filter('init', 'add_custom_shortcode');
    
    function add_custom_shortcode() {
       add_shortcode( ‘impactflow’, ‘impact_flow_link’ );
    }

    This isn’t working at all. The short code doesn’t do anything, not even give me an error.

    I tested a generic custom shortcode that I pulled from your forum:

    function custom_shortcode() {
       $output =  '<span style="color: #ff0000; line-height: 10px; vertical-align: middle; letter-spacing: -1px;">Can this be a custom short code?</span>';
       return $output;
    }
    
    add_filter('init', 'add_custom_shortcode');
    
    function add_custom_shortcode() {
       add_shortcode('custom_shortcode_name', 'custom_shortcode');
    }
    

    This also doesn’t work.

    Am I making a silly mistake? This code is all going into my functions.php file of the Child Theme.

    As always, thanks for your amazing help and this fantastic theme.

    -Joshua

    http://newtechpdx.com
    WordPress 4.7.1
    XVersion: 4.6.4 with Child theme
    Cornerstone Version 1.3.3

    #1339292

    Rad
    Moderator

    Hi there,

    Thanks for posting in.

    I added some comments to your code,

    function impact_flow_link(){
    
    /* 
    
    the ID should be numeric, like 923, 323, 123, etc. But if you wish to get the current page/post ID then use get_the_ID(); But if it's category ID then use get_queried_object()->term_id.
    
    examples,
    
    $post_id = get_the_ID();
    
    or
    
    $post_id = get_queried_object()->term_id;
    
    */
    
    $post_id = get_the_ID();
    
    $value = get_field('impactflow', $post_id);
        
    echo $value;
    
    }
    
    /* 
    
    You don't need adding your shortcode within init hooks
    
    You can remove this
    
    add_filter('init', 'add_custom_shortcode');
    
    function add_custom_shortcode() {
    
    }
    
    Pay attention to curly quotes, this is wrong
    
       add_shortcode( ‘impactflow’, ‘impact_flow_link’ );
    
    */
    
    add_shortcode( 'impactflow', 'impact_flow_link' );
    
    

    And you didn’t mention how you’re calling this shortcode. This is just adding shortcode, what if the problem is where you’re calling it.

    Thanks!

    #1341935

    Joshua
    Participant

    Hey there,

    Thanks for the help. Getting closer, but it does not quite work yet. The shortcode now works, but only if used on post within the category that contains “ImpactFlow Link”. Need to it pull the same information but from anywhere on the site.

    I need to pull the id for the most recent post within a certian category.

    The shortcode is going to be used on a separate page, not on the post itself. We need it to pull the field from the newest post every time a new one it made within this specific category.

    Basically, on the home page, there will be a “Buy tickets now” button that will link to an external site. I want the URL to the external site to be specified in the most recent post from the ‘events’ category.

    I am using:
    $category_id = get_cat_ID('events');
    to grab the category id, but now how to I grab the most recent post id from that category?

    Seriously appreciate your help!

    #1342754

    Lely
    Moderator

    Hello There,

    I think this is the function that you need:https://codex.wordpress.org/Function_Reference/wp_get_recent_posts
    Just pass 1 on number of post and specified the category.

    Hope this helps.

    #1343763

    Joshua
    Participant

    Unfortunately this doesn’t help. I am pulling my hair attempting to figure this out.

    So far I have tried three different combinations of options:

     function impact_flow_link(){
    
    	$category_id = get_cat_ID('events');	
    
    	$args = array( 'category' => $category_id );
    
    	$recent_posts = wp_get_recent_posts( $args );
    
    	$post_id = get_the_ID( $recent_posts );
    
    	$value = get_field( 'impactflow', $post_id );
    
       	echo $value;
     }
    
    add_shortcode( 'impactflow', 'impact_flow_link' );
    
    
    function impact_flow_link(){
    
    		$args = array(
    	'post_type' => 'post',
    	'tax_query' => array(
    		array(
    			'cat' => 'events',
    			),
    		),
    	);
    	$query = new WP_Query( $args );
    
    	$post_id = get_the_ID( $query );
    	$value = get_field( 'impactflow', $post_id );
    
    }
    
    add_shortcode( 'impactflow', 'impact_flow_link' );
    
    
    function impact_flow_link(){
    
     	$args = array( 'numberposts' => '1', 'category' => 'events' );
            
    	$recent_posts = wp_get_recent_posts( $args );
            	foreach( $recent_posts as $recent ){
            echo get_field( 'impactflow', $recent  );
            }
    }
    
    add_shortcode( 'impactflow', 'impact_flow_link' );
    

    The furthest I have gotten with this is getting it to echo the URL I want on a post within the category. Meaning, if I use the shortcode on a “Event Post” it will pull the desired link. However, if I post it on any other post/page not under that category it does not work.

    #1344108

    Joshua
    Participant

    I was able to get this working with:

    function impact_flow_link2(){
    	$args = array(
    		'category_name' => 'events',
    		'orderby' => 'most_recent',
    		'posts_per_page' => '1'
    
    	);
    	$query = new WP_Query( $args );
    		if( $query->have_posts() ) {
    		while ( $query->have_posts() ) {
    			$query->the_post();
    			$post_id = get_the_ID();
    			}
    		}	
    		wp_reset_query();
    
    	$value = get_field( 'impactflow', $post_id );
    	echo $value;
    }

    Thanks for the help guys

    #1344315

    Lely
    Moderator

    Hi There,

    Glad you were able to figure this out!

    Cheers!